我无法在某个点(从用户那里)获得输入,即使没有错误弹出编译
I am not able to get input after a certain point ( from the user) even though no error pops wile compiling
该计划是一个小型医院管理计划。 它适用于C++并使用二进制文件和类。 这是一个简单的程序,它接受患者详细信息的输入并保存在二进制文件中,并通过搜索Regno来显示患者详细信息。
代码不会超出:cin>>A1。PI.age;并开始打印无休止的循环。
请回复我哪里出错了
这是代码:
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<string.h>
#include<stdlib.h>
class all
{ private:
struct address
{ int house;
char street[30];
char city[30];};
struct patient_info
{ char name[40];
address AD1;
int age;
int maritalstatus;
int regno;
char bldgrp[3];
char sex;
}PI;
int task;
protected:
void firstpinfo();
void showpinfo();
void enterpinfo();
public:
void tasks();
char ch;
int serial;
}A1;
class date :public all
{ private :
int day;
int month;
int year;
public:
void enterdate()
{cout<<"enter day of date";
cin>>day;
cout<<"enter month";
cin>>month;
cout<<"enter year";
cin>>year;
}
void showdate()
{ cout<<day<<"/"<<month<<"/"<<year;}
}D1;
//global variables
int count,attempt;
void main()
{ count=0;
cout<<" HOSPITAL MANAGEMENT SOFTWARE
";
D1.enterdate();
A1.tasks();
getch();
while(count==0)
{ A1.tasks();
cout<<"press 0 to continue and 1 to exit";
cin>> count;
}
getch();
}
void all::tasks()
{ attempt=0;
D1.showdate();
cout<<"select task"<<endl
<<"1.show patient details"<<endl
<<"2.enter details of a patient"<<endl
<<"3.exit prog"<<endl;
cin>>task;
switch(task)
{
case 1: {cout<<"enter regno to display"<<endl;
int search;
cin>>search;
fstream fon;
fon.open("hospital.dat",ios::in|ios::binary);
if(!fon)
{cout<<"error in opening"<<endl;
getch();
exit(0);
}
else
{fon.read((char*)&A1,sizeof(A1));
if(A1.PI.regno==search)
{cout<<"showing details";
A1.showpinfo();}
else
{cout<<"regno not found";}
fon.close();
}
break;}
case 2: {cout<<"adding a new patient";
A1.enterpinfo();
fstream fan;
fan.open("hospital.dat",ios::in|ios::binary);
if(fan)
{fan.write((char*)&A1,sizeof(A1));}
fan.close();
break;}
case 3: { cout<<"exiting...press any key";
getch();
exit(0);
break;
}
default: {cout<<"error... press anykey to try again";
getch();
A1.tasks();
};
}}//END OF TASKS
void all::showpinfo()
{cout<<"patient regnon"<<A1.PI.regno<<endl;
cout<<"patient namen"<<A1.PI.name<<endl;
cout<<"address of patientn"<<A1.PI.AD1.house<<" "<< PI.AD1.street<<" "<<PI.AD1.city<<endl;
cout<<"blood group"<<A1.PI.bldgrp<<endl;
cout<<"sex"<<A1.PI.sex<<endl;
cout<<"data extracted";
}
void all:: enterpinfo()
{
cout<<"enter unique registration number";
cin>>PI.regno;
cout<<"enter patient name"<<endl;
cin.getline(A1.PI.name,50);
cout<<"enter address( house, street, city)"<<endl;
cin>>A1.PI.AD1.house;
cin.getline(A1.PI.AD1.street,30);
cin.getline(A1.PI.AD1.city,30);
cout<<"enter age in years"<<endl;
cin>>A1.PI.age;
cout<<"enter 1 for married and 0 for otherwise"<<endl;
cin>>A1.PI.maritalstatus;
cout<<"enter blood group"<<endl;
cin>>A1.PI.bldgrp;
cout<<"enter M for male and F for female";
cin>>A1.PI.sex;
}
为什么会发生这种情况:发生这种情况是因为您混合了cin和cin.getline。 使用 CIN 输入值时,CIN 不仅会捕获该值,还会捕获换行符。因此,当我们输入 2 时,cin 实际上得到了字符串 "2"。然后,它将 2 提取到变量,使换行符卡在输入流中。然后,当 getline(( 去读取输入时,它看到 "" 已经在流中,并且数字我们必须输入一个空字符串!绝对不是本意。
旧解决方案:一个好的经验法则是在使用 cin 读取值后,从流中删除换行符。这可以使用以下内容来完成:
std::cin.ignore(32767, 'n'); // ignore up to 32767 characters until a n is removed
更好的解决方案:每当使用 std::getline(( 读取字符串时,请使用它
std::getline(std::cin >> std::ws, input); // ignore any leading whitespace characters
std::ws 是一个输入操纵器,它告诉 std::getline(( 忽略任何前导空格字符
来源 :LearnCPP网站 转到部分(使用 std::getline(( 输入文本(
希望这是有帮助的
#include<iostream>
#include<fstream>
#include<string>
#include<cstdlib>
不要使用 .h 扩展名
我们认为我们不再需要#include<conio.h>
而且并不简单
getline(cin, A1.PI.name)
比cin.getline(A1.PI.name,50)
然后调整其最大大小
A1.PI.name.resize(50);
相关文章:
- 我有三个 getline,但是一旦编译,输入就太多了
- 我无法在某个点(从用户那里)获得输入,即使没有错误弹出编译
- 直接输入编译错误 (C++)
- 编译一个自定义的tf操作,其中输入是5d张量
- 我无法编译谷歌测试,告诉链接器输入未使用(macOS)
- 将 ProcessEntry32.szExeFile 与用户输入的数据进行比较时,C++ _wcsicmp代码未编译
- variadic函数模板:基于n编译时值在运行时间自动n输入
- 创建一个程序,要求用户输入两个数字,并有三个可能的答案.代码未编译.数字(作为int)
- 有没有一种方法可以获得给定数量的输入,其中该数字是由c++中的编译时模板给定的
- 无法在Eclipse中编译带有输入和输出的C 程序
- 下面输入的代码未编译
- 使用 FFMPEG 将每个输入图像转换为 H264 编译时出错,该图像在 MevisLab 中运行的 Visual St
- 两个GCC编译了相同的输入,生成了两个不同的代码(第二个错误)
- 从C 调用的编译MATLAB函数的输入参数结构
- C++交叉编译不处理换行输入文本文件
- 扩展编译器以在编译时计算"complicated"函数(具有已知的输入值),超出"constexpr"的范围
- 为什么具有引用指针的输入的函数无法编译
- c++ 将函数输出重定向到编译的程序输入,反之亦然
- 使用标准输入流会导致编译失败
- c++输入编译错误