0

This is for homework

So I don't think I need to go into the specifics about my project but the issue with my program is that for my case 1, I need the program to just read a file and store the contents of that file in an array of structs. So here's my program and I'll go into further detail below

#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;
}

So this is still fairly early in my program but I want get these issues fixed so I just don't have a billion errors later on. When I compile the code it asks me for an input file name(as in my record function) so then I enter the file name. Afterwards the program just goes on the prints out the options given in the main and infinite number of times. But more importantly, when I do compile it, the message in my function

"File has been read, please choose another option"

isn't displayed so I figure that my program skips over that for some reason. Why is this causing a loop and why is it skipping over that print statement?

Any help would be appreciated!

chrisHG
  • 80
  • 1
  • 2
  • 18
  • You’re reading just one character into `filea`. Check that your `open`s succeed to help catch things like this. – Davis Herring Feb 03 '18 at 04:35
  • @DavisHerring I declared filea as a char as a global var, so wouldn't the program expect a char array instead of an int? – chrisHG Feb 03 '18 at 04:36
  • No, a computer does exactly what you tell it to do, instead of what you want it to do. `filea[10]` is a single `char`. It is also undefined behavior, because `filea[10]` does not exist, since `filea` is only ten characters long. It is only blind luck that the program continues to run, instead of crashing in a firely explosion. – Sam Varshavchik Feb 03 '18 at 04:39
  • That’s not the duplicate part. You read one character, as I said, and _then_ fail forever trying to read an integer. – Davis Herring Feb 03 '18 at 04:39

1 Answers1

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

to

 cout << "Enter the input file: ";
 cin >> filea;
chrisHG
  • 80
  • 1
  • 2
  • 18