Reading numbers from a txt file

I am trying to create a function which:

1) Asks for a filename from the user
2) if it exists it takes in the file and prints out the contents of it
3) If the file does not exist it would use a default configuration

Here is what I have written


When it enters the if loop it just freezes does nothing and then the programme terminates. However when it enters the else it works fine.

When I run this it compiles however it does not function as intended. I would appreciate any help.
Last edited on
What does your config file look like?
Hello John Davis,

I loaded up your program and ran it.

What I found:

Your problem starts on line 13 when you define "v". At this point it is empty, i.e., it has no size. Therefor on line 26 v[i] is trying to access an element that does not exist.

One fix I had to try is std::vector<int> v(z.size()). The () after "v" will define a vector with the number of elements found in the (). And it did initialize all elements to zero for me. This will allow the for loop at line 25 to work properly.

On line 17 you enter a file name. After this, if you know about functions, this would be be a good place to cal a function to check the extension of the file name or if it even exists. Another thing you can do is put all the letters of what was entered into lower case or upper case letters, (the header file "cctype" will have the functions "std::tolower()" and "std::toupper()"), for this.

It is not a very good idea to leave it up to a user to enter the file name correctly with out doing something to check it.

Line 20. if you are compiling your programs to the C++11 standards the ".cstr()" is not needed a "std::string" will work fine.

Line 19 can be eliminated by writing line 17 as std::ifstream infile(filename);

Line 22. the if statement can be shortened to if (!infile).

The for loop at line 25 can be eliminated by saying v = z;. Even if "v" is defined as an empty vector with no size this works.

I have not tried the else part yet, but the code looks OK for now. The only thing I would likely change is "push_back" to "emplace_back" to keep up with the newer standards.

The return statement should be outside the else block.

When printing out the vector you might like this method:
std::cout << v[i] << (i + 1 < v.size() ? ", " : "\n");

Here is a tip: if statements, for loops, while loops that have only one line do not need the {}s. Indentation of the line, although not necessary, shows that it belongs to the previous line.

Last point. Variable names like "v" and "z" may mean something to you now, but how about in six months. C and C++ allow up to 256 characters for a variable name, so create a name the describes what the variable is or does. It does make your code easier to read and follow both for you and others. After that writing a variable with camel-Case is most often used. An example: "filename" would be "fileName" or "infile" would be "inFile". Again when you get use to seeing camel-Case you will find it easier to read.

Hope that helps,

Andy
Topic archived. No new replies allowed.