gpt4 book ai didi

C++接触卡,重新运行程序麻烦

转载 作者:行者123 更新时间:2023-11-28 05:43:06 24 4
gpt4 key购买 nike

对于我的任务,我必须制作一张包含所需信息的客户卡片。我能够很好地运行程序,但是当我用“你想再次运行吗?(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;


}

最佳答案

您正在混合使用 cin >>getline(cin,。因此当您想要换行时,换行符仍然卡在 cin 中第二次阅读第一个问题的答案。

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

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

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

关于C++接触卡,重新运行程序麻烦,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36726443/

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