Stopping a loop that takes an int with a letter

When the user enters 'q' or 'Q' I want the program to exit the loop. What confuses me with this is that I'm taking in an integer so I'm unsure of how to stop it when a char is enter.

Currently it loops fine until I enter a 'Q' then it endlessly loops results. How can I accomplish this?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
  int main(){
     // Create an instance of the DayOfYear class
    DayOfYear dayOfYearObj;

    int day; // To hold the day
    bool stop = false;

    // Display the purpose of the program.
    cout << "This program converts a number \n"
    << "into a string representing the \n"
    << "month and day.\n\n";
    
    while(stop == false)
        {
            // Get the day as input from the user.
            cout << "\nEnter a whole number between 1 and 365: ";
            cin >> day;
            if( day == 'q' || day == 'Q')
            {
                stop = true;
            }
            else
            {// Set the day.
            dayOfYearObj.setDay(day);

             // Display the object.
            dayOfYearObj.print();
            }
            
        }

    return 0;
}
Well inputting a char into an int will lead to undefined behavior. You could check if cin enters a fail state and then stop it then, but the program would be able to be stopped if anything other than a number was entered, not just q. Perhaps make day a char and then convert if not 'q'?
This program stops reading when the string "q" or "Q" is entered, or the end-of-stream marker is reached, or an I/O error occurs:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <sstream>
#include <string>

int
main()
{
  // Loop condition is false if std::cin reads past the end-of-stream, or if an 
  // I/O error occurs
  for (std::string token; std::cin >> token;) {
    // Quit if q or Q is entered
    if (token == "q" || token == "Q")
      break;

    int result;

    { // convert token to integer, or try again
      std::istringstream ss{ token };
      if (!(ss >> result))
        continue;
    }

    std::cout << "read " << result << "\n";
  }
}


http://coliru.stacked-crooked.com/a/c6655979131c8000
In general, you can use a structure like the above to ignore only malformed chunks of input.

After the loop, you can check to make sure that no error occurred by checking the state of std::cin again.
Last edited on
If you are happy that input is just going to be an integer or a Q, and you are not worried about other failure conditions, then you could try cin.peek():
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
using namespace std;

int main()
{
   char c;
   int i;

   while ( true )
   {
      cout << "Enter an integer, or q to quit: " << flush;
      cin >> ws;
      c = cin.peek();
      if ( c == 'q' || c == 'Q' )
      {
         cout << "It's time to go.";
         return 0;
      }
      cin >> i;
      cout << "You entered " << i << "\n\n";
   }
}
Last edited on
A few good concepts here I'll give each a try just to learn different methods. Thanks!
Topic archived. No new replies allowed.