Hello halenrauch,
PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/
Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.
First I will point that you open a file for input, but never check that it is open. If an input file is not found the stream does not open. Yory program will continue, but nothing will be read.
This is a piece of code that should an open for input:
1 2 3 4
|
if (!inFile)
std::cout << "\n Your error message" << std::endl;
std::this_thread::sleep_for(std::chrono::seconds(5)); // Requires header files "chrono" and "thread"
exit(1);
|
The second line is a pause so that the message can be read before the console window closes. At least it gives you a chance to read the message before the program ends. The "exit(1)" will exit the program at that point. Any number greater than zero denotes an error or non normal termination of the program. Say that you use more than one "exit" the number can help you track down where the problem is.
AS Peter87 pointed out the first while loop has read through the entire file and left the file pointer at the end. You could either close and reopen the file to set the file pointer to the beginning or use
infile.seekg(0, inFile.beg);
see
http://www.cplusplus.com/reference/istream/basic_istream/seekg/ for more information.
In your first while loop by the time "inFile" goes bad or fails you will have added 1 extar to "countUM" before the while loop fails. This is because "inFile' will fail when you try to read past end of file, but since this is inside the while loop yo will add 1 extra to "countUM" before the while loop fails. The more accepted way of doing this is:
1 2
|
while (inFile >> media)
countUM++;
|
This way when you read past end of file the "inFile" stream will fail and then the while loop will fail and the "countUM" will be exactly what you have read.
After this first while loop either close and reopen the input file or use the "seekg()" to reposition the file pointer.
The second while loop is based on "inFile" which is in a failed state at this point, so the second while loop will never be entered. You may want to consider taking the contents of the second while loop, which never has any input from the input file, and put it in the first while loop where it would do some good. In the second while loop "media" will only contain whatever was read last from the first while loop.
After that the "else if" statements have no "if" statement to start them. This will be a compiler error. You do have an "if" statement inside the second while loop, but since it is between the {}s of the while loop it is local to that while loop and ends when the whilw loop ends.
I could be a bit off here because your code is hard to read with out code tags and proper indenting. I will have to load up the code to check it out better.
Hope that helps,
Andy