Thread: having problem with string statement during a loop!

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    7

    having problem with string statement during a loop!

    I'm using a string during a loop and for some reason when the loop repeats it puts the "enter name : " statement and "enter first number : " on the same line for example: "enter name: enter 1st number:".
    I want them on seperate lines. I was going to use fflush(stdin) to fix this problem but after reading the FAQ on this board I had second thoughts. Is there another way around this problem or did I write the code wrong?

    And how do I use code tags? there is an option to close current tag and close all tags but for some reason I can't get it to work?I have no experience with HTML


    [

  2. #2
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    Code:
    cout << "Enter name: ";
    cin >> name;
    cout << endl
         << "Enter first number: ";
    cin >> first_number;
    To use code tags, put all your code between [co de] Code goes here [/co de], except, of course, omit that space so that "code" is one solid word.

  3. #3
    Registered User
    Join Date
    Apr 2005
    Posts
    7
    I inserted the solution but i still get the same problem, I have listed the code to show you my problem (by the way thanks for showing me how to use the code tags).


    BEFORE
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main()
    {
    			// declaration of variables
    			string name2 = "NONE";
    			string name  = " ";
    			int num1     = 0;
    			int num2     = 0;
    			int answer   = 0;
    		
    			cout << "Enter name: ";
    			getline(cin, name);
    			cout <<endl;
    		
    		 while (name != "NONE")
    		{
    			
    			cout << "enter 1st number: ";
    			cin >> num1;
    			cout << "Enter 2nd number: ";
    			cin >> num2;
    
    			// Processing area of program
    			answer = num1 + num2;
    
    			// Output area of program
    			cout << "Answer is: " << answer << endl;
    			cout << endl;
    			cout << "Enter next name: ";
    			getline(cin, name);
    			
    		} //end if
    
    
    	return 0;
    }
    
    
    /*
    Enter name: NONE
    Press any key to continue
    */
    
    
    /*Enter name: xxxxx
    enter 1st number: 3
    Enter 2nd number: 8
    Answer is: 11
    
    enter name: enter 1st number  _ promts here
    */
    AFTER
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main()
    {
    			// declaration of variables
    			string name2 = "NONE";
    			string name  = " ";
    			int num1     = 0;
    			int num2     = 0;
    			int answer   = 0;
    		
    			cout << "Enter name: ";
    			getline(cin, name);
    		
    		
    		 while (name != "NONE")
    		{
    			
    			cout << "enter 1st number: ";
    			cin >> num1;
    			cout << "Enter 2nd number: ";
    			cin >> num2;
    
    			// Processing area of program
    			answer = num1 + num2;
    
    			// Output area of program
    			cout << "Answer is: " << answer << endl;
    			cout << endl;
    			cout << "Enter next name: ";
    			getline(cin, name);
    			cout << endl;
    			
    		} //end if
    
    
    	return 0;
    }
    
    
    /*
    Enter name: xxxxxx
    enter 1st number: 21
    Enter 2nd number: 21
    Answer is: 42
    
    Enter next name:
    enter 1st number  _  promts here
    */

  4. #4
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    When you use the stream extraction operator, white space like ' ' and tabs and newlines are removed from or ignored by the stream.

    getline(), however, reads until a newline is reached. But, that newline remains in the input buffer. To get rid of that newline, you can use the ignore() function.

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    for a more detailed discussion of joshdicks answer, if you want it, you can read the responses here:


    http://cboard.cprogramming.com/showthread.php?t=64574
    You're only born perfect.

  6. #6
    Registered User
    Join Date
    Apr 2005
    Posts
    7
    Well I tried what ya'll suggested and it works great! . Did I insert the ignore() function in its proper place or should I leave it just where it is?

    Code:
    			cout << "Enter name: ";
    			getline(cin, name);
    			
    		
    		 while (name != "NONE")
    		{
    			
    			cout << "enter 1st number: ";
    			cin >> num1;
    			cout << "Enter 2nd number: ";
    			cin >> num2;
    
    			// Processing area of program
    			answer = num1 + num2;
    
    			// Output area of program
    			cout << "Answer is: " << answer << endl;
    			cout << endl;
    			cout << "Enter next name: ";
    			cin.ignore();
    			getline(cin, name);
    		
    			cout << endl;
    			
    		} //end if
    
    
    	return 0;
    }
    
    
    /*
    Enter name: xxxxx
    enter 1st number: 23
    Enter 2nd number: 23
    Answer is: 46
    
    Enter next name: xxxxxx
    
    enter 1st number: 23
    Enter 2nd number: 23
    Answer is: 46
    
    Enter next name: NONE
    
    Press any key to continue
    */

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C String Problem: Not reading end of string
    By sedavis4 in forum C Programming
    Replies: 5
    Last Post: 11-17-2008, 10:29 PM
  2. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  3. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 11:06 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM