即使在编译时没有错误弹出,我也无法从用户那里获得输入([重复]

问题描述 投票:1回答:2
[此程序是小型医院管理程序。它与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 regno\n"<<A1.PI.regno<<endl; cout<<"patient name\n"<<A1.PI.name<<endl; cout<<"address of patient\n"<<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; }

c++ subclass file-handling
2个回答
2
投票
这是因为您混合了cin和cin.getline。当您使用cin输入值时,cin不仅会捕获该值,还会捕获换行符。因此,当我们输入2时,cin实际上会得到字符串“ 2 \ n”。然后,将2提取为变量选择,使换行符停留在输入流中。然后,当getline()去读取输入时,它会看到“ \ n”已经在流中,并且我们必须输入一个空字符串!绝对不是想要的。

一个好的经验法则是,使用cin读取值后,从流中删除换行符。可以使用以下方法完成:

std::cin.ignore(32767, '\n'); // ignore up to 32767 characters until a \n is removed

我希望这能解决您的问题。

1
投票
#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);

© www.soinside.com 2019 - 2024. All rights reserved.