CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Oct 2014
    Posts
    1

    C++ while statement and switch statment

    I'm creating a program for my assignment and keep running into problems:
    Assignment requirements:
    Write a program that lets a User enter letter grades of A,B,C,D or F and counts the number of A's, B's, C's, D's and F's. The User should enter a grade of Z to end data entry. Hence, the screen should include something like:
    : (a description of what the program will do)
    :
    Please enter a letter grade or Z to stop ==> B
    Please enter a letter grade or Z to stop ==> C
    Please enter a letter grade or Z to stop ==> C
    Please enter a letter grade or Z to stop ==> E
    Sorry, E is not a valid grade.
    Please enter a letter grade or Z to stop ==> d
    :
    :
    Please enter a letter grade or Z to stop ==> A
    Please enter a letter grade or Z to stop ==> Z

    There were 3 A's
    There were 1 B's
    There were 5 C's
    There were 2 D's
    There were 0 F's

    Requirements:
    Your program must use a switch statement.
    Use a while() loop with SENTINEL value Z

    This is my program so far also I keep getting error C2660: 'updateCounts' : function does not take 5 arguments:

    // A program that lets a User enter letter grades and counts the number each grade.


    #include <iostream> // provides cout, etc.
    using namespace std;

    void explain_prog_to_user();
    // tells user what the program does

    void decimal_format();
    // formats decimals nicely in output stream os


    void updateCounts (char grade, int& countA, int& countB, int& countC, int& countD, int &countF);


    const char SENTINEL = 'Z';

    int main()
    {
    char default=0;
    char grade;
    int numA=0, numB=0, numC=0, numD=0, numF=0; //accumulator variables needed to report at end

    explain_prog_to_user(); // tell user what proegam does

    decimal_format(); // decimals formatted nicely on screen

    cout << "\n Please enter a letter grade or\n" << SENTINEL << "to stop\n";
    cin >> grade;
    grade=toupper(grade);

    while(grade != SENTINEL)
    {

    if(grade== default)
    cout << "Grade invalid. Please try again \n\n";
    else
    {
    cout << "\nPlease enter your grade: ";
    cin >> grade;



    // update grade counts
    updateCounts (numA, numB, numC, numD, numF);

    }

    // start to process next worker

    cout << "\n Please enter a letter grade or\n" << SENTINEL << "to stop\n";
    cin >> grade;
    grade=toupper(grade);

    }

    // output final total grade counts

    cout << " Totals: \n";
    cout << "Number of A " << numA << endl;
    cout << "Number of B " << numB << endl;
    cout << "Number of C " << numC << endl;
    cout << "Number of D: " << numD << endl;
    cout << "Number of F: " << numF<< endl;


    system("pause");
    return 0;

    }

    void explain_prog_to_user()
    {
    cout << "\nThis program calculates the number of grades of A, B, C, D, and F \n\n";

    }

    void decimal_format()
    {
    cout.setf(ios::fixed);
    cout.setf(ios::showpoint);
    cout.precision(2);
    }



    void updateCounts (char grade, int& countA, int& countB, int& countC, int& countD, int& countF)
    // Precondition: grade is entered by user
    // Postcondition: the grade for the current user’s has been increased by 1.
    {
    switch (grade)
    {
    case 'A': countA++;
    break;
    case 'B': countB++;
    break;
    case 'C': countC++;
    break;
    case 'D': countD++;
    break;
    case 'F': countF++;
    break;
    default: break;
    };
    return;
    }

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: C++ while statement and switch statment

    Quote Originally Posted by wrecktangle View Post
    ...
    This is my program so far also I keep getting error C2660: 'updateCounts' : function does not take 5 arguments:
    ...
    Code:
    void  updateCounts (char grade, int&  countA, int&  countB, int&  countC, int&  countD, int &countF);
    Yes, this function does not take 5 arguments, it takes 6 ones.
    So why do you pass in only 5 parameters?
    Victor Nijegorodov

  3. #3
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,923

    Re: C++ while statement and switch statment

    When posting code, please format your code properly first and use code tags. Go Advanced, select the code and click '#'. As posted, your code is not easy to read.

    Also you have some logic issues with the program. How are you determining whether an entered grade is valid or invalid?
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured