TO CHAINSAW - or anybody else who knows C++

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Aug 2004
Posts: 153
Reputation: cosi is an unknown quantity at this point 
Solved Threads: 1
cosi's Avatar
cosi cosi is offline Offline
Junior Poster

Re: TO CHAINSAW - or anybody else who knows C++

 
0
  #11
Aug 16th, 2004
Hi C++,

You cannot use a char* pointer for which you have not allocated space. char* support contains a junk value unless you do a) or b).

You can either
a) not use a pointer. do char[255] support;
b) use malloc or new. char* support; support = new char[255];
if you do option b), then you must remember to do delete support; when you are done with it.

You shouldn't be doing *support = "....."
use the strcpy from C.

  1. #include <strings.h> // for the strcpy..
  2.  
  3. #define WINDOWS_LINE "888-555-1234 option 2"
  4. #define MAC_LINE 888-555-1234 option 3"
  5.  
  6. // .... in your code
  7. // REMEMBER to do option a) or b) like I said
  8. #ifdef WINDOWS
  9. strcpy(support, WINDOWS_LINE);
  10. #else
  11. strcpy(support, MAC_LINE);
  12. #end

C++, which certification are you going for? I strongly advise you to take a class in C++. Getting the answers from this forum alone won't help you pass.


Ed


Originally Posted by C++
char*support;
if(WINDOWS)*support="888-555-1234 option 2"; *support="888-555-1234 option 3";

but i'm wrong.

Please help,
C++
Reply With Quote Quick reply to this message  
Join Date: Aug 2004
Posts: 350
Reputation: Ghost is an unknown quantity at this point 
Solved Threads: 2
Ghost's Avatar
Ghost Ghost is offline Offline
Posting Whiz

Re: TO CHAINSAW - or anybody else who knows C++

 
0
  #12
Aug 16th, 2004
I've got through 226 of 234 questions of certification on my own. These last eight are VERY difficult. When I tried to run your code through the compiler, the compiler says I need to use char type and that your code declaresª support twice or acts unpredictably when neither macro is defined. Can you please help me mith this and the other ?s posted on: TO CHAINSAW or anybody else who knows C++.

Thanks,
C++

P.S. Is the correct code for the problem:

#include <strings.h>
#define WINDOWS ("888-555-1234 option 2")
#define MACOS ("888-555-1234 option 3")


char* support;
*support = new char[255];
#ifdef WINDOWS{
strcpy(*support, WINDOWS);
delete[] *support;
}
#else{
strcpy(*support, MACOS);
delete[] *support;
}
#end

Oh yeah... I solved problems #1, 3, and 4.
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 759
Reputation: Killer_Typo will become famous soon enough Killer_Typo will become famous soon enough 
Solved Threads: 35
Killer_Typo's Avatar
Killer_Typo Killer_Typo is offline Offline
Master Poster

Re: TO CHAINSAW - or anybody else who knows C++

 
0
  #13
Aug 16th, 2004
[QUOTE=C++]I've got through 226 of 234 questions of certification on my own. These last eight are VERY difficult. When I tried to run your code through the compiler, the compiler says I need to use char type and that your code declaresª support twice or acts unpredictably when neither macro is defined. Can you please help me mith this and the other ?s posted on: TO CHAINSAW or anybody else who knows C++.

Thanks,
C++
[QUOTE]

ahhh i see im sorry, i misunderstood you, thought you had only done the first ten and were getting help for each. not that you only had ten left :-p
Dont forget to spread the reputation to those that deserve!
Reply With Quote Quick reply to this message  
Join Date: Aug 2004
Posts: 350
Reputation: Ghost is an unknown quantity at this point 
Solved Threads: 2
Ghost's Avatar
Ghost Ghost is offline Offline
Posting Whiz

Re: TO CHAINSAW - or anybody else who knows C++

 
0
  #14
Aug 16th, 2004
It's okay - but could you help me with the questions I have?
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 436
Reputation: Chainsaw is an unknown quantity at this point 
Solved Threads: 11
Chainsaw's Avatar
Chainsaw Chainsaw is offline Offline
Unprevaricator

Re: TO CHAINSAW - or anybody else who knows C++

 
0
  #15
Aug 16th, 2004
cosi's version of #ifdef WINDOWS looks better than yours; WINDOWS is a pre-processor directive defined outside your code, so you'd say something like
  1. const char *support = NULL; // const char because we'll point to things we can't change.
  2. #ifdef WINDOWS
  3. support = "888-555-1234 option 2"; // point to the constant string
  4. #endif
  5. #ifdef MACOS
  6. support = "888-555-1234 option 3";
  7. #endif
  8. if (support)
  9. printf( "%s\n", support ); // one of the options was taken
  10. else
  11. printf( "Neither WINDOWS nor MACOS were defined!\n" );
