1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

C++ assignment problem

Discussion in 'Software Development' started by mojomojoa, Feb 29, 2004.

Thread Status:
Not open for further replies.
Advertisement
  1. mojomojoa

    mojomojoa Thread Starter

    Joined:
    Jul 28, 2003
    Messages:
    45
    I'm having a problem with an assignment. I'm new to c++, so this shouldn't be too hard. Here's the instructions:

    Revise the preceding assignment to distinguish resident and nonresident students. Input a residency status code followed by the number of hours enrolled. The status code is "R" for a resident, "Q" for the end-of-data-signal, and any other character for a non-resident. When a "Q" is entered, do not require the user to enter the number of hours enrolled. The letters may be either upper or lower case. The tuition rate is $52.00 per hour for residents and $110.00 per hour for nonresidents. Revise the final statistics to output (1) the average tuition paid, (2) the highest tuition paid, (3) the lowest tuition paid, and (4) the percentage of students who are residents. Use the format shown below.

    My Problem: after typing in q, I still have to type in a number for "Hours" before hitting enter. Can anyone help me?

    My code:
    #include <iostream>
    #include <iomanip>
    #include <limits>
    using namespace std;

    //===========================================

    int main()
    {
    const double RTUITION_RATE = 52, NTUITION_RATE = 110;
    double Hours,Tuition, SCount,Highest,Lowest,AverageTuition,
    PercentResidents,AllTuition,RCount;
    char RCode;

    SCount = 0;
    RCount = 0;
    AllTuition = 0;
    Highest = 0;
    Lowest = INT_MAX;


    //===================================================================================

    do
    {

    cout << "Enter residency code and number of hours enrolled (Q to quit): ";
    cin >> RCode >> Hours;

    if ((RCode != 'q') && (RCode != 'Q'))
    {
    SCount++;
    Tuition = Hours * NTUITION_RATE;
    if ((RCode == 'R') || (RCode == 'r'))
    Tuition = Hours * RTUITION_RATE;
    AllTuition += Tuition;
    if (Tuition > Highest)
    Highest = Tuition;
    if (Tuition < Lowest)
    Lowest = Tuition;
    if ((RCode == 'R') || (RCode == 'r'))
    RCount++;
    cout << "Tutition is $" << Tuition << endl << endl;
    }
    }
    while ((RCode != 'q') && (RCode != 'Q')) ;

    //===================================================================================

    AverageTuition = AllTuition / SCount;
    PercentResidents = (RCount / SCount) * 100;

    cout << fixed << setprecision(2);
    cout << endl;
    cout << "Average tuition: $" << setw(7) << AverageTuition << endl;
    cout << "Highest tuition: $" << setw(7) << Highest << endl;
    cout << "Lowest tuition: $" << setw(7) << Lowest << endl;
    cout << "Percent residents: " << setprecision(1) << setw(6) << PercentResidents
    << "%" << endl << endl;
    return 0;
    }
     
  2. Shadow2531

    Shadow2531

    Joined:
    Apr 30, 2001
    Messages:
    2,636
    Here's a hint for you.

    Change

    cin >> RCode >> Hours;

    to

    cin >> RCode;

    and you will see that's your problem line.

    Also consider that if the while loop itself, checks for q or Q, you MIGHT not need the extra check for q or Q inside the loop.
     
  3. AlbertB

    AlbertB

    Joined:
    Nov 24, 2002
    Messages:
    2,438
    I think it may also be useful to look at the way the question is worded. Code 'R' or 'r' for resident, 'Q' or 'q' for quit and anything else for non-resident suggests to me that they expect something of the form of

    Code:
    if ((RCode != 'q') && (RCode != 'Q')) // Control entry into this section by verifying we do not want to quit
    {
       SCount++;
       if ((RCode == 'R') || (RCode == 'r')) 
       {
          Tuition = Hours * RTUITION_RATE; // Calculate the cost for Residents if entry is 'R'
       }
       else // Key addition this!
       {
          Tuition = Hours * NTUITION_RATE; Calculate the cost for non-residents for eveything else
       };
    }
    
    :)
     
  4. AlbertB

    AlbertB

    Joined:
    Nov 24, 2002
    Messages:
    2,438
    Shadow is right overall. If "cin >> RCode" and "cin >> Hours" are separated and put into the relevant different parts of the program the second entry will appear on screen only when necessary according to the first but the user will not see any difference.

    A little point here, you have declared your variable "char RCode" at the beginning of the program but have not initialised it to some safe value. What happens if just by chance when it has been created it should find itself randomly containing the code for 'Q'? I am not aware that it is ever initialised to a specific default value for you. (Shadow :) ?) So you should declare it perhaps as 'A' to ensure getting into the structure the first time around before you have set it to a value on screen. Initialisation is always a good habit to get into. Wait until you come to pointers! :D
     
  5. mojomojoa

    mojomojoa Thread Starter

    Joined:
    Jul 28, 2003
    Messages:
    45
    That's where I seem to be having my trouble, in the "cin >> RCode >> Hours" line. I'm not sure how to seperate them while at the same time allowing them both to be entered after the same prompt. Writing them in 2 seperate "cin" lines has the same effect, so I know there's got to be something else I'm not doing right.
     
  6. AlbertB

    AlbertB

    Joined:
    Nov 24, 2002
    Messages:
    2,438
    And there's more.... ;)

    I would suggest that in your declarations of your variables you always initialise them to 0 unless there is a different necessary value. If a variable is a double remember to initialise it to 0.0 not just to 0. Remember you can declare and initialise in the same statement:

    Code:
    double Hours=0.0, Tuition=0.0, ........ ;
    int number=0, IQShadow=165, ........ ;
    Now onwards.

    Code:
    If ((RCode != 'q') && (RCode != 'Q')) 
    {
       SCount++;
       Tuition = Hours * NTUITION_RATE;
       if ((RCode == 'R') || (RCode == 'r')) 
          Tuition = Hours * RTUITION_RATE;
          AllTuition += Tuition;
          ..........
    Look at this code snippet from your post and work your way through what it does. You are at a stage where the user has inputted their preference, (Q, R or other), and, at the moment, their Hours.

    First let us assume RCode is 'Q'. The first line 'If' statement effectively prevents them from entering the structure and doing any more. All is happy.

    Next let us assume RCode is Other. They have not entered 'Q' as RCode, so they enter the 'If' structure. Next SCount will be incremented. Then Tuition will be calculated from NTUITION_RATE. The next If statement will reject their RCode and they will pass over. All is still happy.

    Finally consider RCode is 'R' however. As before they have not entered 'Q', so they enter the 'If' structure. SCount will be incremented and Tuition will be calculated from NTUITION_RATE even though it should not in this case. They will then enter the second 'If' structure and recalculate Tuition from RTUITION_RATE. This is not good programming as it is unecessary action and a source of potential trouble later on.

    Look at the following code, remember RCode has been initialised to 'A', (or NULL if you prefer ;) ), and see if you can figure out the structure:

    Code:
    while ((RCode != 'q') && (RCode != 'Q'))
    {
       cout << "Enter Quit (Q), residency code (R), or non-residency code (anything else): ";
       cin >> RCode;
    
       // Control entry into this section by verifying we do not want to quit
       if ((RCode != 'q') && (RCode != 'Q'))
       {
          cout << endl << "Now give me the number of Hours: ";
          cin >> Hours;
    		
           if ((RCode == 'R') || (RCode == 'r')) 
          {
             // Calculate the cost for Residents if entry is 'R'
             Tuition = Hours * RTUITION_RATE; 
             // Other bits relevant to our choice to update in here
          }
    
          // Key addition this! Because we have got this far the choice cannot have been ('Q' or 'q'),
          // now do this for everything other than ('R' or 'r'), just what we were
          // asked for a non-resident.
          else 
          {
             Tuition = Hours * NTUITION_RATE;
             // Other bits relevant to our choice to update in here
          }
       }
    }
    Note that now we do not have the irritation of being asked to input hours even though we have asked to quit. Check where we are asked for this now and see the difference.

    This divides the code more cleanly into the sections which you want. I warn you it is a long way from finished! You will have to figure out how it works then insert the other parts of your code which update your totals into the relevant sections. This should not be too hard and is the correct way to do it, rather than have a completely new set of if statements following to total things up.

    One more tip for posting here at TSG. If you indent your code correctly you can use the "code" "/code" tags, (with [ and ] in place of my " of course), to keep your indenting, much as I have done.

    Let us see the finished code when you get there. :)
     
  7. mojomojoa

    mojomojoa Thread Starter

    Joined:
    Jul 28, 2003
    Messages:
    45
    I wish I could do it that way, but I was instructed to
    write it so the Residency code and the Hours are entered
    together with only a space between them. I should have
    posted an example of the output after my code, that probably
    would have helped. Below is an example handed out by my instructor
    that shows what the output should look like. When I run the program,
    it looks pretty much identical, except of course, I have to enter
    the extra number after 'q' to get out of the loop.(Also, thanks for the tips.)

    Enter residency code and number of hours enrolled (Q to quit): R 12
    Tutition is $624.00

    Enter residency code and number of hours enrolled (Q to quit): N 15
    Tutition is $1650.00

    Enter residency code and number of hours enrolled (Q to quit): r 10.5
    Tutition is $546.00

    Enter residency code and number of hours enrolled (Q to quit): Q

    Average tuition: $ 940.00
    Highest tuition: $1650.00
    Lowest tuition: $ 546.00
    Percent residents: 66.7%
     
  8. Shadow2531

    Shadow2531

    Joined:
    Apr 30, 2001
    Messages:
    2,636
    AlbertB made good points.

    If you want, you can grab the residency code and hours into one string, split it into 2 strings and convert the hour part to a double.

    An example of how I might go about doing it would be like this:

    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main(void)
    {
    string choice;
    string rcode;
    double hours = 0.0;
    start:
    system("cls");
    cout << "Enter rcode then space, then hours: ";
    getline(cin,choice);
    rcode = choice[0];
    rcode[0] = tolower(rcode[0]);
    if (choice[1] == ' ')
    {
    hours = atof((choice.substr(choice.find(" "))).c_str());
    }
    if (choice[1] != ' ' || hours == 0 || rcode[0] != 'r')
    {
    system("cls");
    cout << "input error" << endl << endl;
    system("pause");
    goto start;
    }
    cout << "rcode as string: " << rcode << endl
         << "hours as double: " << hours << endl;
    return 0;
    }
    
     
  9. AlbertB

    AlbertB

    Joined:
    Nov 24, 2002
    Messages:
    2,438
    Oh I see, what a convoluted problem to set! ;) I had assumed that you were to input the Residency code, then the hours only if it were shown to be necessary from the previous Res Code input.

    My apologies, bin most of the previous stuff then :D.





    I suppose the STL string class is out of the question? ;) :D
     
  10. Shadow2531

    Shadow2531

    Joined:
    Apr 30, 2001
    Messages:
    2,636
    The problem line seems to not be a problem in itself.

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main (void)
    {
    char rcode;
    double hours;
    cin >> rcode >> hours;
    cout << rcode << endl << hours;
    return 0;
    }
    
    If you input r 12 for example, it will split it up into both variables like it's supposed to.

    Back to your original code...

    to quit, instead of of just entering q or Q, enter
    q then a space and then any number.

    So to quit, enter Q 1

    Basically meaning if you enter q and then a number, it will split it between the 2 variables. If you enter just q, it's gonna ask you to input the second variable, just as it should.

    So you are going to have to use 1 string and split it into variables.
     
  11. Shadow2531

    Shadow2531

    Joined:
    Apr 30, 2001
    Messages:
    2,636
    If you are not allowed to use string, you can use char arrays like this:

    It assumes that the inputted hours is only 2 digits.

    (it's a lowsy example, but it should give you a hint on how to solve the input problem. The string example a few posts up is better)


    Code:
    #include <iostream>
    
    using namespace std;
    
    int main(void)
    {
    char choice[256];
    char strhours[1];
    char rcode;
    double hours;
    cout << endl << "Enter rcode and hours: ";
    cin.getline(choice,256);
    rcode = choice[0];
    strhours[0] = choice[2];
    strhours[1] = choice[3];
    hours = atof(strhours);
    cout << endl
         << "Hours as Double: " << hours << endl
         << "Rcode as Char: " << rcode << endl
         << endl;
    return 0;
    }
    
     
  12. mojomojoa

    mojomojoa Thread Starter

    Joined:
    Jul 28, 2003
    Messages:
    45
    I appreciate everyone's help, but I finally figured it out.
    Like I thought and others suggested, I had to divide the
    "cin >> Rcode >> Hours;" line, but it was how I was supposed
    to do this that confused me. I simply just had to put the if statement
    that verifies wether 'q' was entered or not between the lines
    "cin >> RCode;" and "cin >> Hours;" Now I can quit the program
    by hitting 'q' without having to type in any bs afterwards. Here is the code:

    Code:
    #include <iostream>
    #include <iomanip>
    #include <limits>
    using namespace std;
     
    //=========================================================
    
    int main()
    	{
    		const double RTUITION_RATE = 52, NTUITION_RATE = 110;
    		double Hours,Tuition, SCount,Highest,Lowest,AverageTuition,
    			   PercentResidents,AllTuition,RCount;
    		char RCode;
    
    		SCount = 0.0;
    		RCount = 0.0;
    		AllTuition = 0.0;
    		Highest = 0.0;
    		Lowest = INT_MAX;
    
    //=========================================================	
    
    do
    	{
    	cout << "Enter residency code and number of hours enrolled (Q to quit): ";
    	cin >>  RCode; 
    	
    	if ((RCode != 'q') && (RCode != 'Q')) 
    			{
    		cin >> Hours;
    		SCount++;
    		Tuition = Hours * NTUITION_RATE;
    		if ((RCode == 'R') || (RCode == 'r')) 
    			Tuition = Hours * RTUITION_RATE;
    		AllTuition += Tuition;
    		if (Tuition > Highest)
    			Highest = Tuition;
    		if (Tuition < Lowest)
    			Lowest = Tuition;
    		if ((RCode == 'R') || (RCode == 'r')) 
    			RCount++;
    		cout << "Tutition is $" << Tuition << endl << endl;
    			}
    	}
    	while ((RCode != 'q') && (RCode != 'Q')) ;
    	
    //=========================================================	
    	
    	AverageTuition = AllTuition / SCount; 
    	PercentResidents = (RCount / SCount) * 100;
    
    	cout << fixed << setprecision(2);
    	cout << endl;
    	cout << "Average tuition:    $" << setw(7) << AverageTuition << endl;
    	cout << "Highest tuition:    $" << setw(7) << Highest << endl;
    	cout << "Lowest tuition:     $" << setw(7) << Lowest << endl;
    	cout << "Percent residents:   " << setprecision(1) << setw(6) << PercentResidents 
    		 << "%" << endl << endl;
    return 0;
    	}
    
     
  13. Shadow2531

    Shadow2531

    Joined:
    Apr 30, 2001
    Messages:
    2,636
    Now THAT makes sense.

    We were all wondering why you couldn't do it that way.
     
  14. Sponsor

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Loading...
Similar Threads - assignment problem
  1. mohittomar13
    Replies:
    7
    Views:
    143
Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/207750

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice