he is what the home work should be. Programming Assignment
CS 320 - Homework Program #1
Due: Sunday, at the end of Week #2
Write a program to compute a student’s tuition for one semester, according to the following specifications.
A student may be classified as undergraduate or graduate. Prompt the user for the student classification and number of units. Error check ALL user input for validity. Re-prompt and allow user corrections for each item when the data is invalid. NOTE: the program should accept either 'U' or 'u' for undergraduate, and either 'G' or 'g' for graduate, and a positive value for the number of units.
Use the following tables to compute the tuition:

Number of Units (UNDERGRADUATE)
Over
But Not Over
Tuition
0
6
$ 500 flat fee + $ 250 per unit
6
12
$ 450 flat fee + $ 245 per unit
12
18
No fee + $ 270 per unit
18
$ 5100 flat fee

Number of Units (GRADUATE)
Over
But Not Over
Tuition
0
6
$ 500 flat fee + $ 250 per unit
6
12
$ 750 flat fee + $ 285 per unit
12
18
No fee + $ 340 per unit
18
$ 6400 flat fee

The program should loop and continue prompting for student classifications and numbers of units until the user indicates s/he is done.

Outputs: Display to the monitor:
Class Undergraduate/Graduate (display word, not letter)
Units xx
Tuition $ xxxxxx.xx

Although the logic for this program is fairly straight-forward, you should allow plenty of time to get used to the C syntax. Only one function, main(), is required in this program. Still, remember to develop and test your code one section at a time.

this is what i have come up with can some one tell me if im on the right track.. I alos fee like I'm doing this the long way.

/*****************************************
File Name: Tmitchellwk1
Description: Calcutaing tuition based on Classification and number of credits
Desinger: Tabatha Mithcell
Date: July 7, 2006
Functions:  Input, output
*****************************************/
#include <stdio.h>
main (void)
{
  #define flatfeeone 500.00    /*flat rate for up to 6 credits*/
  #define Flatfeeu  450.00    /*rate for undergrad rate for 6 to 12
credits*/
  #define Flatfeeg  750.00    /*rate for graduate for 6 to 12 credits*/
  #define flatunit  250.00    /*rate for fee per credit up to 6 credits*/
  #define Flatunitu  245.00    /*rate for undergrate for 6-12 credits*/
  #define Flatunitg  285.00    /*rate for graduate for 6-12 credits*/
  #define Flatpuu    270.00    /*rate for undgrade for 12-18 credits*/
  #define Flatgu     340.00    /*rate for graduate for 12-18 credit*/
  #define Flatu     5100.00    /*rate for undgrade for 18 or more credits*/
  #define Flatg     6400.00    /*rate for graduate for 18 or more credits*/

int credits;    /*number of credits the person is taking*/
char status,    /*Student status (G,g-Grad or U,u-Undergrad)*/
     data;    /*mORE DATA TO ADD*/
float cost;    /*amount of tutition*/

printf ("Data\n (y= yes, n = NO)\n\n");  
printf ('Do you have more data to add.");
scanf ( "%c", &data);

while ( DATA != N ) {
printf ("Status\n (U = undergraduate, G = Graduate)\n\n");
printf ("Enter status (G/U): ");
scanf ( "%c", &status );
if ( (status == 'g') || (status == 'G') )
{  
     printf ("Enter number of credits.");
    scanf ( "%d", &credits );
      
      if ( (credits >= 0) && (credits <= 6) )
        cost =  flatfeeone + (credits * flatunit);
      
       if ((credits > 6) && (credits <= 12))
         cost =  Flatfeeg + (credits *Flatunitg);
        
        
      if  ((credits > 12) && (credits <=18))
         cost = Flatgu * credits;
    
     if (credits > 18)
       cost = Flatg;
      
}    
else if ((status == 'u') || (status == 'U'))
{  
    printf ("Enter number of credits.");
    scanf ( "%d", &credits );
      
      if ( (credits >= 0) && (credits <= 6))
        cost = flatfeeone + (credits * flatunit);
       if ( (credits > 6) && (credits <= 12))
        cost =  Flatfeeu + (credits *Flatunitu);
      if  ( (credits > 12 ) && (credits <= 18))
        cost = Flatpuu * credits;
     if (credits > 18)
        cost = Flatu;
} 
    
printf ("\nClass    %c", status); //fixed output
printf ("\nUnits     %d", credits); //fixed output
printf ("\nTuition  $%.2f\n", cost); //fixed output
}
return 0;
}

I'd say you're on the right track but:
1) please place code posted to this board inside code tags to preserve indentation. If your code truly looks like that, then please learn how to indent.
2) Your instructor put this

