[英]Why getline does not work in a switch, and cin does not take the data appropriately?
我需要了解特定產品適合的汽車型號和年份。 該程序將編寫一個包含文本的文件,該文件將是 html 的輪廓。 我需要將關鍵信息插入正確的位置,以便快速將產品添加到我們的網頁。 當我使用 switch 語句或 if 語句來分隔產品的類別時,輸入會變得混亂並且不允許我回答其中一個問題。 我不能只使用 cin 因為我需要取看起來像“2008 - 2009 - 2010 - 2011”的日期。
這是我到目前為止所得到的! 這個只是普通的cin
,我試過getline
你可以很快 c 我昨天開始,我很新,所以如果這是一個簡單的修復,請原諒我,但我找不到任何東西。
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
void proinfo();
int main()
{
//Sytem Information
system("TITLE This is a Beta of My Product Information Template Version 0.0.2");
//I'm using this as a welcome screen to inform users of new improvements on patches and other pertinent information to the users
cout << "Welcome To My Product Information Template Maker!!! \n It Is Still In Development" << endl;
cout << "\n\nworking features are:" << endl << "\t-None\n" << endl;
system("PAUSE");
system("CLS");
proinfo();
}
void proinfo()
{
//Declare Variables here
int loop(1), protype;
char make[256], model[256], year[256];
string getinput;
while(loop==1)
{
//This is the first menu the user sees where they have to choose what type
//of products users will be adding
system("CLS");
cout << "Welcome to My Product Information Template Version 0.0.0" << endl;
cout << "Please select the number of the product" << endl;
cout << "type you will be ading!\n" << endl;
cout << "1.Fe - Fe2 Moldings" << endl;
cout << "2.CF - CF2 Moldings" << endl;
cout << "What would you like to do? ";
cin >> protype;
if(protype==1)
{
//FE - FE2 Molding
system("cls");
cout << "You have selected" << endl;
cout << "Fe - Fe2 Moldings\n" << endl;
cout << "Please Enter the Make of the molding" << endl;
cin >> make;
cout << "Please Enter the Model of the molding" << endl;
cin >> model;
cout << "Please Enter the Years this molding fits" << endl;
cin >> year;
cout << "You have just created a template for a(n)" << year << " " << make << " " << model << endl;
cout << "Check My Documents For a file called Fe-Fe2template.txt" << endl;
//Asks to quit or restart
cout << "Would you like to make another tempalte?" << endl;
cin >> getinput;
if(getinput=="yes")
{
cout << "Great, Lets keep working!" << endl;
//End of If statement
}
system("PAUSE");
}
if(protype==2)
{
//CF - CF2 Molding
system("cls");
//Asks to quit or restart
cout << "Would you like to make another tempalte?" << endl;
cin >> getinput;
if(getinput=="yes")
{
cout << "Great, Lets keep working!" << endl;
//End of If statement
}
system("PAUSE");
//End of Protype Switch Statement
}
//End of while loop
}
//End of Proinfo()
}
將年份 [256] 更改為字符串年份;
更改cin>>年; getline(cin, year);
添加行 cin.ignore(); 在getline之前。
您的主要問題是流運算符 >> 將換行符留在流中,因此當您嘗試使用 getline 時,它會讀取一個空行。 ignore() 將咀嚼換行符並讓您閱讀您期望的字符串。
所以這應該會讓你上路。
cin.ignore();
cout << "Please Enter the Years this molding fits" << endl;
getline(cin, year);
您還有一些其他小問題,但您會解決的。 最糟糕的是忘記終止循環。
if(getinput=="yes")
{
cout << "Great, Lets keep working!" << endl;
//End of If statement
}
else
{
loop = 0;
continue;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.