I am trying to do case menu for my program. And I always getting error of cross initialization, I have never before saw this error. Maybe someone could explain me what is happening wrong with my code.
#include <iostream>
#include <cstdlib>
#include <string>
#include <sstream>
#include <stdlib.h>
#include <fstream>
using namespace std;
#define N_CARS 1
struct car{
string model;
int year;
double price;
bool available;
}cars [N_CARS];
void menu();
void mainMenu();
void writeToFile(ofstream &outputFile , const car& p)
{
outputFile << p.model << " "
<< p.year << " "
<< p.price << " "
<< p.available<<"\n";
}
int choice1 = 0;
int main(int argc, char** argv) {
menu();
return 0;
}
void menu() {
do {
mainMenu();
switch(choice1) {
case 1:
string mystr;
string mystr2;
string mystr3;
int n;
for (n=0; n<N_CARS; n++)
{
cout << "Enter title: ";
getline (cin,cars[n].model);
cout << "Enter year: ";
getline (cin,mystr);
stringstream(mystr) >> cars[n].year;
cout << "Enter price: ";
getline (cin,mystr2);
stringstream(mystr2) >> cars[n].price;
cout << "Choose availability: ";
getline (cin,mystr3);
stringstream(mystr3) >> cars[n].available;
}
ofstream outputFile;
outputFile.open("bla.txt", fstream::app);
for (n=0; n<N_CARS; n++)
writeToFile(outputFile, cars[n]);
outputFile.close();
break;
case 2:
break;
case 5:
break;
}
} while(choice1 != 5);
}
void mainMenu(void) {
cout << "Main Menu\n";
cout << "1 - Enter Car\n";
cout << "5 - Quit\n";
cout << "Please choose: ";
cin >> choice1;
}
Errors:
In function 'void menu()':
[Error] jump to case label [-fpermissive]
[Error] crosses initialization of 'std::ofstream outputFile'
[Error] crosses initialization of 'std::string mystr3'
[Error] crosses initialization of 'std::string mystr2'
[Error] crosses initialization of 'std::string mystr'