#include <iostream>
#include <string.h>
using namespace std;
int main()
{
while (1)
{
char name1[100];
char adrs1[100];
char rsn1[100];
char XXXXX[100];
cout << "input personal information" << '\n';
cout << "patient 1" << '\n';
cout << "input the name of the patient" << '\n';
cin.getline (name1,100);
cout << "input the address of the patient" << '\n';
cin.getline (adrs1,100);
cout << "input the reason" << '\n';
cin.getline (rsn1,100);
cout << "input the name of the patient" << '\n';
cout << "if you want to exit, input exit" << '\n';
cin.getline (XXXXX,100);
if (XXXXX==name1)
cout << adrs1[100] << rsn1[100] << '\n';
else (XXXXX=="exit");
break;
return 0;
}
}
这是我的程序,编译没问题。但是当我启动程序时,它不打印任何rsn或adrs,它就结束了。 我希望它在读取名称时打印rsn和adrs。 请帮帮我
答案 0 :(得分:1)
您的计划中存在相当多的错误。
此时,我真的建议您通过逐步完成代码来获取一本书或尝试调试代码。在你学习的这个阶段,你比这个网站更有帮助,
答案 1 :(得分:0)
要完成上述答案,正确的程序将是:
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
while (1)
{
char name1[100];
char adrs1[100];
char rsn1[100];
char XXXXX[100];
cout << "input personal information" << '\n';
cout << "patient 1" << '\n';
cout << "input the name of the patient" << '\n';
cin.getline (name1,100);
cout << "input the address of the patient" << '\n';
cin.getline (adrs1,100);
cout << "input the reason" << '\n';
cin.getline (rsn1,100);
cout << "input the name of the patient" << '\n';
cout << "if you want to exit, input exit" << '\n';
cin.getline (XXXXX,100);
if (strcmp(XXXXX,name1) == 0)
cout << adrs1 << rsn1 << '\n';
else /*(XXXXX=="exit");*/
break;
//return 0;
}
}
答案 2 :(得分:0)
name1
变量,可以使用char name1[100] = {};
您无法直接比较if (XXXXX==name1)
,使用strncmp
功能可以使用相同的if (!strncmp(XXXXX,name1,100))
cout << adrs1 << rsn1 << '\n';
else if (!strncmp(XXXXX,"exit",100))
break;
。我更喜欢字符串类而不是char指针。使用以下内容:
{{1}}