|
||||||
| C and C++ C and C++ forum for discussing all forms of C except for C#. These languages are powerful low level languages used for creating Operating Systems, Device Drivers, compilers and much more. |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Display Modes |
|
|||
|
Looping statement problem : TOTAL Course fee
~Write a complete C program using while loop to compute total course fees for students.
~Ask the user to enter their name(string), id no(string) and duration of study(integer). ~Suppose that the course fee is starting from RM10000 in the first year and increases 5% the following years. ~Calculate the annual fee and totala course fees for the whole duration of study. ~Dsplay yhe information as shown below. Enter name : Ahmad bin Ali Enter ID : K12000000 Enter Duration of study [year] : 3 -------------------------------------------------------------- JAPAN MALAYSIA TECHNICAL INSTITUTE --------------------------------------------------------------- Student name : Ahmad bin Ali Student ID :K12000000 Duration of study :3 Year Course Fee 1 RM10000.00 2 RM10500.00 3 RM11025.00 ------------------------------------------------ Total Course Fees: RM31525.00 ------------------------------------------------ I have tried so many time to solve the problem. my code isnt working. The total course fees cannot calculated. |
|
|||
|
Re: Looping statement problem : TOTAL Course fee
could you explain this line?
Code:
if( printf("RM%.2f", courseFee) < 0 ) exit (1);
Code:
for(count=0; count <=year; count+=1)
{
printf("Year\t\tCourse Fee:");
total=coursefee*0.5;
all+=total;
}
Last edited by WingedPanther; 09-12-2009 at 05:27 PM.. Reason: add code tags (the # button) |
|
||||
|
Re: Looping statement problem : TOTAL Course fee
Alright, you're not too far away from what I wrote.
![]() Code:
if( printf("RM%.2f", courseFee) < 0 ) exit(1);
In your code, the count variable in the for loop should be checked if it's less than (<) year, not less than or equal to (<=), since it starts at zero. That's a common error known as an "Off by one" error. Also, you should iterate count with "count++" instead of "count+=1" since it's easier to write and read. ![]() The main problem you're having is that coursefee is being multiplied by 0.5, which is half of what it started at, and you're not updating coursefee, so it'll always be the same value. Instead of having an intermittent "total" value, you should instead just do what I did, using *= 1.05 to get the desired effect of increasing coursefee by 5 percent. Also, your "all" variable should be added before changing coursefee (since it starts at 10000 and you don't want to start "all" off at 10500).
__________________
On Hiatus... |
|
|||
|
Re: Looping statement problem : TOTAL Course fee
i still had the problem for the total course fee..
here's my code: Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void main()
{
int numYears;
double courseFee=10000, totalFee;
double grandFee;
int iii;
char name[40], id[20];
printf("----------------\n");
printf("USER INFORMATION\n\n");
printf("----------------\n");
printf("Enter name:");
gets(name);
printf("Student id:");
gets(id);
printf("\n\nDuration of study(year):");
scanf("%d",&numYears);
printf("\n\n----------------------------------\n");
printf("JAPAN MALAYSIA TECHNICAL INSTITUTE\n");
printf("----------------------------------\n");
printf("\nYear\t\tCourse Fee\n");
for (iii = 1; iii <= numYears; ++iii)
{
if( printf("\n%d\t\tRM%.2f",iii, courseFee) < 0 ) exit (1);
totalFee += courseFee;
courseFee *= 1.05;
grandFee+=courseFee;
}
printf("\n\nTOTAL\n");
printf("-------\n");
printf("Total Course Fee:\nRM%.2lf\n\n",grandFee);
}
Last edited by WingedPanther; 09-12-2009 at 05:28 PM.. Reason: add code tags (the # button) |
|
||||
|
Re: Looping statement problem : TOTAL Course fee
Please, for future reference, place your code in between [code] and [/code] tags, to make reading your code easier and to maintain whitespace, similarly to how I had it. You can use the # symbol on the top of the reply box to insert those tags.
Now to the answer: First, I highly discourage the use of void main, and I don't care what your professor said. Use int main in every situation, it's standard, and it will work on all computers without any problems. At the end of main you'll have to "return 0;", no big deal, just do it. Second, what is grandFee for? I don't see what it accomplishes except to cause you problems, if you eliminate it entirely, and instead of printing grandFee on the bottom you print totalFee (which should be the right amount) you'll be fine. grandFee is getting the courseFee added to it AFTER your 5% addition to courseFee, which makes it so grandFee is greater than totalFee. Here, let me show you more clearly, assuming 3 years: Code:
courseFee = 10000; // year 1: totalFee += courseFee; // totalFee == 10000 courseFee *= 1.05; // courseFee == 10500 grandFee += courseFee; // grandFee == 10500 // year 2: totalFee += courseFee; // totalFee == 20500 courseFee *= 1.05; // courseFee == 11025 grandFee += courseFee; // grandFee == 21750 // year 3: totalFee += courseFee; // totalFee == 31750 - CORRECT! courseFee *= 1.05; // courseFee == 11576.25 grandFee += courseFee // grandFee == 33326.25 - INCORRECT! printf(Total Course Fee:\nRM%.2f\n\n", grandFee); // Will end up printing "33326.25", not "31750"!
__________________
On Hiatus... |
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | Search this Thread |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| [Win7] Problem Steps Recorder | Termana | Tutorials | 1 | 04-14-2009 10:24 AM |
| Looping problem ( C ) | lithiummethoxide | C and C++ | 7 | 03-22-2009 09:08 PM |
| If Statement Problem | .Sin | Visual Basic Programming | 3 | 03-08-2009 09:53 AM |
| can someone help me turn regular code into a switch statement?? | Hawk1 | C and C++ | 3 | 10-18-2008 01:05 PM |
| Help! Rlink32 Error | Sparky | Pascal/Delphi | 6 | 10-16-2008 11:31 AM |
All times are GMT -5. The time now is 09:29 AM.
Amrosama.cc
Arekbulski.cc
Debtboy.cc
Guest.cc
Jaan.cc
James.cc
Mathx.cc
Tsz.cc
Vswe.cc