Thursday, July 9, 2009

C++: How do you edit a char string from a different function that you originally declared it in?

I'm using C++.





I thought passing the char string to the function that will edit it as a reference parameter would cause the char string to be directly edited, but apparently not. Why does thie following code not work?


(Please note: You can ignore the stuff about "uniqueInteger". It's just the char str that I'm concerned about...)





How do you edit a char string from a different function that you declared it in?





#include "stdafx.h"


#include %26lt;iostream%26gt;


#include %26lt;fstream%26gt;


#include %26lt;string%26gt;


using namespace std;





int getToken(ifstream %26amp; myFile, char *str)


{


str = "I want to edit this in getToken(), but I declared it in main()! Help!";


return 1;


}





void main()


{


ifstream myFile("test.txt");


int uniqueInteger = 0;


char str[200];


uniqueInteger = getToken(myFile, str);


for (int i=0; i%26lt;10; i++)


{


cout %26lt;%26lt; str;


}


cout %26lt;%26lt; "\nType was " %26lt;%26lt; uniqueInteger %26lt;%26lt;"\n";





myFile.close();


system("PAUSE");


}

C++: How do you edit a char string from a different function that you originally declared it in?
//#include "stdafx.h"


#include %26lt;iostream%26gt;


#include %26lt;fstream%26gt;


#include %26lt;string%26gt;


using namespace std;





int getToken(ifstream %26amp; myFile, char *str) {


cout%26lt;%26lt;"function call "%26lt;%26lt;endl;


char alfa[]="final1";


int i=0;


while((*str!='\0')%26amp;%26amp;(i%26lt;6)){


*str=alfa[i];


++str;


i++;


}





return 1;


}





int main(){


ifstream myFile("test.txt");


int uniqueInteger = 0;


//char *str = new char[200];


char str[] = "inicio";


cout%26lt;%26lt;"inicial main "%26lt;%26lt;str%26lt;%26lt;endl;


uniqueInteger = getToken(myFile, str);


for (int i=0; i%26lt;10; i++){


cout %26lt;%26lt;" cada linea en main "%26lt;%26lt;i%26lt;%26lt;"-%26gt;"%26lt;%26lt;str%26lt;%26lt;endl;


}


cout %26lt;%26lt; "\nType was " %26lt;%26lt; uniqueInteger %26lt;%26lt;"\n";





myFile.close();


system("PAUSE");


return 0;


}


i think this is you want be more specific. bye


No comments:

Post a Comment