remember to develop and test your code one section at a time.

in the instructions for a reason. I'm surprised your code compiled given that DATA hasn't been declared as a variable (data has been declared but C/C++ code is case specific so DATA is not the same as data).

To be standard, main() should declared with return type of int, not void. Hopefully your instructor encourages standard code where possible. Indeed, you even have a return statement at the end of your code, which wouldn't be needed if main() returned type void, and which I would have expected your compiler to complain about if you truly declared main() with return type void.

ok that make a little more since. But it looks like my while loop is off. Is there something wrong with that as well.

Be as consistent as you can with your coding style. For example, if you capitalize the first letter of a "variable" that's been defined then do it for every "variable" declared with a define. If you place the opening curly brace on the same line as the loop keyword or the if/else conditional, then do it for every loop keyword or if/else conditional.

Also, I'd encourage you to indent more like this:

while
  if
     if
     else if
     else if
     else
  else
     if 
     else if
     else if
     else

as it's much easier to read.

Since the secondary if statements are mutually exclusive I would use a single if with several else ifs rather than sequential ifs.

Those are all style issues, but the changes will help you, and others, debug your code.


As to the concern with the while loop being "off", that is a vague concern. Do you mean "off" to be if you enter a g followed by a 5 you don't get the correct answer? Or what do you mean by "off". Fixing the style issues will make it look "better" visually. But, because of the indentation concerns, it is a little difficult for me to wade through the while loop, but the logic seems intact. You might have a syntax problem where you meant to put one define "variable" and you put another by mistake, but otherwise the logic looks pretty good to me, short of compiling and running the program and putting in the appropriate test values to see if I get the appropriate result----which is really something you should be doing.

This is the program that i have come up with so far. But for some reason it is stuck in a loop. And i'm lost as in to if it should olny be one out put. Or should the out put be for all of the number that you entered.

#include <stdio.h>
main ()
{
#define flatfeeone 500.00 /*flat rate for up to 6 credits*/
#define Flatfeeu 450.00 /*rate for undergrad rate for 6 to 12
credits*/
#define Flatfeeg 750.00 /*rate for graduate for 6 to 12 credits*/
#define flatunit 250.00 /*rate for fee per credit up to 6 credits*/
#define Flatunitu 245.00 /*rate for undergrate for 6-12 credits*/
#define Flatunitg 285.00 /*rate for graduate for 6-12 credits*/
#define Flatpuu 270.00 /*rate for undgrade for 12-18 credits*/
#define Flatgu 340.00 /*rate for graduate for 12-18 credit*/
#define Flatu 5100.00 /*rate for undgrade for 18 or more credits*/
#define Flatg 6400.00 /*rate for graduate for 18 or more credits*/
 
int credits; /*number of credits the person is taking*/
char status, /*Student status (G,g-Grad or U,u-Undergrad)*/
data; /*mORE DATA TO ADD*/
float cost; /*amount of tutition*/
 
printf ("Data\n (y= yes, n = NO)\n\n"); 
printf ("Do you have more data to add.");
scanf ( "%c", &data);
while ( data != 'n' && data != 'N' ) { // fixed variable in all caps and enclose in ''
printf ("Status\n (U = undergraduate, G = Graduate)\n\n");
printf ("Enter status (G/U): ");
getchar(); //need this to clear the buffer
scanf ( "%c", &status );
printf ("Enter number of credits.");
scanf ( "%d", &credits );
if ( (status == 'g') || (status == 'G') )
{ 
 
if ( (credits > 0) && (credits <= 6) )
cost = flatfeeone + (credits * flatunit);
 
if ((credits > 6) && (credits <= 12))
cost = Flatfeeg + (credits *Flatunitg);
 
 
if ((credits > 12) && (credits <=18))
cost = Flatgu * credits;
 
if (credits > 18)
cost = Flatg;
 
} 
else if ((status == 'u') || (status == 'U'))
{ 
 
if ( (credits > 0) && (credits <= 6))
cost = flatfeeone + (credits * flatunit);
if ( (credits > 6) && (credits <= 12))
cost = Flatfeeu + (credits *Flatunitu);
if ( (credits > 12 ) && (credits <= 18))
cost = Flatpuu * credits;
if (credits > 18)
cost = Flatu;
}
//need to prompt user to continue adding data
printf ("\nData\n (y= yes, n = NO)\n\n"); 
printf ("Do you have more data to add.");
scanf ( "%c", &data);
getchar(); //need this to clear the buffer
}
 
printf ("\nClass %c", status); 
printf ("\nUnits %d", credits); 
printf ("\nTuition $%.2f\n", cost); 
return 0;
}

