Count++ not adding to initial count

Hello, I have spent hours on this project and am hoping for some help. I have been trying to get my counts to work but every time I run the program it outputs 0 for the count. for some reason my count is not being affected by the if statements. The if statements sort the files just don't have any effect on the counts. thanks for the help!

#include<iostream>
#include<fstream>
#include<string>
#include<iomanip>

using namespace std;

int main()
{
string media = "unsorted_media.txt";
string movies = "movies.txt";
string music = "music.txt";
string images = "images.txt";
string error = "error.txt";
string ext;

int countUM = 0, countM = 0, countU = 0, countI = 0, countE = 0;

string current;

ifstream inFile;

ofstream outMovies;
ofstream outMusic;
ofstream outImages;
ofstream outError;

inFile.open(media);
outMovies.open(movies);
outMusic.open(music);
outImages.open(images);
outError.open(error);


inFile >> media;

while (inFile)
{
inFile >> media;
countUM++;
}




while (inFile)
{

ext = media.substr(media.length() - 4);

if (ext == ".avi" || ext == ".mkv" || ext == ".mov" || ext ==
".mp4" || ext == ".wmv")
{
outMovies << media << endl;
countM++;


}


else if (ext == "flac" || ext == ".m4a" || ext == ".mp3" || ext
== ".wav" || ext == ".wma")
{
outMusic << media << endl;
countU++;

}

else if (ext == ".bmp" || ext == ".gif" || ext == ".jpg" || ext
== ".png" || ext == "tiff")
{
outImages << media << endl;
countI++;
}

else
{
outError << media << endl;
countE++;
}

inFile >> media;

}


cout << "\n\t\tYou have " << countUM << " files to sort.\n\n";

system("pause");

system("cls");




cout << countM;

system("pause");
return 0;

}
You have two while loops. The first while loop will read the whole file so there is nothing left for the second while loop to read.

If the file is not empty countUM should not be zero.
Last edited on
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
Hello halenrauch,

When I loaded up your program I discovered that I have no input file to use and have no idea what is in your input file.

The file or a decent sample would help. So that everyone is working with the same information.

When I loaded the program and had the proper indenting I noticed the the "if" "else if" statements are all inside the second while loop and the problem of an "else" without an "if" goes away.

Could really use the input file to make this work.

Andy

What's w/ all the unused counts? Should probably append those to their respective files too. Curious that you used the variable "media" to store the file name, and then later you're overwriting that same variable's contents with strings from that file.

You'd also need to guarantee each string token does not contain whitespace in order for a while (ifs >> media) { /*...*/ } solution to work. Otherwise you'd need a getline solution.
Topic archived. No new replies allowed.