Thread: some really weird problems with string !

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    37

    some really weird problems with string !

    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:


    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;
    }
    and this works FINE ...
    but when I have 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;
    }
    ...it only accepts 2 string instead of 3 ! (MAX=3). The output is like this:

    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
    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.
    Last edited by mellisa; 01-20-2003 at 02:06 AM.

  2. #2
    Registered User
    Join Date
    Dec 2002
    Posts
    103

    This should solve ur problem

    Code:
    #include <iostream.h> // change this to new style
    #include<string.h>    // change this to new style
    
    
    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;
    	cin.ignore(); // Just added this. Since you are accepting the value
    				  // in a character variable, the input stream still has
    	              // the enter symbol. Just flush it out.
    
    	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 0; // added 0 here
    }
    Have a wonderful day.... and keep smiling... you look terrific that way
    signing off...
    shiv... as i know him

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    37
    thanks,

    I got the exact same answer 10min ago , help from another friend....

    wah thanks everybody ! now i can move on with my program , hopefully you guys never have to see me here again hehe

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  2. String Manipulation problems -_-
    By Astra in forum C Programming
    Replies: 5
    Last Post: 12-13-2006, 05:48 PM
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  4. Replies: 4
    Last Post: 03-03-2006, 02:11 AM
  5. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 11:06 PM