Beginner C/C++

Moderators: None (Apply to moderate this forum)
Number of threads: 5428
Number of posts: 16943

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Help with C, loops Posted by bobbismal on 11 Sept 2009 at 12:40 PM
I have to write a program that calculates simple interest. Then loops and does it again unless you input "-1". It works fine, but it should stop as soon as you input "-1", and it keeps going until the the end of the program.



#include <stdio.h>


/* function main begins program execution */
int main( void )
{
float principal; /* define principal variable */
float itr; /* define interest variable */
int tm; /* define term in days variable */
float rtch; /* define interest charge variable */

while ( principal != -1 ){
/* input info */
printf( "Enter loan principal (-1 to end): " );
scanf_s( "%f", &principal );
printf( "Enter interest rate:" );
scanf_s( "%f", &itr );
printf( "Enter term of the loan in days: " );
scanf_s( "%d", &tm );


/* calculate */
rtch = (principal * itr * tm) /365;
printf( "The interest charge is $%.2f\n\n\n", rtch );}


return 0;

} /* end */

BANG HEAD ON DESK
Report
Re: Help with C, loops Posted by Lundin on 13 Sept 2009 at 11:48 PM
Seems to work fine, except a few problems:

- You are using some non-standard scanf, it isn't clear whether this flushes the input buffer properly or not. Had it been standard scanf(), you would have to write a getchar() after each call, to remove the line feed character that is put into stdin when the user presses enter.

- You are using int, you should use signed int. It is implementation-defined whether "int" is signed or not, it depends on the compiler.
Report
Re: Help with C, loops Posted by toddlerasim on 22 Sept 2009 at 12:16 AM
#include <stdio.h>

#include <process.h>
/* function main begins program execution */
int main( void )
{
float principal; /* define principal variable */
float itr; /* define interest variable */
int tm; /* define term in days variable */
float rtch; /* define interest charge variable */

while ( principal != -1 ){
/* input info */
printf( "Enter loan principal (-1 to end): " );
scanf_s( "%f", &principal );
if(principal==-1)
{
   exit(0);
}
printf( "Enter interest rate:" );
scanf_s( "%f", &itr );
printf( "Enter term of the loan in days: " );
scanf_s( "%d", &tm );


/* calculate */
rtch = (principal * itr * tm) /365;
printf( "The interest charge is $%.2f\n\n\n", rtch );}


return 0;

} /* end */



That should solve your problem.

But then I am just a toddler in C/C++.
Report
Re: Help with C, loops Posted by Lundin on 22 Sept 2009 at 2:08 AM
There is no header called process.h in the C language.

exit() is found in stdlib.h
Report
Re: Help with C, loops Posted by toddlerasim on 22 Sept 2009 at 8:40 AM
You are right about exit() being found in stdlib.h. Unfortunately, Turbo C++ does have it and there you have the choice of either stdlib or process.h. I blundered and put process.h instead of stdlib.h.

Many thanks for pointing that out.
Report
Re: Help with C, loops Posted by toddlerasim on 22 Sept 2009 at 8:45 AM
This post was posted twice. Wonder how and why.
Report
Re: Help with C, loops Posted by toddlerasim on 22 Sept 2009 at 8:46 AM
Post deleted for the reason given in my second post.



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.