Again, I hope it's the board is screwing up your indentation because the second program you posted looks ugly(!) and is very difficult to read.

Please, if you are going to declare main() with return type void, then don't use a return statement. You'd be much better off declaring main with return type int though. (int main(){......return 0;})

What do you mean by "it is stuck in a loop"?

That could mean that you aren't seeing any output at the end of your program after the calculations are complete so you assume you are in an infinite loop, when in reality the program is just closing before you can visualize the final output. In that case, pause the program (put one or more fgets() or whatever, just before the return statement).

However, that could also mean that you are stuck in an infinite loop, in which case you will have to track it down somehow. I usually use a series of output statements, to document where I am, followed by a statement to pause the flow of the program, (fgets() or whatever) so I can actually see where I am in the program.

The most likely infinite loop would be the while loop you have. Therefore, if you think you are in an infinite loop, then I would check the value of data to be sure it is what you think it is. (I also try to avoid conditionals like (data != 'n' && data != 'N') because it's too easy for me to get tripped up in the boolean logic. I'd take the input and change it to either upper or lower case and then just test the one case, not both.)

If that isn't it, then peppering the program with other instances of pausing statements can be done to track down what seems to be flipping you into an infinite loop.

If your program is expecting an input of an int and it gets an input of a char, that can also cause an infinite loop. Therefore, validating input is of the proper type may be helpful but is a bit of a hassle for beginner type programs, where assumption of valid input types is the norm.

Of course, if you aren't refering to being stuck in an infinite loop, then these ideas won't help.

To me, the instruction set would imply that the output statements indicating the total cost for a given student should be within the while loop. As written, your program will only output the last values of status, credits, and cost that were entered, not the information entered student by student.

Please try and understand the code given below and ask if you have doubts regarding it and try to write the code once again on your own.

Maybe this is what you were looking for :

