C++ Help

Announcements
    • 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...
 
 
 
Write a reply… Reply
Submit reply

Register

Thanks for posting! You just need to create an account in order to submit the post
  1. this can't be left blank
    that username has been taken, please choose another Forgotten your password?
  2. this can't be left blank
    this email is already registered. Forgotten your password?
  3. this can't be left blank

    6 characters or longer with both numbers and letters is safer

  4. this can't be left empty
    your full birthday is required
  1. Oops, you need to agree to our Ts&Cs to register
  2. Slide to join now Processing…

Updated: October 7, 2016
TSR Support Team

We have a brilliant team of more than 60 Support Team members looking after discussions on The Student Room, helping to make it a fun, safe and useful place to hang out.

Poll
Would you prefer to be told about sex by your:
Study resources

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.