Can't find the problem. crosses initialization of 'std::ifstream inFile'

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# include <iostream>
# include <fstream>
# include <conio.h>
# include <string>
# include <stdio.h>
# include <cstdlib>
# include <time.h>

using namespace 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";
}
Last edited on
move line 59 before the switch. Also, remember to close the file.
1
2
3
4
5
6
7
8
9
10
11
            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;
} //<-- 
It worked thanks alot guys :))))))))
Topic archived. No new replies allowed.