help! many problems

This is a discussion on help! many problems within the C++ Programming forums, part of the General Programming Boards category; Code: #include <iostream> #include <string> #include <iomanip> #include <fstream> #include <windows.h> using namespace std; int main() { int counter; //While ...

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    68

    help! many problems

    Code:
    #include <iostream>
    #include <string>
    #include <iomanip>
    #include <fstream>
    #include <windows.h>
    
    using namespace std;
    
    int main()
    {
        
        int counter; //While statement counter
        int numberOfTests;
        int studentTestTotal;
        int testScore; //Test score from file
        float studentAverage;
        float classAverage;
        string studentName; //Student Name
        string fileName;
        
        ifstream inFile;
        
        cout << "Please enter the filename to retrieve scores from: ";
        getline(cin,fileName);
        
        inFile.open(fileName.c_str());
        
        if (!inFile)
        {
             cout << "File failed to open";
        }    
    
        inFile >> numberOfTests;     
        
        while (!inFile.eof())
        {
             inFile >> studentName;
             cout << studentName;
             studentTestTotal = 0;
             studentAverage = 0;
             
             while (counter <= numberOfTests)
             {
                   inFile >> testScore;
                   cout << testScore;
                   studentTestTotal = studentTestTotal + testScore;
                   counter++;
             }
             
             cout << endl;
             
                   
        }
        
        inFile.close();
        system("pause");
        return 0;
              
    }
    This is what i have so far. Basically I need it to read in names and numbers from a file and output them. Then it needs to calculate the average for each person's score and give them a letter grade. Finally it needs to take all of the averages and calculate a class average.

    1) I do not reset the counter at all... Yet it compiles and works perfectly. If i reset the counter it doesn't.... WHY!?

    2) when i use inFile to input the names and scores... it puts them on a separate line. How can i get them to go on one line.

    3) if i add this line
    Code:
    studentAverage = studentTestTotal / numberOfTests;
    the program once again does not work. It is driving me crazy help is greatly appreciated

  2. #2
    Registered User
    Join Date
    Sep 2009
    Posts
    68
    I noticed a few people have looked at this topic. Any solutions advice suggestions anything would be appreciated...

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    1. Where were you planning to reset counter?

    2. This sentence makes no sense.

    3. Define "does not work".

  4. #4
    Registered User
    Join Date
    Sep 2009
    Posts
    68
    Well yes to my knowledge I would have to reset counter but in this case it works without me doing it... idk why.

    What i meant by the second statment is. I want the scores to appear like this eventually

    Name score1 score2 score 3 average grade

    but now it comes out like this

    name
    score1
    score2
    score3

    thirdly.
    Code:
    #include <iostream>
    #include <string>
    #include <iomanip>
    #include <fstream>
    #include <windows.h>
    
    using namespace std;
    
    int main()
    {
        
        int counter; //While statement counter
        int numberOfTests;
        int studentTestTotal;
        int testScore; //Test score from file
        float studentAverage;
        float classAverage;
        string studentName; //Student Name
        string fileName;
        
        ifstream inFile;
        
        cout << "Please enter the filename to retrieve scores from: ";
        getline(cin,fileName);
        
        inFile.open(fileName.c_str());
        
        if (!inFile)
        {
             cout << "File failed to open";
        }    
    
        inFile >> numberOfTests; 
        
        while (!inFile.eof())
        {
             inFile >> studentName;
             cout << studentName;
             studentTestTotal = 0;
             studentAverage = 0;
             
             while (counter <= numberOfTests)
             {
                   inFile >> testScore;
                   cout << testScore;
                   studentTestTotal = studentTestTotal + testScore;
                   counter++;
             }
             studentAverage = (studentTestTotal/numberOfTests);
             cout << studentAverage;
             
             
             cout << endl;
             
                   
        }
        
        inFile.close();
        system("pause");
        return 0;
              
    }
    it add a zero to EVERY single line. If you were available to chat in AIM maybe I would greatly appreciate it.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    1. Not resetting counter "works" if you only have one student in the file. If you have more than one, it will not read future students. (Or to be more specific, it will read every other piece of data in the file as a name, not as a grade.)

    2. That has nothing to do with inFile << and everything to do with you printing out new lines (see above).

    3. This is still back to 1 above -- you're not reading in any tests, consequently all the averages are zero.

  6. #6
    Registered User
    Join Date
    Sep 2009
    Posts
    68
    ooo ok the first part makes sense... So how would I go about making it read them as tests making the division work? simply reset the counter? where would that be exactly? Is there anyway we could chat in a IM?

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You need to reset the counter when you want the counter to go back to zero (i.e. when you are starting a new student). Note also that you are reading one too many numbers for your tests (i.e., if number of tests is 4, you will try to read 5 numbers).

  8. #8
    Registered User
    Join Date
    Sep 2009
    Posts
    68
    DOH! gah thank you tabstop! you are amazing. My counter was my problem. I wasn't thinking it started at 0 and not 1. I appreciate it greatly.

  9. #9
    Registered User
    Join Date
    Sep 2009
    Posts
    68
    crap one more quick question tab. Say i want to add headers to this went i output

    How would I do the headers because one file might have 3 test scores while the other may have 4?

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Put the "Test" part of the header in a loop.

  11. #11
    Registered User
    Join Date
    Sep 2009
    Posts
    68
    o so basically like

    Code:
    while ( x < numberOfTests )
    {
    cout << "test" << x;
    }
    ? correcto

  12. #12
    Registered User
    Join Date
    Sep 2009
    Posts
    68
    Can i align the scores anyway? so the output looks decent

  13. #13
    Registered User
    Join Date
    Sep 2009
    Posts
    68
    I got it just using setw()

    Thanks for all the help tab

  14. #14
    Registered User
    Join Date
    Aug 2006
    Posts
    99
    Code:
    studentAverage = studentTestTotal / numberOfTests;
    Let's see...

    (float) = (int) / (int)

    Something look funny there?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. No clue how to make a code to solve problems!
    By ctnzn in forum C Programming
    Replies: 8
    Last Post: 10-16-2008, 03:59 AM
  2. String Manipulation problems -_-
    By Astra in forum C Programming
    Replies: 5
    Last Post: 12-13-2006, 05:48 PM
  3. Rendering problems (DirectX?)
    By OnionKnight in forum Tech Board
    Replies: 0
    Last Post: 08-17-2006, 01:17 PM
  4. contest problems on my site
    By DavidP in forum Contests Board
    Replies: 4
    Last Post: 01-10-2004, 09:19 PM
  5. DJGPP problems
    By stormswift in forum C Programming
    Replies: 2
    Last Post: 02-26-2002, 04:35 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21