getline (cin, variable) ne voulant pas fonctionner correctement dans c ++?
Question
Voici mon programme à ce jour:
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';
}
}
}
Je ne compris le principal car il est le donateur de problème ici, tout fonctionne bien d'autre.
Le problème vient quand j'utiliser le choix « D » (entrée d'une nouvelle chaîne.) Dès que la touche enter est touché, le programme retourne droit à l'invite de choix et définit la variable inputString à blanc (pas le mot vide, mais rien dedans)
la première getline (cin, inputString) fonctionne parfaitement bien, le second est le donneur d'émission ... des suggestions?
La solution
je pense que vous devez ajouter quelque chose comme ceci:
std::cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n');
après votre cin >> choice
La raison est que l'utilisateur est entré effectivement « D \ n », mais seulement « D » s'inscrit dans la variable de choix, de sorte que le « \ n » va dans le tampon de cin. lorsque vous appelez getline, getline voit que « \ n » dans la mémoire tampon et revient avec rien dedans ...
cet appel sera au-dessus de supprimer tout « \ n », qui sont dans le tampon de cin.
Une autre solution serait quelque chose comme:
char newline;
cin >> choice;
cin.get(newline);
Cela ne supprime un seul '\ n' du tampon .. (depuis cin >> choice
ajouté un \n
)
Autres conseils
Ne serait-il préférable pour vous d'utiliser un boîtier de commutation, comme celui-ci?
#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();}
}
}
Vous n'êtes pas tout à fait à l'aide getline correctement. Essayez d'utiliser un squelette comme ça ...
#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;
}