Reply With Quote Quick reply to this message  
Join Date: Aug 2004
Posts: 153
Reputation: cosi is an unknown quantity at this point 
Solved Threads: 1
cosi's Avatar
cosi cosi is offline Offline
Junior Poster

Re: TO CHAINSAW - or anybody else who knows C++

 
0
  #16
Aug 16th, 2004
No. Your code is wrong. I thought I taught you how pointers and dereferencing work. *sigh* I guess I won't lose my cool over your fishing for answers. Do heed our advice; All these questions should come easy to those seeking certification. Could you please give a background of your experience with C++? I still think you need to take a class and get some real project experience with the language.

Anyways, I cooked up the solution below. Study it and understand what each line means. You should be able to reproduce the same functionality without any references if you are looking to get certification.


Ed

  1. #include <stdio.h>
  2. #include <strings.h>
  3.  
  4. #define WINDOWS
  5. #define WL "hi"
  6. #define ML "hi2"
  7.  
  8. void test() {
  9. char *support = new char[255];
  10. #ifdef WINDOWS
  11. strcpy(support, WL);
  12. #elif MAC
  13. strcpy(support, ML);
  14. #endif
  15.  
  16. printf("%s", support);
  17. delete support;
  18. }
  19.  
  20.  
  21. int main() {
  22. test();
  23. }


Originally Posted by C++
#include <strings.h>
#define WINDOWS ("888-555-1234 option 2")
#define MACOS ("888-555-1234 option 3")


char* support;
*support = new char[255];
#ifdef WINDOWS{
strcpy(*support, WINDOWS);
delete[] *support;
}
#else{
strcpy(*support, MACOS);
delete[] *support;
}
#end

Oh yeah... I solved problems #1, 3, and 4.
Reply With Quote Quick reply to this message  
Join Date: Aug 2004
Posts: 350
Reputation: Ghost is an unknown quantity at this point 
Solved Threads: 2
Ghost's Avatar
Ghost Ghost is offline Offline
Posting Whiz

Re: TO CHAINSAW - or anybody else who knows C++

 
0
  #17
Aug 16th, 2004
I got the same error message as before. The message is:

Your code declaresÂ* support twice or acts unpredictably when neither macro is defined.

an you please help?
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 436
Reputation: Chainsaw is an unknown quantity at this point 
Solved Threads: 11
Chainsaw's Avatar
Chainsaw Chainsaw is offline Offline
Unprevaricator

Re: TO CHAINSAW - or anybody else who knows C++

 
0
  #18
Aug 16th, 2004
C++, you posed the question as:

"Write conditionalª code that will produce the line of source code charª *support="888-555-1234 option 2"; if the macro WINDOWS is defined, and charª *support="888-555-1234 option 3"; if the macro MACOS is defined. (Note: it will never be the case that both MACOS and WINDOWS are simultaneously defined.) "


so I guess the literal interpretation of that is:
  1. #ifdef WINDOWS
  2. char* support = "888-555-1234 option 2";
  3. #endig
  4. #ifdef MACOS
  5. char* support = "888-555-1234 option 3";
  6. #endif
Though I still prefer my suggestion for a couple of reasons; (1) the char* may as well be a const char*, though that's not what the question asked, and (2) what if neither WINDOWS nor MACOS were defined? The question doesn't say, but I think in a real program you'd have to deal with that.

COSI - were you saying my code was wrong? What was wrong with it? You said something about pointers and dereferencing; can you enlighten me?
Reply With Quote Quick reply to this message  
Join Date: Aug 2004
Posts: 350
Reputation: Ghost is an unknown quantity at this point 
Solved Threads: 2
Ghost's Avatar
Ghost Ghost is offline Offline
Posting Whiz

Re: TO CHAINSAW - or anybody else who knows C++

 
0
  #19
Aug 16th, 2004
It's correct - with a minor error. You forgot to delete support after using it. I couldn't have figured it out without your help. I only have 4 questions left for my certification!!!
Reply With Quote Quick reply to this message  
Join Date: Aug 2004
Posts: 350
Reputation: Ghost is an unknown quantity at this point 
Solved Threads: 2
Ghost's Avatar
Ghost Ghost is offline Offline
Posting Whiz

Re: TO CHAINSAW - or anybody else who knows C++

 
0
  #20
Aug 17th, 2004
Can you help me with the following problem too?

Define a macro MAX that accepts two parametersÂ* and expands to the valueÂ* of the largerÂ* of the two (you'll need the conditionalÂ* operatorÂ* for this).

I think the answer is:

#define MAX(x,y) (
if(x>y)delete(y)
else if((y)>(x))delete(x)
)


Thanks,
C++
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC