Looking to Loop A menu

So I've searched all over but I can't seem to find any help on how to loop menus a specific way. I know this question has been asked a million times, but I can't wrap my head around how to go about it. I want to loop the portion where the user enters their info and after it is displayed. Start from the top if the user enters in N.

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

#include <string>

#include <cmath>

using namespace std;

//define max variable lengths for inputs.

 #define MAX_NAME_LENGTH   50
 #define MAX_EMAIL_LENGTH    100
 #define MAX_DESCRIBE_LENGTH   300

int main (){
	
// Set variables for inputs by user using char

	char userName[MAX_NAME_LENGTH];
	char userEmail[MAX_EMAIL_LENGTH];
	char userInfo[MAX_DESCRIBE_LENGTH];
	
	const char nl2[] = "\n\n"; //creates two break lines
    
	
// prompts asking for user input

   cout << "Hello and welcome to VendPro Profile Builder"<< endl<< endl;
   
   cout << "Please enter your name  to begin" << endl << nl2 << endl;
   
   cin.getline(userName,MAX_NAME_LENGTH); // will alow user to input requested info
	
   cout << " Thank you. Now, please enter in a valid email address" << endl << nl2 << endl;
   
   cin.getline(userEmail,MAX_EMAIL_LENGTH);
   
   
   cout << "Thank you. Now, tell us about yourself? When complete please enter # to complete. " << endl <<nl2<< endl;
   
   cin.getline(userInfo,MAX_DESCRIBE_LENGTH,'#');
   
   cout << "Please Review information below. Is it correct? " << endl<< nl2 << endl;
   
   cout << " 	Your information" << endl << endl;
   
   cout << "========================" << endl;
   
   cout << "Name : " << userName << endl;
   
   cout << "Email : " << userEmail << endl;
   
   cout << " About You :" << userInfo << endl;

   cout << "Is the information Correct? Please enter "Y" for Yes and "N" for No " << endl;
   
   
}
Last edited on
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
 #include <iostream>

#include <string>

#include <cmath>

using namespace std;

//define max variable lengths for inputs.

 #define MAX_NAME_LENGTH   50
 #define MAX_EMAIL_LENGTH    100
 #define MAX_DESCRIBE_LENGTH   300

int main (){
	
// Set variables for inputs by user using char

	char userName[MAX_NAME_LENGTH];
	char userEmail[MAX_EMAIL_LENGTH];
	char userInfo[MAX_DESCRIBE_LENGTH];
	
	const char nl2[] = "\n\n"; //creates two break lines
    
	
// prompts asking for user input

   cout << "Hello and welcome to VendPro Profile Builder"<< endl<< endl;
   
   string userInput = "N";
   while (userInput == "N")
  {
   cout << "Please enter your name  to begin" << endl << nl2 << endl;
   
   cin.getline(userName,MAX_NAME_LENGTH); // will alow user to input requested info
	
   cout << " Thank you. Now, please enter in a valid email address" << endl << nl2 << endl;
   
   cin.getline(userEmail,MAX_EMAIL_LENGTH);
   
   
   cout << "Thank you. Now, tell us about yourself? When complete please enter # to complete. " << endl <<nl2<< endl;
   
   cin.getline(userInfo,MAX_DESCRIBE_LENGTH,'#');
   
   cout << "Please Review information below. Is it correct? " << endl<< nl2 << endl;
   
   cout << " 	Your information" << endl << endl;
   
   cout << "========================" << endl;
   
   cout << "Name : " << userName << endl;
   
   cout << "Email : " << userEmail << endl;
   
   cout << " About You :" << userInfo << endl;

   cout << "Is the information Correct? Please enter 'Y' for Yes and 'N' for No " << endl;

  cin >> userInput;
  }
}
string userInput = "N";
while (userInput == "N")
{

^^ do/while avoids 'seeding' a value in a loop. Its not important here because the
variable creation and init works, but if the loop variable had already existed, you can avoid
seeding it like this:
do
{
...
cin>>userInput ;
...
} while (userInput == "N");

macros have type problems. its better to use constants, which strongly type the value. sure, its fine here in simple code, but in more complex code being unsure of the real type of those #defines can be annoying.
Last edited on
@jonnin and @repeater, thank you! I figured a do/Whle would be the best possible way.
Topic archived. No new replies allowed.