CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Dec 2016
    Posts
    20

    Problem with run agin loop

    I'm having problem with the loop that asks the user if she/he want to run the program again. When I input something else then Y or N the program gets in an infinite loop, how can I fix that?

    Code:
    int main()
    {
        char ch;
        
        do
        {
            string Start,End;
            
            cout<<"Some content: ";
            getline(cin, Start);
            
            cout<<"Some content: ";
            getline(cin, End);
            
            cout<<convert(Start,End)<<" kr" << endl;
            
            cout << "Run program again(y/n)? ";
            
            cin >> ch;
            cin.ignore(250,'\n');
            do
            {   ch = toupper(ch);
            } while ( !(ch == 'Y' || ch == 'N'));
        } while (ch == 'Y');
        return 0;   
    }

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: Problem with run agin loop

    Code:
    do
    {
        ch = toupper(ch);
    } while ( !(ch == 'Y' || ch == 'N'));
    Within the loop, where do you obtain a new input from the user if the input is not as expected?
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3
    Join Date
    Dec 2016
    Posts
    20

    Re: Problem with run agin loop

    @2kaud nowhere I guess

  4. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: Problem with run agin loop

    Quote Originally Posted by marfur View Post
    @2kaud nowhere I guess
    Yep - that's why you get an infinite loop. If the input isn't as required then you need to display an error message and re-prompt the user and obtain input again.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  5. #5
    Join Date
    Dec 2016
    Posts
    20

    Re: Problem with run agin loop

    Okay thanks @2kaud ! How do I do if I want to end the program regardless of what the user inputs except Y?

  6. #6
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: Problem with run agin loop

    Quote Originally Posted by marfur View Post
    How do I do if I want to end the program regardless of what the user inputs except Y?
    That's not what your original code was trying to achieve - and the user prompt does ask for y/n. However, if any answer will do and only loop again if Y entered then consider
    Code:
    	char ch;
    	do {
    		//rest of program here
    
    		cout << "Run program again(y/n)? ";
    		cin >> ch;
    	} while (toupper(ch) == 'Y');
    However, if you want it as per your original, then consider
    Code:
    	char chu;
    	do {
    		//rest of program here
    
    		do {
    			char ch;
    			cout << "Run program again(y/n)? ";
    			cin >> ch;
    			chu = toupper(ch);
    			if ((chu != 'N') && (chu != 'Y'))
    				cout << "Invalid input." << endl;
    
    		} while ((chu != 'N') && (chu != 'Y'));
    	} while (chu == 'Y');
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

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