cin fail

so when i try to put a letter for number of children for the cin to fail and inform that " number of child must be a number" it shows up for adults as "number of adult must be a number instead.

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
 #include <iostream>
#include <cmath>
#include <string>
#include <iomanip>
using namespace std;


int
main ()
{

  string movieName;
  int noOfAdultTicketSold;
  int noOfChildTicketSold;
  const double AdultTicFee = 6;
  const double ChildTicFee = 3;

  cout << "movie name:";
  getline (cin, movieName);
  cout << "adult tickets sold?";
  cin >> noOfAdultTicketSold;
  cout << "child tickets sold?";
  cin >> noOfChildTicketSold;


  cout << setw (30) << left << "Movie Name:" << "\"" << movieName << "\"" <<
    endl;
    
  cout << setw (33) << left << "Adult Tickets Sold:" << noOfAdultTicketSold;
  cout << setw (5) << "" << endl;
  if (cin.fail ())
    {
      cout << endl << "Number of adults must be a number." << endl;
      exit (0);
    }

  cout << setw (33) << left << "Child Tickets Sold:" << noOfChildTicketSold;
  cout << setw (5) << "" << endl;


  if (cin.fail ())
    {
      cout << endl << "Number of child must be a number." << endl;
      exit (0);
    }
  double TotalTake =
    AdultTicFee * noOfAdultTicketSold + noOfChildTicketSold * ChildTicFee;

  cout << setw (30) << left << "Gross Box Office Profit: " << "$  " <<
    setw (7) << right << fixed << setprecision (2) << TotalTake << endl;

  double NetBoxofficeProfit = TotalTake * 0.2;

  cout << setw (30) << left << "Net Box office Profit: " << "$  " << setw (7)
    << right << NetBoxofficeProfit << endl;

  double AmountPaidtoDistributor = TotalTake - NetBoxofficeProfit;

  cout << setw (30) << left << "Amount Paid to Distributor: " << "$  " <<
    setw (7) << right << AmountPaidtoDistributor << endl;


  return 0;
Ok so in cpp it is very linear, when your code runs it starts at the top and works its way down. So when you enter a letter for either of your tickets inputs it continues to work its way down the page and it hits the first cin.fail statement but the cin.fail statement doesn't know the difference between which ticket input was bad it just knows that a cin statement failed. A solution is that after getting input check it immediately that it's usable then mover on.

Also, I don't know if you know how to use loops yet but if you do it would a much better idea that instead of just testing if the input was bad but if it is bad get new input until you get good input.
Hello poonamp6792,

If you are going to check if "cin" fails you need to do this after the "cin". The way you have it you have no idea which "cin" failed.

Two things that is missing from your if statements are:
1
2
3
4
// Error message, which you have.

std::cin.clear();  // <--- To clear the state bits of "cin"
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Requires header file <limits>. Clears the input buffer. 


So the if statement at line 31 should come after line 21. And line 41 should move to 24.

Give that a try and see how it works. Once I restart my computer I have a different solution you can try.

Hope that helps,

Andy
Hello poonamp6792,

Give this a try:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
	std::cout << "adult tickets sold? ";
	std::cin >> noOfAdultTicketSold;

	while (!std::cin)
	{
		std::cout << "\n Invalid input. Need a whole number.: \n" << std::endl;
		std::cin.clear();
		std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Requires header file <limits>.

		std::cout << "adult tickets sold? ";
		std::cin >> noOfAdultTicketSold;
	}

	std::cout << "child tickets sold? ";
	std::cin >> noOfChildTicketSold;

	// Same while loop as above.


You could follow the while loop, which handles non numeric input, with any other verification that you may want.

Hope that helps,

Andy
Topic archived. No new replies allowed.