Reputation: 13
I'm working on a project and have a similar loop to the one below where the user inputs several values in the format of a character followed by an integer. I would like to be able to exit this loop when the user simply enters the trailer value 'E'. Is it possible to get out of this loop by ONLY inputting an 'E' instead of an 'E' followed by an integer?
int main()
{
char letter;
int charge;
do
{
cout << "Input letter and charge: ";
cin >> letter >> charge;
}while(letter != 'E');
return 0;
}
Upvotes: 1
Views: 104
Reputation: 38909
I'm not a fan of while(true)
when there actually is an exit condition so I'd do this:
int main()
{
char letter = 'E';
int charge;
cout << "Input letter and charge: ";
for(cin >> letter; letter != 'E'; cin >> letter)
{
cin >> charge;
cout << "Input letter and charge: ";
}
return 0;
}
Upvotes: 3
Reputation: 1032
You can use break:
do
{
cout << "Input letter and charge: ";
cin >> letter;
if (letter == 'E') break;
cin >> charge;
} while (true);
Upvotes: 5