How do i write if input equals string or if input equals integer?

when user inputs a string, code goes berserk.
i want it to go to the very last else but instead it loops 0 as the input

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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
 #include <cstdlib> 
#include <ctime> 
#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
	double moneyinbank = 500;
	double moneywagered;
	int randomnumber1,randomnumber2;
	srand(time(NULL));
	randomnumber1 = rand() % 6 + 1;
	randomnumber2 = rand() % 6 + 1;
			cout << "Rules of the game\n";
			cout << "You have $500 in your bank.\n";
			cout << "You can only wager the amount avaliable in your bank.\n";
			cout << "Roll two dice, \nif you roll doubles the amount you wagered will be added to your total amount.";
			cout << "\nif not, the amount you wagered will be subtracted from your total amount.";
			cout <<	"\nYou can accumulate as much money as you please but if you reach 0 the game is over. \n";
			cout << "\n ";
			cout << "You have ";
			cout << moneyinbank;
			cout << " dollars in your bank.\n";
			cout << "How much would you like to wager?\n";
			cin >> moneywagered;
			if (moneywagered < 0)
			{
				cout << "You have entered ";
				cout << moneywagered;
				cout << ". Invalid amount. Run program again to start over.";
				exit(0);
			}
			else if (moneywagered > moneyinbank)
			{
				cout << "You have entered ";
				cout << moneywagered;
				cout << ". Invalid amount.\n";
				cout << "You have ";
				cout << moneyinbank;
				cout << " dollars in your bank. Run program again.";
				exit(0);

			}
			
			else if (moneywagered <= moneyinbank && moneywagered >= 0 )
			{
				while (moneyinbank > 0)
				{
					cout << "You have wagered ";
					cout << moneywagered;
					cout << " dollars.\n";
					cout << "\nNow the dice is being rolled";
					cout << "....\n";
					cout << "\n";
					srand(time(NULL));
					randomnumber1 = rand() % 6 + 1;
					cout << "You rolled a ";
					cout << randomnumber1;
					cout << " and a ";
					randomnumber2 = rand() % 6 + 1;
					cout << randomnumber2;
					cout << ".\n";
					if (randomnumber1 == randomnumber2)
					{
						cout << "You rolled doubles!\n";
						cout << "$";
						cout << moneywagered;
						cout << " is being added to your bank.\n";
						moneyinbank = moneywagered + moneyinbank;
						cout << "You now have $";
						cout << moneyinbank;
						cout << " in your bank.\n";
						cout << "How much would you like to wager?\n";
						cin >> moneywagered;
						if (moneywagered < 0 || moneywagered > moneyinbank)
						{
							cout << "You have entered ";
							cout << moneywagered;
							cout << ". Invalid amount. Run program again to start over.";
							exit(0);
						}
					}
					else if (randomnumber1 != randomnumber2)
					{
						cout << "You did not roll doubles!\n";
						cout << "$";
						cout << moneywagered;
						cout << " is being subtracted from your bank.\n";
						moneyinbank = moneyinbank - moneywagered;
						cout << "You now have $";
						cout << moneyinbank;
						cout << " in your bank.\n";
						if (moneyinbank <= 0)
							cout << "No more money in bank!\n GAME OVER!";
						else
						{
							cout << "How much would you like to wager?\n";
							cin >> moneywagered;
							if (moneywagered > moneyinbank || moneywagered < 0)
							{
								cout << "You have entered ";
								cout << moneywagered;
								cout << ". Invalid amount.\n";
								cout << "You have ";
								cout << moneyinbank;
								cout << " dollars in your bank. Run program again.";
								exit(0);
							}
												 
						}

					}
				}
			}
			else
			{
				cout << "You have entered an invalid amount. Run program again to start over.";
				exit(0);
			}
}
Last edited on
to deal with this you should read the data from the user AS a string. It will go nuts if you read it as an integer.
then convert the string to integer with your favorite tool, stoi() is one of the choices.
validation is on you, stoi isnt too smart so you can check all the input string for isdigit()s or whatever validation you want to do.
Last edited on
when user inputs a string, code goes berserk.

Extracting data from std::cin using formatted extraction (operator>>) is not easily error proof when retrieving numeric data, it is all too easy to break it.

C++: Console User Input Done Right
http://lb-stuff.com/user-input

A bit of a read, but worth the time to learn how to cut down on problems with getting numeric data from users who have fat fingers and are trying to break your code.
Last edited on
@OP
The attached several snippets show a couple of ways of handling user input errors. None of it is a simple catch all or quick fix. For instance someone 'could' conceivably have a name "1234" but an integer AbcZ.9 is neither an integer nor a float/integer or any other conceivable orthodox numerical value.

The nearest catch all in some instances is to get the user to verify their input.

https://cppguide.readthedocs.io/en/latest/cpp/exception.html
Last edited on
Topic archived. No new replies allowed.