Reputation: 11
I am working on an assignment for class. I need to make a program that uses a menu to count vowels and consonants. In the menu I need to have an option D "Please enter a new string". However when I input d, the cout string outputs, then the whole menu, with no chance to input a new string. Everything else in the program seems to work fine besides this.
#include<iostream>
#include <iomanip>
#include <ctype.h>
using namespace std;
int countVowel(char*);
int countConst(char*);
int main()
{
const int SIZE = 101;
char input[SIZE];
int vow, con;
char choice;
cout << "Please enter a sentence. (100 characters max.)" << endl;
cin.getline(input, SIZE);
do
{
cout << "A) Count the number of vowels in the string" << endl;
cout << "B) Count the number of consonants in the string" << endl;
cout << "C) Count both the vowels and consonants in the string" << endl;
cout << "D) Enter another string" << endl;
cout << "E) Exit the program" << endl;
cout << "Please select an option" << endl;
cin >> choice;
choice = tolower(choice);
switch (choice)
{
case 'a':
vow = countVowel(input);
cout << "Number of vowels: " << vow << endl;
break;
case 'b':
con = countConst(input);
cout << "Number of consonants: " << con << endl;
break;
case 'c':
vow = countVowel(input);
con = countConst(input);
cout << "Number of vowels: " << vow << endl;
cout << "Number of consonants: " << con << endl;
break;
case 'd':
cout << "Please enter a sentence. (100 characters max.)" << endl;
cin.getline(input, SIZE);
cout << endl;
break;
case 'e':
break;
default:
cout << "Invalid input!" << endl;
break;
}
} while (choice != 'e');
return 0;
}
int countVowel(char *str)
{
int count = 0;
for (int i = 0; str[i] != '\0'; i++) {
if (str[i] == 'a' || str[i] == 'e' || str[i] == 'i' ||
str[i] == 'o' || str[i] == 'u' || str[i] == 'A' ||
str[i] == 'E' || str[i] == 'I' || str[i] == 'O' ||
str[i] == 'U')
{
count++;
}
}
return count;
}
int countConst(char *str)
{
int count = 0;
for (int i = 0; str[i] != '\0'; i++) {
if (str[i] == 'b' || str[i] == 'c' || str[i] == 'd' ||
str[i] == 'f' || str[i] == 'g' || str[i] == 'h' ||
str[i] == 'j' || str[i] == 'k' || str[i] == 'l' ||
str[i] == 'm' || str[i] == 'n' || str[i] == 'p' ||
str[i] == 'q' || str[i] == 'r' || str[i] == 's' ||
str[i] == 't' || str[i] == 'v' || str[i] == 'w' ||
str[i] == 'x' || str[i] == 'y' || str[i] == 'z' ||
str[i] == 'B' || str[i] == 'C' || str[i] == 'D' ||
str[i] == 'F' || str[i] == 'G' || str[i] == 'H' ||
str[i] == 'J' || str[i] == 'K' || str[i] == 'L' ||
str[i] == 'M' || str[i] == 'N' || str[i] == 'P' ||
str[i] == 'Q' || str[i] == 'R' || str[i] == 'S' ||
str[i] == 'T' || str[i] == 'V' || str[i] == 'W' ||
str[i] == 'X' || str[i] == 'Y' || str[i] == 'Z')
{
count++;
}
}
return count;
}
Upvotes: 1
Views: 82
Reputation: 41
The problem with your code is that, when you press 'd',the program not only runs case d, however it also runs the code to enter choice as it is in the same do while loop.Therefore, you need to skip the choice input. For that what if have done is that initialized variable m as a moderator. When 'd' is pressed, m is incremented and the if statement becomes false, therefore taking input only the string
if(m%2==0)
{cout << "A) Count the number of vowels in the string" << endl;
cout << "B) Count the number of consonants in the string" << endl;
cout << "C) Count both the vowels and consonants in the string" << endl;
cout << "D) Enter another string" << endl;
cout << "E) Exit the program" << endl;
cout << "Please select an option" << endl;
cin>>choice;
choice = tolower(choice);}
for the switch case 'd'
case 'd':
cout << "Please enter a sentence. (100 characters max.)" << endl;
m++;
cin.getline(input,SIZE);
cout << endl;
break;
Replacing getline with ignore doesn't solve the problem.
Upvotes: 1