Menu messing up, not working properly, functions work fine without menu.

Ok so I have 2 functions, one checks for a valid password, the other capitalizes some letters. When I call them without using the menu they work fine, but when they are called using the menu, the function acts as if I pressed enter and just skips.

Basically I guess what I mean is that when I pick a choice it treats it as if I pressed enter twice, once to choose an option and again which skips the function (because you are suppose to cin something and pressing enter skips it).

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

using namespace std;
void change_case_string();
void check_valid_password();

void check_valid_password()
{
     char string[16]; //Checking the password for each condition
     int check4cap;
     bool Pass = false;
     do{
          cout << "Enter the password you would like to use,\n"
               << "it must conain atleast 5 letters, start with\n"
               << "a letter, contain atleast one capital letter\n"
               << "and have no punctuation: ";
               cin.getline (string,16);
               if (strlen(string) < 5) //Length
                  {
                                  cout << "Your password is too short! Must be atlest 5 characters.\n\n";
                  }
                  else if (!isalpha(string[0])) //1st Character
                  {cout << "The first character of your password must be a letter.\n\n";}
                  
               for(int check4cap = 0; check4cap < strlen(string); check4cap++) //Loop to check for capitals
               {
                       if (isupper(string[check4cap]))
                       {
                          cout << "Your password has been accepted.\n";
                          Pass = true;
                          break;
                       }
               }
               if (!Pass)
               {
                cout << "Your password must contain atleast one uppercase letter.\n\n";
               }
                              
               
        }
          while (strlen(string) < 5 || !isalpha(string[0]));
     
     
     system("pause");
}

void change_case_string()
{
     
     char string[50]; //First letter capitalized, and the rest after each space.
     cout << "Enter the string you would like to have capitalized: ";
     cin.getline (string,50);
     
     for(int index = 0; index < strlen(string); index++) 
     {
     	static bool makeuppercase = true; 
	  
      	if(string[index] == ' ') 
        {
		makeuppercase = true;
    	}
	
    	if(makeuppercase && string[index] >= 'a' && string[index] <= 'z') 
        {
        makeuppercase = false;
        string[index] = string[index] - 32;
      	}
     }

     cout << string << endl;

system ("pause");
     
}


int main()
{
    int loop=1;
    int choice;

    
    while(loop==1)
    {
    cout << "* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\n" 
         << "*               What would you like to do?                    *\n"
         << "*                                                             *\n"
         << "*                  1. Capitalize String                       *\n"
         << "*                  2. Check for Valid Password                *\n"
         << "*                  3. Close Program                           *\n"
         << "*                                                             *\n"
         << "* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\n"
         << "                Enter selection [1-3]: ";     
    cin >> choice;
    cout << endl;
    
    switch(choice)
    {
                  case 1:
                       change_case_string();
                  case 2:
                       check_valid_password();
                  case 3:
                       cout << "Program is now closing!\n\n";
                       cout << endl;
                       system ("pause");
                       return 0;
    }
    }


}
Last edited on
You haven't got any break statements for your cases
yes add break statements to your case statements.

break;

also your case 3 is not doing what you think it is doing... it should look more like this.
replace your return statement with loop = 0;
and put your return after your while loop.

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
{
    int loop=1;
    int choice;

    
    while(loop==1)
    {
    cout << "* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\n" 
         << "*               What would you like to do?                    *\n"
         << "*                                                             *\n"
         << "*                  1. Capitalize String                       *\n"
         << "*                  2. Check for Valid Password                *\n"
         << "*                  3. Close Program                           *\n"
         << "*                                                             *\n"
         << "* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\n"
         << "                Enter selection [1-3]: ";     
    cin >> choice;
    cout << endl;
    
    switch(choice)
    {
                  case 1:
                       change_case_string();
                       break;
                  case 2:
                       check_valid_password();
                       break;
                  case 3:
                       cout << "Program is now closing!\n\n";
                       cout << endl;
                       system ("pause");
                       loop = 0;
                       break;
                       
    }
    }

return 0;
}
Last edited on
Cool ok thanks for replies, I'm going to try this soon and post back.
I added breaks and it still treats it like I press enter 2 times when choosing an option.
SOLVED :)

The answer lies in using "getline" and "stringstream" and not "cin>>". Never use "cin>>". Look at what I added in lines 3, 83, 98 and 99. Have you been taught that? I also used amaac's switch statement (lines 105-116) instead of yours because it has breaks in each case, although I must confess, im not sure why amaac used a loop 0 instead of a return 0. In any case, the code below works flawlessly now.

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

using namespace std;
void change_case_string();
void check_valid_password();

void check_valid_password()
{
     char string[16]; //Checking the password for each condition
     int check4cap;
     bool Pass = false;
     do{
          cout << "Enter the password you would like to use,\n"
               << "it must conain atleast 5 letters, start with\n"
               << "a letter, contain atleast one capital letter\n"
               << "and have no punctuation: ";
               cin.getline (string,16);
               if (strlen(string) < 5) //Length
                  {
                                  cout << "Your password is too short! Must be atlest 5 characters.\n\n";
                  }
                  else if (!isalpha(string[0])) //1st Character
                  {cout << "The first character of your password must be a letter.\n\n";}
                  
               for(int check4cap = 0; check4cap < strlen(string); check4cap++) //Loop to check for capitals
               {
                       if (isupper(string[check4cap]))
                       {
                          cout << "Your password has been accepted.\n";
                          Pass = true;
                          break;
                       }
               }
               if (!Pass)
               {
                cout << "Your password must contain atleast one uppercase letter.\n\n";
               }
                              
               
        }
          while (strlen(string) < 5 || !isalpha(string[0]));
     
     
     system("pause");
}

void change_case_string()
{
     
     char string[50]; //First letter capitalized, and the rest after each space.
     cout << "Enter the string you would like to have capitalized: ";
     cin.getline (string,50);
     
     for(int index = 0; index < strlen(string); index++) 
     {
     	static bool makeuppercase = true; 
	  
      	if(string[index] == ' ') 
        {
		makeuppercase = true;
    	}
	
    	if(makeuppercase && string[index] >= 'a' && string[index] <= 'z') 
        {
        makeuppercase = false;
        string[index] = string[index] - 32;
      	}
     }

     cout << string << endl;

system ("pause");
     
}


int main()
{
    int loop=1;
    int choice;
    string mystring;
    
    while(loop==1)
    {
    cout << "* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\n" 
         << "*               What would you like to do?                    *\n"
         << "*                                                             *\n"
         << "*                  1. Capitalize String                       *\n"
         << "*                  2. Check for Valid Password                *\n"
         << "*                  3. Close Program                           *\n"
         << "*                                                             *\n"
         << "* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\n"
         << "                Enter selection [1-3]: ";     


    getline (cin,mystring);
    stringstream(mystring)>>choice;

    cout << endl;
    
    switch(choice)
    {
                  case 1:
                       change_case_string();
                       break;
                  case 2:
                       check_valid_password();
                       break;
                  case 3:
                       cout << "Program is now closing!\n\n";
                       cout << endl;
                       system ("pause");
                       loop = 0;
                       break;
                       
    }

    
  }



}
Last edited on
Topic archived. No new replies allowed.