#include <stdio.h>
const float flatfeeone = 500.00; //flat rate for up to 6 credits
const float Flatfeeu = 450; //rate for undergrad rate for 6 to 12 credits
const float Flatfeeg = 750; //rate for graduate for 6 to 12 credits
const float flatunit = 250; //rate for fee per credit up to 6 credits
const float Flatunitu = 245; //rate for undergrate for 6-12 credits
const float Flatunitg = 285; //rate for graduate for 6-12 credits
const float Flatpuu = 270; //rate for undgrade for 12-18 credits
const float Flatgu = 340; //rate for graduate for 12-18 credit
const float Flatu = 5100; //rate for undgrade for 18 or more credits
const float Flatg = 6400; //rate for graduate for 18 or more credits
void clearGarbage ()
{
while ( (getchar () != '\n') || (getchar () != EOF) )
{
// clear buffer and advance
}
}
float computeGraduateTution (int credits)
{
float cost = 0;
if ( (credits > 0) && (credits <= 6) )
cost = flatfeeone + (credits * flatunit);
if ((credits > 6) && (credits <= 12))
cost = Flatfeeg + (credits *Flatunitg);
if ((credits > 12) && (credits <=18))
cost = Flatgu * credits;
if (credits > 18)
cost = Flatg;
return cost;
}
float computeUnderGraduateTution (int credits)
{
float cost = 0;
if ( (credits > 0) && (credits <= 6))
cost = flatfeeone + (credits * flatunit);
if ( (credits > 6) && (credits <= 12))
cost = Flatfeeu + (credits *Flatunitu);
if ( (credits > 12 ) && (credits <= 18))
cost = Flatpuu * credits;
if (credits > 18)
cost = Flatu;
return cost;
}
int getChoice ()
{
char ch;
fflush (stdin);
printf ("\nDo you want to continue? ");
while (1)
{
scanf ("%c", &ch);
switch (ch)
{
case 'y':
case 'Y':
return 1;
case 'n':
case 'N':
return 0;
default:
printf ("Do you want to continue? ");
fflush (stdin);
}
}
}
void gatherData (char* ptrStatus, int* ptrCredits)
{
do
{
fflush(stdin);
printf ("Enter status (g/u): ");
scanf ("%c", ptrStatus);
}
while (*ptrStatus != 'g' && *ptrStatus != 'G'&& *ptrStatus != 'u' && *ptrStatus != 'U' );
fflush(stdin);
do
{
printf ("Enter the credit of the student: ");
scanf ("%d", ptrCredits);
}
while (*ptrCredits < 0);
fflush(stdin);
}
float computeTution (char tmpStatus, int tmpCredits)
{
float cost = 0;
switch (tmpStatus)
{
case 'g':
case 'G':
cost = computeGraduateTution (tmpCredits); 
break;
case 'u':
case 'U':
cost = computeUnderGraduateTution (tmpCredits);
break;
default:
printf ("Please enter the correct course: ");
break;
}
return cost;
}
void display (char tmpStatus, int tmpCredits, float tmpCost)
{
printf ("\n---------------------------------------------------------------------");
if (tmpStatus == 'g' || tmpStatus == 'G')
printf ("\nClass : Graduate"); 
else
printf ("\nClass : Under Graduate"); 
printf ("\nCredits: %d", tmpCredits); 
printf ("\nTuition: $%.2f\n", tmpCost); 
printf ("---------------------------------------------------------------------\n");
}
 
int main (void)
{
int credits; 
char status,choice; 
float cost; 

do
{ 
gatherData (&status, &credits); 
cost = computeTution (status, credits);
display (status, credits, cost);
}
while (getChoice ());
return 0;
}

this just seems like it is a lot longer. but it dose the same thing. THe code that i wrote. Was any of it going in the correct direction. Or is this just another way to write the same thing. I see that you used a lot more floast and did not do any defines. ANd i also see that you did not use a while statement. Can you explain that to me.

~sos gave a solution that does basically what you do except that it uses multiple functions instead of a single function. At least in C++, it is my understanding that const variables are prefered to defines, but I'm not sure if the same holds in C or not. ~sos does a little more error checking, and uses switch statements instead of changing input to all upper or all lower case or checking both upper and lower case within a conditional, but the underlying logic of using a loop to gather information, using a series of if statements to do the calculations, etc is the same. I thought your solution was on the right track (and the indentation comments hold as well for ~sos as for you). Since I don't routinely download and run programs, I can't say where the problem was in your program, if there was one. Hopefully, you've tried to debug it as I explained previously, so you can be more explicit in where you found a problem. If it works okay so far, then you could add the additional error checking that is called for in the instruction set, but for now I would make sure it works as expected as is before trying to add something different.

this just seems like it is a lot longer. but it dose the same thing. THe code that i wrote. Was any of it going in the correct direction. Or is this just another way to write the same thing. I see that you used a lot more floast and did not do any defines. ANd i also see that you did not use a while statement. Can you explain that to me.

>>> #defines are basically not used due to the bad publicity attached to it ( u can read the articles on #defines by googling for them) but again it is a matter of personal preference. Any function or progg construct performs to the fullest when in proper hands, so as far as beginners are concerned stick to using const since it makes more sense if you look at the program in a logical way.

The bottom line dont use #define as far as constant values in the program are concerned.

>>>
I didtn use a while { } loop, instead i used a do {....} while (condition); loop, since the input from the user session has to be carried out atleast once.

>>>
Also i added a bit of error checking in your code since your original code didnt check for any problems with the user input and assumed that the input was perfect.

>>>
Yes my prog in the end does wat you intended to do in your original post but i personally advice you to use "functions based on logical differentiation" that is functions that concentrate on one task at a time. Its better to write modular code than a single huge code block since it minimizes the bugs and makes error detection easy.

Hope this explanation sufficed the answers to your questions. If not you are free to ask again. Bye.

This article has been dead for over six months. Start a new discussion instead.