对程序使用 switch 语句,导致选项的无限循环

Using a switch statement for a program, causing an infinite loop for options

本文关键字:选项 无限循环 语句 程序 switch      更新时间:2023-10-16

这是用于家庭作业的

所以我认为我不需要详细介绍我的项目,但我的程序的问题在于,对于我的情况 1,我需要程序只读取一个文件并将该文件的内容存储在结构数组中。所以这是我的程序,我将在下面进一步详细介绍

#include <iostream>
#include<fstream>
using namespace std;
char filea[10]; //Global variables going to be used for the input file
struct cars{ //Like the movie 
int year;
char make[10];
char model[10];
float price;
bool ava;
};
void record(cars cars2[]);
int main(void) {
int option = 0;
cars cars2[10]; //Like the movie -> cars2 will be used for the functions
do {
cout << endl; //Extra space to make it look organized
cout << "1. Record the File" << endl;
cout << "2. Show Car Information" << endl;
cout << "3. Transfer Information into Another File" << endl;
cout << "4. List the Cars Based on Price(Low-High)" << endl;
cout << "5. Total Price After Entering Days" << endl;
cout << "6. Choice of Car(enter index) and Price for Car" << endl;
cout << "7. Close the Program" << endl;
cout << "Choose an option: ";
cin >> option;
switch (option) {
case 1:
record(cars2);
break;
case 2:
break;
}
}
while(option != 7); //Going to be used to terminate the program 
return 0;
}
void record(cars cars2[10]) { //Open the file then store everything in an 
array of structs
int x;
ifstream temp; //Gonna be used as a temporary file to store stuff in
cout << "Enter the input file: ";
cin >> filea[10];
temp.open(filea); //This opens the file for reading
for(x = 0; x < 5; x++){
temp >> cars2[x].year >> cars2[x].make >> cars2[x].model >> cars2[x].price >> cars2[x].ava;
}
cout << "File has been read, please choose another option" << endl;
}

所以这在我的程序中还处于相当早的位置,但我想解决这些问题,这样我以后就不会有十亿个错误。当我编译代码时,它会要求我输入文件名(如在我的记录函数中),然后我输入文件名。之后,程序只是继续打印出主要和无限次给出的选项。但更重要的是,当我编译它时,我的函数中的消息

"文件已读取,请选择其他选项"

没有显示,所以我认为我的程序出于某种原因跳过了它。为什么这会导致循环,为什么它会跳过该打印语句?

任何帮助将不胜感激!

cout << "Enter the input file: ";
cin >> filea[10];

cout << "Enter the input file: ";
cin >> filea;