gpt4 book ai didi

c++ - 为什么在一个switch中getline不行,cin取数据不合适?

转载 作者:太空狗 更新时间:2023-10-29 20:47:20 26 4
gpt4 key购买 nike

我需要输入特定产品适用的汽车的品牌型号和年份。该程序将编写一个包含文本的文件,该文件将成为 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;
}

关于c++ - 为什么在一个switch中getline不行,cin取数据不合适?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5958429/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com