Correct information? Yes or no Questions.

So if you look over my code you can see that I have the user restart the program, instead of that I want to create some sort of loop to start back to having the user insert the correct information.

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
  #include <iostream>                                     
#include <string>                                     
#include <iomanip>                                     
#include <string>

using namespace std;                                    

int main()                                              
{                                                       
	string name;                            
	double gpa = 0;
	int scholarship = 0;


	cout << "Please enter name, gpa, and scholarship amount. ";
	cin >> name >> gpa >> scholarship;
	cout << endl;
	

	cin.clear();

	cin.ignore(200, '\n');


	cout << "Name: " << name << endl;
	cout << "GPA: " << gpa << endl; 
	cout << "Scholarship: " << scholarship << endl;

	cout << "Is the information you entered correct (yes/no) ";
	
	string answer;
	cin >> answer;


	if (answer == "yes" || answer == "Yes")
	{
		cout << setw(10) << name << setw(7) << gpa
			<< setw(8) << scholarship << endl;

		cout << setfill('*');
		cout << setw(10) << name << setw(7) << gpa
			<< setw(8) << scholarship << endl;

		cout << setw(10) << name << setfill('#')
			<< setw(7) << gpa
			<< setw(8) << scholarship << endl;

		cout << setw(10) << setfill('@') << name
			<< setw(7) << setfill('#') << gpa
			<< setw(8) << setfill('^') << scholarship
			<< endl;

		cout << setfill(' ');
		cout << setw(10) << name << setw(7) << gpa
			<< setw(8) << scholarship << endl;
	}

	else if (answer == "no" || answer == "No")
	{
		cout << "Please restart the program and try again." << endl;
	
	}
    return 0;                                           
}
Around all the code you want to repeat, put
1
2
3
do {
  // code that may need repeating
} while ( !(answer == "yes" || answer == "Yes") );


> else if (answer == "no" || answer == "No")
What if the user answers "maybe" or "perhaps" or "not sure" ?
Topic archived. No new replies allowed.