There's no such thing as a stupid question, but they're the easiest to answer.
JoinTour
Login
 
Tag Cloud
acer black screen boot bsod computer connection crash css dell display drive driver drivers email error ethernet excel explorer firefox firefox 3 hard drive internet internet explorer itunes laptop lcd malware monitor network networking outlook outlook 2003 outlook express partition password printer problem problems ram router slow sound sprtcmd.exe trojan usb virus vista windows windows xp wireless
Software Development
Search
Search in:
 
Advanced Search
Tech Support Guy Forums > Software & Hardware > Software Development >
C++ assignment problem


HELLO AND WELCOME! Before you can post your question, you'll have to register -- it's completely free! Click here to join today! We highly recommend that you print a copy of our Guide for New Members. Enjoy!

Closed Thread
 
Thread Tools
mojomojoa's Avatar
Member with 45 posts.
 
Join Date: Jul 2003
29-Feb-2004, 02:48 AM #1
C++ assignment problem
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;
}
Shadow2531's Avatar
Distinguished Member with 2,629 posts.
 
Join Date: Apr 2001
29-Feb-2004, 05:21 AM #2
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.
AlbertB's Avatar
Distinguished Member with 2,432 posts.
 
Join Date: Nov 2002
Location: Hampshire, UK
29-Feb-2004, 11:40 AM #3
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
   };
}
AlbertB's Avatar
Distinguished Member with 2,432 posts.
 
Join Date: Nov 2002
Location: Hampshire, UK
29-Feb-2004, 11:56 AM #4
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!
__________________
1. "I make no personal claim to the truth, only the right to seek it, prove it in argument, and to be wrong many times in order to reach it."

2. "We have made a cage of words and placed our God inside, as boys trap a cricket, to make him sing for us alone."

Galileo Galilei
mojomojoa's Avatar
Member with 45 posts.
 
Join Date: Jul 2003
29-Feb-2004, 02:46 PM #5
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.
AlbertB's Avatar
Distinguished Member with 2,432 posts.
 
Join Date: Nov 2002
Location: Hampshire, UK
29-Feb-2004, 06:46 PM #6
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.
__________________
1. "I make no personal claim to the truth, only the right to seek it, prove it in argument, and to be wrong many times in order to reach it."

2. "We have made a cage of words and placed our God inside, as boys trap a cricket, to make him sing for us alone."

Galileo Galilei

Last edited by AlbertB : 29-Feb-2004 06:54 PM.
mojomojoa's Avatar
Member with 45 posts.
 
Join Date: Jul 2003
01-Mar-2004, 01:48 AM #7
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%
Shadow2531's Avatar
Distinguished Member with 2,629 posts.
 
Join Date: Apr 2001
01-Mar-2004, 05:02 AM #8
AlbertB made good points.

Quote:
Originally posted by mojomojoa:I was instructed to write it so the Residency code and the Hours are entered together with only a space between them
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;
}

Last edited by Shadow2531 : 01-Mar-2004 11:57 AM.
AlbertB's Avatar
Distinguished Member with 2,432 posts.
 
Join Date: Nov 2002
Location: Hampshire, UK
01-Mar-2004, 07:58 AM #9
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 .





I suppose the STL string class is out of the question?
__________________
1. "I make no personal claim to the truth, only the right to seek it, prove it in argument, and to be wrong many times in order to reach it."

2. "We have made a cage of words and placed our God inside, as boys trap a cricket, to make him sing for us alone."

Galileo Galilei
Shadow2531's Avatar
Distinguished Member with 2,629 posts.
 
Join Date: Apr 2001
01-Mar-2004, 09:27 AM #10
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.
__________________
10 ? "a line as the unending horizon"
20 ? "a curve as the rolling hillside"
30 ? "a point as a distant bird"
40 ? "a ray as the rising sun"
run

Last edited by Shadow2531 : 01-Mar-2004 09:50 AM.
Shadow2531's Avatar
Distinguished Member with 2,629 posts.
 
Join Date: Apr 2001
01-Mar-2004, 10:25 AM #11
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;
}
__________________
10 ? "a line as the unending horizon"
20 ? "a curve as the rolling hillside"
30 ? "a point as a distant bird"
40 ? "a ray as the rising sun"
run
mojomojoa's Avatar
Member with 45 posts.
 
Join Date: Jul 2003
02-Mar-2004, 02:19 AM #12
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;
	}
Shadow2531's Avatar
Distinguished Member with 2,629 posts.
 
Join Date: Apr 2001
02-Mar-2004, 02:29 AM #13
Now THAT makes sense.

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

Last edited by Shadow2531 : 02-Mar-2004 03:02 AM.
Closed Thread

THIS THREAD HAS EXPIRED.
Are you having the same problem? We have volunteers ready to answer your question, but first you'll have to join for free. Need help getting started? Check out our Welcome Guide.


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
WELCOME TO TECH SUPPORT GUY! Are you looking for the solution to your computer problem? Join our site today to ask your question -- for free! Our site is run completely by volunteers who help people like you solve computer problems. See our Welcome Guide to get started.



Thread Tools


You Are Using:
Server ID
Advertisements do not imply our endorsement of that product or service.
All times are GMT -4. The time now is 01:53 PM.
Copyright © 1996 - 2008 TechGuy, Inc. All rights reserved.
Powered by vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Powered by Cermak Technologies, Inc.