输入后无限循环
Infinite loop after input
所以我一直在做作业,我偶然发现了一个问题。在我为我的程序输入部分日期后,它就进入了无限循环。这个想法是创建一个由结构条目组成的数组。我不确定问题似乎来自哪里,因为它只是开始不停地打印出“您的选择:”。这是我的代码:
#include <iostream>
using namespace std;
struct property {
int num;
char nBrok[50];
char type[10];
char adress[50];
char outlook[20];
double price;
double size;
int nRooms;
int floor;
char status[2];
};
int menu() {
int ch;
cout<<"Choose an option from 1-5."<<endl;
cout<<"1. Add property."<<endl;
cout<<"2."<<endl;
cout<<"3."<<endl;
cout<<"4."<<endl;
cout<<"5. Exit"<<endl;
do {
cout << "\nYour Choice: ";
cin>>ch;
} while (ch<1 || ch>5);
return ch;
}
void addProp(property bDanni[]) {
int count;
cout<<"1. Add property."<<endl;
cout<<"How many real estate properties would you like to add?"<<endl;
cin>>count;
for(int i=0; i < count; i++)
{
cin.ignore();
cout<<"Enter the entry number of the property: "; cin>>bDanni[i].num;
cout<<"Enter the name of the property's broker: "; cin.getline(bDanni[i].nBrok, 50);
cin.ignore();
cout<<"Enter the type of the property: "; cin.getline(bDanni[i].type, 10);
cin.ignore();
cout<<"Enter the adress of the property: "; cin.getline(bDanni[i].adress, 50);
cin.ignore();
cout<<"Enter the outlook of the property: "; cin.getline(bDanni[i].outlook, 20);
cin.ignore();
cout<<"Enter the price of the property: "; cin>>bDanni[i].price;
cout<<"Enter the size of the property: "; cin>>bDanni[i].size;
cout<<"Enter the number of rooms of the property: "; cin>>bDanni[i].nRooms;
cout<<"Enter the floor of the property: "; cin>>bDanni[i].floor;
cout<<"Enter the status the property: "; cin.getline(bDanni[i].status, 2);
cin.ignore();
}
}
int main() {
property bDanni[100];
int choice;
do {
choice = menu();
switch (choice) {
case 1: addProp(bDanni);break;
}
}while (choice !=5);
return 0;
}
我是个笨蛋,清除缓冲区应该发生在 getline 之前,使用它是因为 cin 跳过了间隔。很抱歉浪费了大家的时间。
在使用cin后准备使用getline时使用cin.ignore()。因为cin在缓冲区中留下了空字符。
所以我一直在做作业,我偶然发现了一个问题。在我为我的程序输入部分日期后,它就进入了无限循环。这个想法是创建一个由结构条目组成的数组。我不确定问题似乎来自哪里,因为它只是开始不停地打印出“您的选择:”。这是我的代码:
#include <iostream>
using namespace std;
struct property {
int num;
char nBrok[50];
char type[10];
char adress[50];
char outlook[20];
double price;
double size;
int nRooms;
int floor;
char status[2];
};
int menu() {
int ch;
cout<<"Choose an option from 1-5."<<endl;
cout<<"1. Add property."<<endl;
cout<<"2."<<endl;
cout<<"3."<<endl;
cout<<"4."<<endl;
cout<<"5. Exit"<<endl;
do {
cout << "\nYour Choice: ";
cin>>ch;
} while (ch<1 || ch>5);
return ch;
}
void addProp(property bDanni[]) {
int count;
cout<<"1. Add property."<<endl;
cout<<"How many real estate properties would you like to add?"<<endl;
cin>>count;
for(int i=0; i < count; i++)
{
cin.ignore();
cout<<"Enter the entry number of the property: "; cin>>bDanni[i].num;
cout<<"Enter the name of the property's broker: "; cin.getline(bDanni[i].nBrok, 50);
cin.ignore();
cout<<"Enter the type of the property: "; cin.getline(bDanni[i].type, 10);
cin.ignore();
cout<<"Enter the adress of the property: "; cin.getline(bDanni[i].adress, 50);
cin.ignore();
cout<<"Enter the outlook of the property: "; cin.getline(bDanni[i].outlook, 20);
cin.ignore();
cout<<"Enter the price of the property: "; cin>>bDanni[i].price;
cout<<"Enter the size of the property: "; cin>>bDanni[i].size;
cout<<"Enter the number of rooms of the property: "; cin>>bDanni[i].nRooms;
cout<<"Enter the floor of the property: "; cin>>bDanni[i].floor;
cout<<"Enter the status the property: "; cin.getline(bDanni[i].status, 2);
cin.ignore();
}
}
int main() {
property bDanni[100];
int choice;
do {
choice = menu();
switch (choice) {
case 1: addProp(bDanni);break;
}
}while (choice !=5);
return 0;
}
我是个笨蛋,清除缓冲区应该发生在 getline 之前,使用它是因为 cin 跳过了间隔。很抱歉浪费了大家的时间。
在使用cin后准备使用getline时使用cin.ignore()。因为cin在缓冲区中留下了空字符。