Go Back   CodeCall Programming Forum > Software Development > C and C++
Register Blogs Search Today's Posts Mark Forums Read

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.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 09-12-2009, 12:32 AM
Newbie
 
Join Date: Sep 2009
Posts: 5
syarif20 is an unknown quantity at this point
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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 09-12-2009, 12:57 AM
ZekeDragon's Avatar
Code Warrior
 
Join Date: Jul 2009
Location: Nowhere, Washington
Posts: 1,721
ZekeDragon is a name known to allZekeDragon is a name known to allZekeDragon is a name known to allZekeDragon is a name known to allZekeDragon is a name known to allZekeDragon is a name known to all
Send a message via AIM to ZekeDragon Send a message via MSN to ZekeDragon Send a message via Skype™ to ZekeDragon
Re: Looping statement problem : TOTAL Course fee

What code do you have so far, it helps to give us what you've got so far?
__________________
On Hiatus...
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 09-12-2009, 12:59 AM
Newbie
 
Join Date: Sep 2009
Posts: 5
syarif20 is an unknown quantity at this point
Re: Looping statement problem : TOTAL Course fee

the calculation is not correct..
total course fee is same as the last fee.

does i need to use nested loop?

i need to program with C
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 09-12-2009, 01:12 AM
ZekeDragon's Avatar
Code Warrior
 
Join Date: Jul 2009
Location: Nowhere, Washington
Posts: 1,721
ZekeDragon is a name known to allZekeDragon is a name known to allZekeDragon is a name known to allZekeDragon is a name known to allZekeDragon is a name known to allZekeDragon is a name known to all
Send a message via AIM to ZekeDragon Send a message via MSN to ZekeDragon Send a message via Skype™ to ZekeDragon
Re: Looping statement problem : TOTAL Course fee

Do you have any code? I'd make a double called "courseFee" and "totalFee", then assign courseFee 10000. After that, I'd make a for loop to iterate over each year, however many years that is, and you multiply "courseFee" by 1.05, and then += that value to totalFee, and print out courseFee on each iteration. Something similar to this:
C Code:
  1. double courseFee, totalFee;
  2. int iii;
  3. courseFee = 10000;
  4. for (iii = 0; iii < numYears; ++iii) {
  5.     if( printf("RM%.2f", courseFee) < 0 ) exit (1);
  6.     totalFee += courseFee;
  7.     courseFee *= 1.05;
  8. }
What do you have so far?
__________________
On Hiatus...
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 09-12-2009, 01:17 AM
Newbie
 
Join Date: Sep 2009
Posts: 5
syarif20 is an unknown quantity at this point
Re: Looping statement problem : TOTAL Course fee

could you explain this line?
Code:
    if( printf("RM%.2f", courseFee) < 0 ) exit (1);
here's my code:
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)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 09-12-2009, 01:30 AM
ZekeDragon's Avatar
Code Warrior
 
Join Date: Jul 2009
Location: Nowhere, Washington
Posts: 1,721
ZekeDragon is a name known to allZekeDragon is a name known to allZekeDragon is a name known to allZekeDragon is a name known to allZekeDragon is a name known to allZekeDragon is a name known to all
Send a message via AIM to ZekeDragon Send a message via MSN to ZekeDragon Send a message via Skype™ to ZekeDragon
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);
What this code does is simple, it prinfs the string "RM" then follows it with the float (double) value of courseFee with two numbers after the decimal point. The "%" tells printf that it's an argument character (one of the arguments that follow it on printf), then the .2 means print two and only two numbers after the decimal, then "f" means that the value to expect is a float or double. The reason it's inside an if statement is to check printf's return value, which could be negative, and if it is that means it failed to print. If it failed to print, you want the program to quit with status 1 (EXIT FAILURE).

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...
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 09-12-2009, 01:37 AM
Newbie
 
Join Date: Sep 2009
Posts: 5
syarif20 is an unknown quantity at this point
Re: Looping statement problem : TOTAL Course fee

waaa....thanks you so much! i appreaciated the explaination so much..my problem solved now..

wish you luck and have a good day!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 09-12-2009, 04:06 AM
Newbie
 
Join Date: Sep 2009
Posts: 5
syarif20 is an unknown quantity at this point
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)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9 (permalink)  
Old 09-12-2009, 05:15 AM
ZekeDragon's Avatar
Code Warrior
 
Join Date: Jul 2009
Location: Nowhere, Washington
Posts: 1,721
ZekeDragon is a name known to allZekeDragon is a name known to allZekeDragon is a name known to allZekeDragon is a name known to allZekeDragon is a name known to allZekeDragon is a name known to all
Send a message via AIM to ZekeDragon Send a message via MSN to ZekeDragon Send a message via Skype™ to ZekeDragon
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"!
Finally, for the love of all that is holy don't use gets()! You know how to use scanf, AT LEAST use that instead, but you really should use fgets() over any other! You'll need to parse the string yourself, but trust me, you'll be better off that way.
__________________
On Hiatus...
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
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.


vBulletin v3.8.0 ©2010, Jelsoft Enterprises Ltd.


no new posts

LinkBacks Enabled by vBSEO 3.1.0