-
December 19th, 2016, 03:52 AM
#1
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;
}
-
December 19th, 2016, 05:11 AM
#2
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)
-
December 19th, 2016, 11:30 AM
#3
Re: Problem with run agin loop
-
December 19th, 2016, 12:38 PM
#4
Re: Problem with run agin loop
Originally Posted by marfur
@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)
-
December 19th, 2016, 03:43 PM
#5
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?
-
December 19th, 2016, 04:02 PM
#6
Re: Problem with run agin loop
Originally Posted by marfur
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|