I started learning C++ 2 days ago so sry if its messy.
As soon as i try to read from a txt file (Line 59)
the error: "crosses initialization of 'std::ifstream inFile'" occurs.
I commented the error messages to the according lines.
I know its alot of code but i cant figure it out myself.
System:
Win7 64bit, IDE: Code::Blocks 13.12
This "programm" will be able to save lists of values as txt files and load them back in.
After that it will choose and print one random value from the designated list to the console.
So basicly an advanced "roll the dice" thing.
# include <iostream>
# include <fstream>
# include <conio.h>
# include <string>
# include <stdio.h>
# include <cstdlib>
# include <time.h>
usingnamespace std;
void pauseC(); // for easier pausing
void dbTest();
int main() {
bool on; // to turn the whole thing off
char choice;
string tmpSt[100]; // the temporary storage
string tch; // all sorts of temporary choices
int tmpStPoint= 0; // keeps track of how many strings has been added to the array
int showCounter= 0; // to go through the array
int runPoint; // random array index
on= true;
while(on) {
system("cls");
cout << "Main menu" << endl
<< "***********************" << endl
<< "0 tempAdd" << endl // adds something to the array
<< "1 load" <<endl // will push lines from a text file to the array
<< "2 save" <<endl // will write the content of the array to singel lines in a txt file
<< "3 clearTemp" <<endl // sets all previously set strings in the array to ""
<< "4 show" <<endl // prints the content off the array to the console
<< "5 run" <<endl // prints a random entry in the array to the console
<< "6 close" << endl
<< "***********************" << endl;
cin >> choice;
switch (choice) {
case'0':
tch= "";
cout << "type something" << endl;
cin >> tch;
tmpSt[tmpStPoint]= tch;
tmpStPoint++;
pauseC();
break;
case'1':
// HERE IS WHERE THE PROBLEM OCCURS
// i didnt do anything with the array yet to get the concept running first
ifstream inFile; // here it says "crosses initialization of 'std::ifstream inFile'"
inFile.open("numbers.txt");
int x, y;
inFile >> x >> y;
cout << x << " " << y << endl;
break;
case'3': // here it says "jump to case label [-fpermissive]"
showCounter= 0;
while(showCounter < tmpStPoint) {
tmpSt[showCounter]= ""; //clears the array
showCounter++;
}
tmpStPoint= 0;
break;
case'4': // here it says "jump to case label[-fpermissive]"
showCounter= 0;
cout << "arr: ";
while(showCounter < tmpStPoint) {
cout << tmpSt[showCounter] << ", ";
showCounter++;
}
cout << "\n"; //prints the array
cout << "end" << endl;
pauseC();
break;
case'5': // here it says "jump to case label[-fpermissive]"
srand(time(NULL));
runPoint= rand() % tmpStPoint;
cout << "******************\n";
cout << "random: " << runPoint << endl; // prints random array entry
cout << tmpSt[runPoint];
cout << "******************\n";
pauseC();
break;
case'6': // here it says "jump to case label[-fpermissive]"
on= false;
break;
default: // here it says "jump to case label[-fpermissive]"
cout << "Invalid Input!" << endl;
break;
}
}
}
void pauseC() {
cout << "press enter...\n";
cin.sync();
cin.get();
}
void dbTest() {
cout << "**DEBUG**\n";
}
case'1':
{ //enclose it with braces
ifstream inFile; // trying to create a variable
inFile.open("numbers.txt");
int x, y;
inFile >> x >> y;
cout << x << " " << y << endl;
break;
} //<--