CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Nov 2010
    Posts
    81

    Problem with re-running a program

    I like to put a validation loop around my class assignments that asks the user if they would like to run the program, then ask again if they'd like to re-run the program. However, while working on an assignment, I noticed that my loop works for everything I've written before - but not strings. WHY!?!? Here are two examples of what I'm talking about... first is my loop that works:

    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main ()
    {
    	char rerun;
    	cout	<< "Would you like to run this program? (y/n): ";
    	cin		>> rerun;
    
    	while (rerun == 'y' ||rerun == 'Y')
    	{
    		char test[81];
    		cout	<< "Type something: ";
    		cin		>> test;
    
    		cout	<< "You typed : " << test << endl;
    
    		cout	<< "Would you like to run this program again? (y/n): ";
    		cin		>> rerun;
    	}
    
      return 0;
    }
    Now, here is what I was TRYING to do - getline a string:

    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main ()
    {
    	char rerun;
    	cout	<< "Would you like to run this program? (y/n): ";
    	cin		>> rerun;
    
    	while (rerun == 'y' ||rerun == 'Y')
    	{
    		string test;
    		cout	<< "Type something: ";
    		getline(cin, test);
    
    		cout	<< "You typed : " << test << endl;
    
    		cout	<< "Would you like to run this program again? (y/n): ";
    		cin		>> rerun;
    	}
    
      return 0;
    }


    When I run my program and answer the first question with 'y', it does this:

    Would you like to run this program (y/n)?: y
    Type something: You typed:
    Would you like to run this program again (y/n)?:
    Press any key to continue...

    Why doesn't this second program run?

  2. #2
    Join Date
    Jan 2011
    Location
    Orange County, CA
    Posts
    82

    Re: Problem with re-running a program

    Quote Originally Posted by LogicWavelength View Post
    Code:
    		string test;
    		cout	<< "Type something: ";
    		getline(cin, test);
    wait, shouldnt it be:
    Code:
                     string test;
      		cout	<< "Type something: ";
    		cin.getline(test, SIZE);
    where SIZE=whatever size you want the string to be?
    Check out this link: http://www.cplusplus.com/reference/i...tream/getline/

    (PS: you might want to add some \n and/or << endl to clean it up a bit )

  3. #3
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: Problem with re-running a program

    Why would you ask the user if they want to run the program? The fact that it's running ought to indicate that they do. From a design perspective, that's kind of goofy.

  4. #4
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,725

    Re: Problem with re-running a program

    This is a common problem when mixing operator >> and getline().

    consider :

    Code:
    char c;
    string s;
    
    cin >> c;
    getline(cin,s);
    The "cin >> c" gets the next character from the input stream.
    The "return ('\n')" that the user enters is still in the input stream.

    getline() starts with the current position in the input stream and
    reads until it a '\n' is encountered. However, the '\n' is still
    in the buffer from the "cin >> c".

    Code:
    cin >> c;
    cin.ignore(1000,'\n');  // add this line to get rid of the new-line character
    getline(cin,s);
    Usually you do not hardwire a length such as 1000 ...

    Code:
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // #include <limits>

  5. #5
    Join Date
    Jan 2011
    Location
    Orange County, CA
    Posts
    82

    Re: Problem with re-running a program

    Quote Originally Posted by GCDEF View Post
    Why would you ask the user if they want to run the program? The fact that it's running ought to indicate that they do. From a design perspective, that's kind of goofy.
    well, i did when i first started programming, pretty much for extra practice, and i also added the rerun option after the program finished.
    --maybe that's why?

  6. #6
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: Problem with re-running a program

    Quote Originally Posted by iiSoMeGuY 7x View Post
    well, i did when i first started programming, pretty much for extra practice, and i also added the rerun option after the program finished.
    --maybe that's why?
    Maybe it's time to rethink that. Run it once, then when it's done, ask if they want to rerun it.

  7. #7
    Join Date
    Jan 2011
    Location
    Orange County, CA
    Posts
    82

    Re: Problem with re-running a program

    Quote Originally Posted by GCDEF View Post
    Maybe it's time to rethink that. Run it once, then when it's done, ask if they want to rerun it.
    lol, i know. i didnt use it for programs i expected other people to use, just for practice programs i was experimenting with or whatever i was doing at the time. i dont do it anymore, just when i first started since i self-taught myself and needed the extra practice.

  8. #8
    Join Date
    Aug 2009
    Posts
    440

    Re: Problem with re-running a program

    I think what he is getting at is that it should run once, and then after all that is done, ask if you'd like to rerun. Basically instead of having the ask logic at the beginning of the loop, put it at the end. Initialize the rerun variable to true or 'y', etc so that it automatically enters the loop the first time. Or, use a do while loop.

  9. #9
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: Problem with re-running a program

    Quote Originally Posted by Alterah View Post
    I think what he is getting at is that it should run once, and then after all that is done, ask if you'd like to rerun. Basically instead of having the ask logic at the beginning of the loop, put it at the end. Initialize the rerun variable to true or 'y', etc so that it automatically enters the loop the first time. Or, use a do while loop.
    Exactly. Thank you.

  10. #10
    Join Date
    Nov 2010
    Posts
    81

    Re: Problem with re-running a program

    I actually had that realization in a dream last night. Why am I asking them if the want to run it? "They" opened my compiler and ran it. I am going to move the logic to the end only.

  11. #11
    Join Date
    Nov 2010
    Posts
    81

    Re: Problem with re-running a program

    I actually had that realization in a dream last night. Why am I asking them if the want to run it? "They" opened my compiler and ran it. I am going to move the logic to the end only.

    **EDIT**
    I simply can't mix cin >> and getline. I converted my loop to run on strings. Here it is:

    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main ()
    {
    	string rerun = "y";
    	while (rerun == "y" || rerun == "Y")
    	{
    		string test;
    		cout	<< "Type something: ";
    		getline(cin, test);
    
    		cout	<< "You typed : " << test << endl;
    
    		bool valid = false;
    		while (valid == false)
    		{
    			cout	<< "Would you like to run this program again? (y/n): ";
    			getline(cin, rerun);
    			
    			if (rerun == "y" || rerun == "Y" || rerun == "n" || rerun == "N")
    				valid = true;
    		}
    	}
    
      return 0;
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured