unwanted text after user chooses to rerun the code

So, I'm trying to make a program that can do simple division. The problem is that if the user wants to rerun the code (without launching it again), there is some unwanted text that appears after asking the user to input the first number again.


it shows this after a rerun:
Enter first number: Please enter a number

How would I avoid 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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <iostream>
#include <string>
#include <sstream>
using namespace std;


double ask_for_number(string const &prompt)
{
    double x;
    string line;
    while((cout << prompt << ": ") //inserts line 30 and 31
       && getline(cin, line) //reads whole line
       && !(istringstream{line} >> x)) //validate input
    {
        cout << "Please enter a number" << endl << endl;
    }
    return x; // repeats until all variables filled
}


int main()
{
  bool calculate = true;
  while (calculate)
  {


    double N1 = ask_for_number("Enter first number"); //
    double N2 = ask_for_number("Enter second number");
    double quotient = 0;

    quotient = N1 / N2;

    if (N2 == 0)
    {
        cout << "Quotient does not exist" << endl; // if divided by zero
    }
    else
    {
       cout << "Quotient is " << quotient << endl << endl; // if not divided by zero
    }

  bool loop = true; // user now decides if another calculation is needed
  while ( loop )
    {

      char choice; // user variable

      cout << "Enter 1 to continue" << endl; //options
      cout << "Enter 2 to quit" << endl;
      cout << "Your choice? "; cin >> choice; // user choice input


      if (choice == '1')
        loop = false; // condition to end program
      else if (choice == '2')
        calculate = false, loop = false; // end both loops
      else
        cout << "\n Please pick either 1 or 2 to proceed" << endl << endl;

    } // end of "while (loop)" in line 57
  } // end of "while (running)" in line 24


  return 0;
} // end of all 


Last edited on
On line 51, cin >> choice will leave a newline in the buffer to be read in next time you ask for input. Check the second post here for the fix:
http://www.cplusplus.com/forum/beginner/1988/
The idea is to ignore the rest of the line (not to keep the program open).
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream>
#include <string>

double ask_for_number( std::string prompt )
{
    double x;
    // unless we need to parse complex input, favour formatted input for reading numbers
    if( std::cout << prompt << ": " && std::cin >> x ) return x ;

    // input failed
    std::cout << "Please enter a number\n" ; // inform the user
    std::cin.clear() ; // clear the failed state
    std::cin.ignore( 1000, '\n' ) ; // throw away invalid input

    return ask_for_number(prompt) ; // try again
}

bool ask_for_continue()
{
      char choice; // user variable
      std::cout << "continue (y/n)? ";
      std::cin >> choice; // user choice input

      switch (choice)
      {
          case 'y' : case 'Y' : return true ;
          case 'n' : case 'N' : return false ;
          default:
              std::cout << "Please enter either y or n\n" ;
              return ask_for_continue() ;
      }
}

int main()
{
  do // favour simple looping constructs
  {
      const double N1 = ask_for_number("\nEnter first number"); //
      const double N2 = ask_for_number("Enter your second number");

      if (N2 == 0) std::cout << "Quotient does not exist\n" ;
      else
      {
          const double quotient = N1 / N2;
          std::cout << "Quotient is " << quotient << "\n\n" ;
      }

  } while( ask_for_continue() ) ;
}
Thanks to the both of you. I learned more than what I came here for lol (that's a good thing, of course).
Topic archived. No new replies allowed.