Reputation: 849
Here's my program so far:
int main()
{
char choice = 'D';
string inputString;
cout << "Please input a string." << endl;
getline(cin, inputString);
LetterCount letterCount(inputString);
while(choice != 'E')
{
cout << "Please choose from the following: " << endl
<< "A) Count the number of vowels in the string." << endl
<< "B) Count the number of consonants in the string." << endl
<< "C) Count both the vowels and consonants in the string." << endl
<< "D) Enter another string." << endl << "E) Exit the program." << endl;
cin >> choice;
if(choice == 'A' || choice == 'a')
{
cout << "There are " << letterCount.vowelCount() << " vowels in this string." << endl;
}
else if(choice == 'B' || choice == 'b')
{
cout << "There are " << letterCount.consonantCount() << " consonants in this string." << endl;
}
else if(choice == 'C' || choice == 'c')
{
cout << "There are " << letterCount.vowelCount() << " vowels and " << letterCount.consonantCount()
<< " consonants in this string, for a total of " << (letterCount.vowelCount() + letterCount.consonantCount())
<< " letters." << endl;
}
else if(choice == 'D' || choice == 'd')
{
cout << "Please type in another string." << endl;
getline(cin, inputString);
letterCount.setInputString(inputString);
}
else
{
choice = 'E';
}
}
}
I'm only including the main as it's the issue giver here, everything else functions properly.
The problem comes up when I use choice 'D' (input a new string.) as soon as enter is hit, the program returns right to the choice prompt and sets the inputString variable to blank (not the word blank, but nothing in it)
the first getline(cin, inputString) works perfectly fine, the second one is the issue giver...any suggestions?
Upvotes: 1
Views: 4077
Reputation: 2421
You're not quite using getline correctly. Try using a skeleton like this...
#include <iostream>
#include <iomanip>
#include <string>
using std::getline;
using std::string;
using std::cin;
using std::cout;
using std::endl;
const string instructions = "Please choose from the following:\nA) Count the number of vowels in the string.\nB) Count the number of consonants in the string.\nC) Count both the vowels and consonants in the string.\nD) Enter another string.\nE) Exit the program.\n";
int main()
{
// print instructions
cout << instructions;
// input text
string text;
bool processing = true;
// start loop
while (processing && getline(cin, text) && !text.empty())
{
// switch on input character
switch (text[0])
{
case 'A':
case 'a':
{
cout << "Doin A stuff" << endl;;
break;
}
case 'B':
case 'b':
{
cout << "Doin B stuff" << endl;;
break;
}
case 'C':
case 'c':
{
cout << "Doin C stuff" << endl;;
break;
}
case 'D':
case 'd':
{
cout << "Doin D stuff" << endl;;
break;
}
case 'E':
case 'e':
{
cout << "Exiting..." << endl;;
processing = false;
continue;
break;
}
default:
{
cout << "I have no idea what you entered" << endl;
processing = false;
continue;
break;
}
}
// print instructions for next loop
cout << endl << instructions;
}
return 0;
}
Upvotes: 0
Reputation: 5455
Wouldnt it be better for you to use a switch case, such as this one?
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
//Initialize
void newString();
void menu();
void switchCases(choice);
//Variables
string inputString;
char choice;
LetterCount letterCount(inputString);
int main(){
menu();
}
void newString(){
cout << "Please type in another string." << endl;
getline(cin, inputString);
letterCount.setInputString(inputString);
}
void menu(){
cout << "Please choose from the following: " << endl
<< "A) Count the number of vowels in the string." << endl
<< "B) Count the number of consonants in the string." << endl
<< "C) Count both the vowels and consonants in the string." << endl
<< "D) Enter another string." << endl << "E) Exit the program." << endl;
cin >> choice;
newString();
}
void switchCases(char choice){
switch (choice){
case 'A':
case 'a':{
cout << "There are " << letterCount.vowelCount() << " vowels in this string." << endl;
break;}
case 'B':
case 'b':{
cout << "There are " << letterCount.consonantCount() << " consonants in this string." << endl;
break;}
case 'C':
case 'c':{
cout << "There are " << letterCount.vowelCount() << " vowels and " << letterCount.consonantCount()
<< " consonants in this string, for a total of " << (letterCount.vowelCount() + letterCount.consonantCount())
<< " letters." << endl;
break;}
case 'Q':
case 'q':{
break;}
default:
case 'D':
case 'd':{
main();}
}
}
Upvotes: 2
Reputation: 6566
i think you have to add something like this:
std::cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n');
after your cin >> choice
The reason for this is, that the user actually entered 'D\n' but only 'D' fits into the choice variable, so the '\n' goes into the buffer of cin. when you call getline, getline sees that '\n' in the buffer and returns with nothing in it...
that call above will remove all '\n' which are in the cin buffer.
Its explained quite well here
another solution would be something like:
char newline;
cin >> choice;
cin.get(newline);
This will only remove a single '\n' from the buffer.. (since cin >> choice
added one \n
)
Upvotes: 2