0

so my file won't rewind properly despite doing what my instructor told me to do. So i'm doing this.

inputFile.clear();
inputFile.seekg(0);

right before doing this.

while (inputFile >> name)
{
    if (sex == "M")
    {
        mCount++;
        mTotal = mTotal + score;
    }
    else if (sex == "F")
    {
        fCount++;
        fTotal = fTotal + score;
    }
    if (college == "CC")
    {
        ccCount++;
        ccTotal = ccTotal + score;
    }
    else if (college == "UN")
    {
        unCount++;
        unTotal = unTotal + score;
    }
    Count++;
    Total = Total + score;

}

but instead of doing what it should the file simply reads the last line instead of the entire .txt file and i don't know why.

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • Welcome to stackoverflow.com. Please take some time to read [the help pages](http://stackoverflow.com/help), take [the SO tour](http://stackoverflow.com/tour), read about [how to ask good questions](http://stackoverflow.com/help/how-to-ask), and please learn how to create a [Minimal, Complete, and Verifiable Example](http://stackoverflow.com/help/mcve). – Some programmer dude May 08 '18 at 03:01
  • What input do you give? Could you show us the rest of the code in action here? – Kostas May 08 '18 at 03:01
  • 1
    Have you checked the value of `Count` at the end? It appears your `if ... else..` statements could be overly restrictive giving the appearance of nothing being read. But, nobody can help you unless you help us help you with [**A Minimal, Complete, and Verifiable Example (MCVE)**](http://stackoverflow.com/help/mcve) – David C. Rankin May 08 '18 at 03:04
  • the input is this 'cout << "Input file name: "; getline(cin, inputFileName); // Open the input file. inputFile.open(inputFileName);' i know that part works because it is used earlier in the code and functions fine – user9473976 May 08 '18 at 03:05
  • 1
    By the way, you have a loop which read into `name`, but nowhere inside the loop do you use that `name` variable? You do use some totally unrelated variables, which you don't seem to be fetching from the file. – Some programmer dude May 08 '18 at 03:05
  • my count outputs 48, or 4 times the amount of inputs – user9473976 May 08 '18 at 03:08
  • the name variable is used in a different section of functional code. as long as the name variable exist the rest of the variables should as well if the user inputted them correctly – user9473976 May 08 '18 at 03:10
  • 1
    Other than saying you if statements are pointless because you don't read anything into any variable other than `name` I am not sure what we can do. – drescherjm May 08 '18 at 03:10
  • Outside of the loop, `name` is useless. It will only contain the value of the *last* name you read, which coincidentally seems to be your problem. Reading into `name` doesn't append or something, it *overwrites* the current contents of `name`. I suggest you take some time to learn how to use a debugger to step through your code line by line, then you can see the values of all variables and how they change (or *not* change) in the loop. – Some programmer dude May 08 '18 at 03:21
  • I also recommend you get [a few good books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282) to read, since you seem to have missed some basics about input and output using streams. – Some programmer dude May 08 '18 at 03:22

0 Answers0