I tried so hard to get all the functions working ... and now just when I'm putting the whole program together, the first function is giving me craps ! =(
Really weird, but this function which accepts string into an array (got help from someone here earlier) works perfectly on its own but when put in some sort of a control loop or a switch , it goes crazy... this is what I mean:
and this works FINE ...Code:#include <iostream.h> #include<string.h> void enter(); const int MAX = 3; char array[MAX][81]; //declared globally for other functions later. int i; int main(void) { enter (); return 0; } ----------------------------------string into array function---------------- void enter() { for (i = 0; i<MAX ; i++) { cout << "enter a string " <<endl; cin.getline(array[i], 80, '\n'); } for(i=0; i<MAX; i++) cout<<array[i]<<endl; return; }
but when I have this:
...it only accepts 2 string instead of 3 ! (MAX=3). The output is like this:Code:#include <iostream.h> #include<string.h> void enter(); const int MAX = 3; char array[MAX][81]; int i; int main(void) { char answer; cout<<"Enter 'E' to start entering text"<<endl; cin>>answer; if (answer=='E') { enter (); } return 0; } void enter() { for (i = 0; i<MAX ; i++) { cout << "enter a string " <<endl; cin.getline(array[i], 80, '\n'); } for(i=0; i<MAX; i++) cout<<array[i]<<endl; return; }
what the heck is wrong? I tried using cin.ignore() to stop the message from displaying twice, but then strings are not saved properly if i use that.Code:Output: Enter 'E' to start entering text Input : E Output: enter a string enter a string //it goes weird here, the message is display twice, skip array[0] Input : 1st string Ouput : enter a string Input : 2nd string Displays: //nothing on this line 1st string 2nd string



LinkBack URL
About LinkBacks



