Monday, July 27, 2009

C++: Why does this not work?

I want to declare a character string in main() and edit it's contents in rewriteString.





rewriteString()'s main purpose is to return an integer, so the character string will have to be edited directly rather that being returned (since you can't return both an integer and a character string at the same time). My compiler is giving me conversion problems with the following code:





#include "stdafx.h"


#include %26lt;iostream%26gt;


using namespace std;





int overwriteString(char str)


{


str = "This is what I want to overwrite main()'s char str with!";





// I'll eventually want to process information here


// that will result in an integer that must be returned to main().


// I'll temporarily just use "1"...





return 1;


}





void main()


{


int uniqueInteger = 0;


char str[ ] = "I want to overwrite this string with the message from getToken()";


uniqueInteger = getToken(*%26amp; str);


cout %26lt;%26lt; str;


cout %26lt;%26lt; "\n Integer returned: " %26lt;%26lt; uniqueInteger %26lt;%26lt; "\n";





system("PAUSE");

C++: Why does this not work?
This is a good question. Two things to keep in mind.





1)The name of an array is a pointer to the beginning of the array.





So, the syntax you are using to pass str to getToken is a little funky. We'll come back to that.





Second, you have to decide what you want to do to str. Right now, it points to the beginning of an array of characters. Do you want to change those characters, or do you want str to point to an entirely new array of characters?





I'll assume you just want to overwrite the original array of characters. So, first, you should pass str properly. Since it's a pointer to an array (point 1), you don't need to use %26amp; or *. Just say getToken(str).





The parameter for getToken should be (char * s) The variable name doesn't matter, but the type does. If you like, you can refer to elements of s using array notation. This is weird, but it's a consequence of rule 1.





You can also use string functions with s, of course, which might be more useful. So, let's rename overwriteString() to getToken() so it gets called by main(), and try the following code:





int getToken(char *s){





strcpy(s, "My new string.");


return 1;


}





This will work.





Why wouldn't





s="My new string.";





work?





Because it changes the value of s so it points to a new string entirely. The string declared as str in main() is unchanged. I don't think this is what you wanted.
Reply:#include %26lt;iostream%26gt;





you forgot the .h (#include%26lt;iostream.h%26gt;)
Reply:hi ...the whole program isnt there so i cant debug the whole thing for you but i can tell you some sure errors from it





line 1.. Even if its another file always #include %26lt;stdfx.h%26gt;


you have to use arrow brackets always


line 2... error in the header file it should be #include%26lt;iostream.h%26gt;


the .h is missing there


line 3...im not to sure i'v even seen something like that but it check it out


line 19.... Please use another variable name instead of 'str' beacuase some inbuikt functions in C++ have the thing str. You can use myStr or anything else like that


send me a message and show me the whole program and i will debug it for u !

800flowers.com

No comments:

Post a Comment