New Posts  All Forums:Forum Nav:

C++ menu trouble

post #1 of 3
Thread Starter 
OK I'm creating a menu, first time using a switch sadly lol. Where is my fault in why E will not exit;

Code:
char menu();
int vowels(string);
int consonants(string);
void get_string(string&);

int main()
{
char choice;
bool ans;
int num_vow, num_cons;
string inp;

cout << "Please enter starting phrase\
";
getline(cin, inp);


 ans = true;
do {
choice = menu();

switch (choice){
case 'A':
case 'a':
num_vow=vowels(inp);
cout << "\
";
cout << "The number of vowels is : " << num_vow << "\
";
system ("pause");
case 'B':
case 'b':
num_cons=consonants(inp);
cout << "\
";
cout << "The number of consonants is : " << num_cons << "\
";
system ("pause");
case 'C':
case 'c':
num_vow=vowels(inp);
num_cons=consonants(inp);
cout << "\
";
cout << "The number of vowels is : " << num_vow << "\
";
cout << "The number of consonants is : " << num_cons << "\
";
system ("pause");
case 'D':
case 'd':
get_string(inp);
case 'E':
case 'e':
ans == false;
}
} while(ans == true);

return 0;
}
Thanks
    
CPUMotherboardGraphicsRAM
Intel Core i7 930 [4.33 Ghz] EVGA X58 SLI LE EVGA GTS 450 [SLI] 6 GB DDR 3 OCZ 1600 [1T-7-8-7-18 @ 1474] 
Hard DriveOptical DriveOSMonitor
Vertex 2 [120 GB] + 4x WD Green 2 TB [Raid1] Samsung BD-R [12x] Microsoft Windows 7 Pro [x64] [3] Samsung SyncMaster 2233sw 
KeyboardPowerCaseMouse
I-rocks Corsair HX650 [650W] Lian Li PC K62 Logi 
Mouse Pad
Metallica 
  hide details  
Reply
    
CPUMotherboardGraphicsRAM
Intel Core i7 930 [4.33 Ghz] EVGA X58 SLI LE EVGA GTS 450 [SLI] 6 GB DDR 3 OCZ 1600 [1T-7-8-7-18 @ 1474] 
Hard DriveOptical DriveOSMonitor
Vertex 2 [120 GB] + 4x WD Green 2 TB [Raid1] Samsung BD-R [12x] Microsoft Windows 7 Pro [x64] [3] Samsung SyncMaster 2233sw 
KeyboardPowerCaseMouse
I-rocks Corsair HX650 [650W] Lian Li PC K62 Logi 
Mouse Pad
Metallica 
  hide details  
Reply
post #2 of 3
You need to break after every switch case.

switch (choice) {

case '...':
do something;
do something;
break;

case '...':
do something;
do something;
break;
}
Wahoo!
(13 items)
 
  
CPUMotherboardGraphicsRAM
Xeon W3530 @1v stable EVGA SLI3 X58 EVGA GTX 470 6GB working ram 6GB dead sticks :(  
Hard DriveCoolingOSMonitor
4x1TB F3 Raid 5, Intel SSD Lots of Noctua fans. Windows 7 Ultimate. 2x Dell U2410f Monitors  
KeyboardPowerCaseMouse
Filco w/ cherry browns! Seasonic X-750 Lian-Li PC-B10 Original deathadder 
  hide details  
Reply
Wahoo!
(13 items)
 
  
CPUMotherboardGraphicsRAM
Xeon W3530 @1v stable EVGA SLI3 X58 EVGA GTX 470 6GB working ram 6GB dead sticks :(  
Hard DriveCoolingOSMonitor
4x1TB F3 Raid 5, Intel SSD Lots of Noctua fans. Windows 7 Ultimate. 2x Dell U2410f Monitors  
KeyboardPowerCaseMouse
Filco w/ cherry browns! Seasonic X-750 Lian-Li PC-B10 Original deathadder 
  hide details  
Reply
post #3 of 3
Since you are using a switch statement, and each statement depends on a letter entered, remember to put a "break;" without quotations as final code line before the next case, else it will execute code it shouldn't be executing.

Also on case E, you typed ans == false, from what I saw it should be ans = false, since you want it to exit when ans is false. Remember that == is for equal while = is for assignation.
First Build
(17 items)
 
  
CPUMotherboardGraphicsRAM
i7-2600k @4.5Ghz Gigabyte Z68X-UD7 B3 MSI 6950 G.Skill Sniper 2x4GB 
Hard DriveHard DriveHard DriveCooling
Crucial M4 64GB Samsung SpinPoint 250GB Hitachi 1TB Noctua NH-D14 
OSMonitorMonitorKeyboard
Windows Server 2012 Samsung S22B300 22' ViewSonic VA703B 17" CM Quickfire TK 
PowerCaseMouse
Corsair HX850 NZXT Phantom Mionix Naos 3200 
  hide details  
Reply
First Build
(17 items)
 
  
CPUMotherboardGraphicsRAM
i7-2600k @4.5Ghz Gigabyte Z68X-UD7 B3 MSI 6950 G.Skill Sniper 2x4GB 
Hard DriveHard DriveHard DriveCooling
Crucial M4 64GB Samsung SpinPoint 250GB Hitachi 1TB Noctua NH-D14 
OSMonitorMonitorKeyboard
Windows Server 2012 Samsung S22B300 22' ViewSonic VA703B 17" CM Quickfire TK 
PowerCaseMouse
Corsair HX850 NZXT Phantom Mionix Naos 3200 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming