【问题标题】:C++ contact card, re-running program troubleC++接触卡,重新运行程序麻烦
【发布时间】:2016-04-19 18:19:39
【问题描述】:

对于我的任务,我必须制作一张包含一些所需信息的客户卡。我能够很好地运行该程序,但是当我用“你想再次运行吗?(Y/N)”重新运行它时,前两个问题出现在同一行,它弄乱了程序。谁能帮我解决这个问题?

#include <iostream>
#include <cstdlib>
#include <iomanip>
int main()
{

char answer;


system("CLS");
cout << "*********************************************" << endl;
cout << "*********************************************" << endl;
cout << "***            W E L C O M E !            ***" << endl;
cout << "*** In this program you will be creating  ***" << endl;
cout << "***      a Customer Contact card!         ***" << endl;
cout << "*********************************************" << endl;
cout << "*********************************************" << endl;
system("pause");

do

{

string name;
string city;
string address;
string state;
string phone_number;
string zip;




system("CLS");

cout << endl;
cout << "Enter the name of your contact : ";
getline(cin,name);
cout << "Enter your contact's phone number : ";
getline(cin,phone_number);
cout << "Enter your contact's address : ";
getline(cin,address);
cout << "Enter the city your contact lives in : ";
getline(cin,city);
cout << "Enter the state your contact lives in (Enter the abbreviation)  : ";
getline(cin,state);
cout << "Enter your contact's zip code : ";
cin >> zip;
system("pause");

system("CLS");

cout << "*********************************************" << endl;
cout << "***                                       ***" << endl;
cout << "*********************************************" << endl;
cout << "*** " << name <<  setw(41- name.length()) << "***" << endl;
cout << "*** " << address << setw(41- address.length()) << "***" << endl;
cout << "*** " << city << " " << state << " , " << zip << setw(30- city.length()) << "***" << endl;
cout << "*** " << state << setw(41- state.length()) << "***" << endl;
cout << "*** ";
cout<<"(";
for(int i = 0; i < 3; i++) {
cout << phone_number[i];
}
cout << ")";
for(int i = 3; i < 6; i++) {
cout << phone_number[i];
}
cout << "-";
for(int i = 6; i < 10; i++) {
cout << phone_number[i];
}
cout << setw(38- phone_number.length()) << "***" << endl;


cout << "*** " << zip << setw(41- zip.length()) << "***" << endl;
cout << "*********************************************" << endl;
cout << "*********************************************" << endl;
cout << endl;
cout << "Do you want to create another contact card? (Y/N)" << endl;
cin >> answer;

}

while (answer == 'Y' || answer == 'y');


return 0;


}

【问题讨论】:

标签: c++


【解决方案1】:

您正在混合cin &gt;&gt;getline(cin,。所以当你想在第二次运行时阅读第一个问题的答案时,换行符仍然停留在cin

坚持一个或另一个,这种令人困惑的行为不应该出现。

this answer 中所述:您还可以在cin &gt;&gt; answer 之后添加以下内容以清除cin 直到并包括换行符:

cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n');

【讨论】:

  • 我尝试将所有 cin 转换为 getline(cin,但 getline(cin, answer) 不起作用。
  • 没错,您必须更改answer 的类型。也许我刚刚在上次编辑中引用的ignore 替代方案在这一点上会更容易。
  • 你能说明在我的情况下它会如何写吗?抱歉,我对这些东西非常陌生,而且这是一门初学者课程,所以我还是很糟糕。
  • 只需按照上面写的方式添加行。您可能还需要在顶部添加#include &lt;limits&gt;
猜你喜欢
  • 2023-04-01
  • 1970-01-01
  • 2013-06-15
  • 1970-01-01
  • 2017-02-23
  • 1970-01-01
  • 2011-05-24
  • 2014-02-03
  • 2012-09-11
相关资源
最近更新 更多