Join TSR now and get answers to all your questions about uniSign up now

C++ Help

    • Thread Starter
    Offline

    0
    ReputationRep:
    Hello, is there anyone who can help with the following C++ question:

    Create a program that will read in a set of grades from a file called “grade.txt” (see below) and report the total number of letter grades i.e. A, B, C, D and F in the file. You program must use case logic and the “switch” statement.
    “grade.txt” :

    ABADACAFABCDFFFACDCCBBACBACCCBBA AA
    AADBACAFFBBCCDAABBFFAACCBBAACCCC BB
    Offline

    0
    ReputationRep:
    Im not at my computer, but here is the psuedo code

    Int numOfA, numOfB, numOfC; ect for a counter of each grade in the file.

    You then want to read the first letter of the file using fstream and set it to a temporary variable (in this case string value; ), and use switch case to determine its grade. All switch case is is an if, else if, else if ect construct, it just looks cleaner.
    Eg switch {
    Case 1: (value == "A" )
    numOfA++;
    Case 2 ....

    }

    You want to loop through the whole file letter by letter, performing the switch case to increment your counters of grades.

    At the end of the file, you want to close the file, and display the values of each count
    cout <<" Number of A's is: " + numOfA << endline;
    Offline

    0
    ReputationRep:
    Heres a tutorial on file reading which should help https://youtu.be/Iho2EdJgusQ
    and one in switch case https://youtu.be/_CelY_ZBXb4
    • Thread Starter
    Offline

    0
    ReputationRep:
    (Original post by DocileDom)
    Im not at my computer, but here is the psuedo code

    Int numOfA, numOfB, numOfC; ect for a counter of each grade in the file.

    You then want to read the first letter of the file using fstream and set it to a temporary variable (in this case string value; ), and use switch case to determine its grade. All switch case is is an if, else if, else if ect construct, it just looks cleaner.
    Eg switch {
    Case 1: (value == "A" )
    numOfA++;
    Case 2 ....

    }

    You want to loop through the whole file letter by letter, performing the switch case to increment your counters of grades.

    At the end of the file, you want to close the file, and display the values of each count
    cout <<" Number of A's is: " + numOfA << endline;

    You are a legend.
    It works perfectly.
    Thank you very much!
    Offline

    2
    ReputationRep:
    You can also watch many C++ youtube tutorials.
    Offline

    0
    ReputationRep:
    (Original post by RockyH10)
    You are a legend.
    It works perfectly.
    Thank you very much!
    No problem mate, if you need any further help with c++ or c# im happy to help
    Offline

    1
    ReputationRep:
    This is teaching you bad practice. Using std::map<char, int> is a lot cleaner:

    for(char c : fileContent)
    ++map[c];

    Then can just print out at the end. But I guess you have to use switch...
 
 
 
Poll
Which party will you be voting for in the General Election 2017?

The Student Room, Get Revising and Marked by Teachers are trading names of The Student Room Group Ltd.

Register Number: 04666380 (England and Wales), VAT No. 806 8067 22 Registered Office: International House, Queens Road, Brighton, BN1 3XE

Quick reply
Reputation gems: You get these gems as you gain rep from other members for making good contributions and giving helpful advice.