How to get while loop to not accept string statement

I want to get my while loop to run an error everytime someone inputs a letter or word into something that requires an int input. I don't know how to get it to work, since it just passes through and moves on regardless

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
122
123
124
125
126
127
128
129
130
#include <iostream>
#include <string>

using namespace std;

string askText(string prompt);
int askNumber(string prompt);
void tellStory(string name, string gem, string color, string weapon, string noun, int number, string bodyPart, string verb);
void welcome();

int main()
{
    welcome();
    
	char again = 'y';

    string name = askText("Please enter a name: ");
	string gem = askText("Please enter a gem: ");
	string color = askText("Please enter a color: ");
	string weapon = askText("Please enter a weapon: ");
    string noun = askText("Please enter a plural noun: ");
	int number;
	while (number)
	{
		int number = askNumber("Please enter a number: ");
		if (0 < number <= 1000)
		{
			break;
		}
		else if (number = 0)
		{
			cout << "Please choose a number larger than 0.";
			again;
		}
		else if (number > 1000)
		{
			cout << "You cannot choose a number larger than 1000.";
			again;
		}
		else
		{
			cout << "You have done something wrong. Try again.";
			again;
		}

	}

    string bodyPart = askText("Please enter a body part: ");
    string verb = askText("Please enter a verb: ");

	string& rname = name;
	string& rgem = gem;
	string& rcolor = color;
	string& rweapon = weapon;
    string& rnoun = noun;
    int& rnumber = number;
    string& rbodyPart = bodyPart;
    string& rverb = verb;
    
    tellStory(name, gem, color, weapon, noun, number, bodyPart, verb);

    return 0;
}

string askText(string prompt)
{
    string text;
    cout << prompt;
    cin >> text; 
    return text;
}

int askNumber(string prompt)
{
    int num;
    cout << prompt;
    cin >> num;
    return num;
}

void welcome()
{
	cout << "\tWelcome to Ivy's Mad Lib.\n\n";
    cout << "Answer the following questions to help create\n";
	cout << "a new story based on Steven Universe.\n\n";\
}

void tellStory(string rname, string rgem, string rcolor, string rweapon, string rnoun, int rnumber, string rbodyPart, string rverb)
{
    cout << "\nHere's your story:\n\n";
    cout << "A rare aristocrat ";
    cout << rgem;
    cout << " by the name of ";
	cout << rname;
	cout << " has gone to\n";
	cout << "report to their leader, ";
	cout << rcolor;
	cout << " Diamond.\n";
	cout << "However, while on the way, a group of rebel ";
	cout << rnoun;
	cout << " blocked ";
	cout << rname;
	cout << "'s way. \n";
	cout << rname;
	cout << " called upon the gem in their ";
	cout << rbodyPart;
	cout << " to drag out their ";
	cout << rweapon;
	cout << ".\n";
	cout << rnumber;
	cout << " ";
	cout << rnoun;
	cout << " came ";
	cout << rname;
	cout << "'s way and they destroyed them with their ";
	cout << rweapon;
	cout << ".\n";
	cout << rname;
	cout << " ran to ";
	cout << rcolor;
	cout << " Diamond, hoping that they were safe from the attack. \n";
	cout << rname;
	cout << " had to ";
	cout << rverb;
	cout << " the door to get through, but they were too late.\n";
	cout << rcolor;
	cout << " Diamond was nowhere to be found, and all the other gems\n";
	cout << "were crying from grief.\n\n";
}
You can use cin.fail() or !cin

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>

using namespace std;

int main()
{ 
	int x;
	bool run = true;
	while (run)
	{
		cin >> x;
		if (!cin) // if(cin.fail()) works too
		{
			run = false;
		}	
	}
	return 0;
}
Topic archived. No new replies allowed.