这是作业
所以我认为我不需要详细讨论我的项目,但我的程序的问题是,对于我的案例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;
}
所以这在我的程序中仍然相当早,但我希望修复这些问题,以便稍后我就不会有十亿错误。当我编译代码时,它要求我输入文件名(如我的记录函数),然后输入文件名。之后程序只会打印主要和无限次给出的选项。但更重要的是,当我编译它时,我的函数中的消息
“文件已被阅读,请选择其他选项”
未显示,因此我认为我的程序由于某种原因跳过了该程序。为什么这会导致循环,为什么它会跳过该print语句?
任何帮助将不胜感激!
答案 0 :(得分:0)
cout << "Enter the input file: ";
cin >> filea[10];
到
cout << "Enter the input file: ";
cin >> filea;