Random program crashing

I am having some random issues with my C++ program.
Apparently I can't delete some useless lines, or else the program crashes.
If I delete the lines where I declare variable flow1 (line 39) or when I assign it a value of 0 (line 45), the program crashes. If I do not delete it, it works fine.
The problem is that I am not using that variable flow1 anywhere else in my main.
It seems quite a bit random to me. Thank you for your time and patience.

EDIT:
InputInfo.txt is a simple text file that contains the following: "InputData.txt 10"
InputData.txt is a text file that contains 40 values typed randomly: "17 19 1 4 15 20 2 5 14 19 1 4 15 20 2 5 14 19 1 4 15 20 2 5 14 19 1 4 15 20 2 5 14 19 1 4 15 20 2 5"

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
#include <iostream>
#include <new>
#include <fstream>
#include <cstdio>
#include <string>
#include <istream>
#include <ios>
#include <iomanip>

using namespace std;

class WellSlice{
    double x,y,
           pressure,
           rate;  

public:
    void set_value (double, string);
    void print_values ();
};


void WellSlice::set_value (double value, string variable){ 
if (variable =="x1") {x = value;}
else if (variable =="y1"){y = value;}
else if (variable =="pressure1"){pressure = value;}
else if (variable =="rate1"){rate = value;}
}

void WellSlice::print_values (){ 
cout << x << ", " << y << ", " << pressure << ", " << rate;
}

int main(){
    WellSlice * Well;
    int i, n;
    double x1, y1;
    double pressure1;
    //double flow1;
    double rate1;
    ifstream InputInfo ("InputInfo.txt");
    char * TXTName;

    x1 = 0; y1 = 0;
    //flow1 = 0;
    pressure1 = 0;
    rate1 = 0;

    if (InputInfo.is_open()){
        while (!InputInfo.eof()){
        InputInfo >> TXTName;
        InputInfo >> i;
        }
    }

    ifstream InputData (TXTName);

    Well = new (nothrow) WellSlice[i]; 
    if(Well == NULL){                          
        cout << "\nError allocating memory\n";
        return 0;
    }

    if (InputData.is_open()){
        for (n=0; n<i; n++){                       
            InputData >> x1;
            InputData >> y1;
            InputData >> pressure1;
            InputData >> rate1;
            if (InputData.eof() == true) break;       .
            Well[n].set_value(x1, "x1");
            Well[n].set_value(y1, "y1");
            Well[n].set_value(pressure1, "pressure1");
            Well[n].set_value(rate1, "rate1");
        }
    }
    cout << "\nYou have entered: ";
    for (n=0; n<i; n++){
        cout << "\nWell slice number " << (n+1) << ":\n";
        Well[n].print_values();
    }
    delete[] Well;

    return 0;


}
Last edited on
My compiler gives me the following warning.
51: warning: ‘TXTName’ may be used uninitialized in this function


operator>> assumes that the char pointer that you pass to it is pointing to memory that is large enough to store the input string. TXTName has not been initialized so when operator>> starts writing data it's very likely to crash because TXTName probably doesn't point to a valid memory location.
Last edited on
Thank you for your answer.
I just edited in to this thread the contents of the two .txt files the program reads.

I think I understand what you mean.
Do you possibly have a solution for that problem?

Thanks.
Well, you could make TXTName a char array, or better yet a std::string.

Not sure why you are reading TXTName and i inside a loop. Can it contain multiple filenames that you want to read? The way you have it now will not do because the loop will run once to many times leaving TXTName and i with not so useful values. http://www.parashift.com/c++-faq/istream-and-eof.html
Thank you very much, Peter87, you are absolutely right.
@Dput
You forgot the null terminator.
Topic archived. No new replies allowed.