Hello Daniweb, I'm new to C and I need help with this basic calculator. It's supposed to calculate investment, only it never gives me the correct result Here is the source,

``````#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <math.h>

int main()
{
double result;
int a;
int b;
int c;

printf("Enter the amount of money to be invested:");
scanf("%d",  &a);
getchar();
printf("Enter the percentage");
scanf("%d",  &b);
getchar();
printf("Enter the period of investment (years)");
scanf("%d",  &c);
getchar();
result = a * pow (2.71828183, b * c);
printf("You will end up with %f", result);

return 0;

}``````

Sorry for the double post but I wasn't allowed to edit my original.

Anyways here's my edited source.

``````#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <windows.h>
#include <math.h>

int main()
{
float result;
int money;
float percent;
float year;
float e = 2.71828183;

printf("Enter the amount of money:");
scanf("%d", &money);
getchar();

printf("Enter percentage:");
scanf("%d", &percent);
getchar();

printf("Enter years:");
scanf("%d", &year);
getchar();

result = money * pow (e, percent * year);
printf("%f", result);

Sleep(4000);
return 0;
}``````

What's with all the `getchar()` lines?

What are the values just before the calculation? Print the values.

The getchar()'s are there to pull newlines off the keyboard buffer. In this case, since the program is scanf()'ing for numbers, the getchar()'s are not needed.

Scanf() will jump over a newline, if it is looking for a number.

Money should not be an integer, when the user enters it. It should be a float or double. Then, you can multiply money times 100 to deal with it as an integer, as all pennies. Sounds odd, but it works *really* well.

If percentage is a float, it should not be scanf()'d in with a %d - you know that's haywire! Same mistake with year variable. :(

You know that "percent means hundredths" (8.5% == 0.085), but I'm not sure all your user's know that. You might want to take in the percent as just an 8.5 kind of a float, and then divide it by 100, using your program, instead of relying on the user's math skills.

Make those changes, and see how it works.

Thank you both for your reply, I just started C about a day ago :), so I'm still getting used to %d %f %s, so on, but I made some of the changes you asked and here is the source.

``````#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <math.h>

int main()
{
// PERT  Money * E ^ Rate * Time
// PERT Example 1000  * 2.71828183 ^ .05 * 10
double result;
float money;
float rate;
float time;
float e = 2.71828183;

printf("Enter the amount of money:");
scanf("%f", &money);

printf("Enter percentage:");
scanf("%f", &rate);
rate / 100;

printf("Enter the period of investment (years):");
scanf("%f", &time);

result = money * pow (e, rate * time);
printf("%f", result);
Sleep(5000);
return 0;
}``````
``````#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <math.h>

int main()
{
// PERT  Money * E ^ Rate * Time
// PERT Example 1000  * 2.71828183 ^ .5 * 10
double result;
float money;
float drate;
float time;
float e = 2.71828183;
float rate;

printf("Enter the amount of money:");
scanf("%f", &money);

printf("Enter percentage:");
scanf("%f", &drate);
rate = drate / 100;

printf("Enter the period of investment (years):");
scanf("%f", &time);

result = money * pow (e, rate * time);
printf("%f\n", result);

return EXIT_SUCCESS;
}``````

Still not working correctly. Again I'm terribly sorry for this double or quadroople posting, but I'm not allowed to edit any previous posts.

Never mind! It works! Awesome job! Thank you :)!

Still not working correctly. Again I'm terribly sorry for this double or quadroople posting, but I'm not allowed to edit any previous posts.

It is perfectly fine to post any updated code again.
Once a post is summited there's only a forty-minutes window of opportunity for editing.

Never mind! It works! Awesome job! Thank you :)!

Before you celebrate your achieved success, I encourage you to enter a char or a whole stream of characters as input. Observe the behavior of your program.

Before you celebrate your achieved success, I encourage you to enter a char or a whole stream of characters as input. Observe the behavior of your program.

Right, I tried many cases, it came out fine. Thanks again :).

Be kind, Aia - she's only been programming for a few days.

Be kind, Aia - she's only been programming for a few days.

Agree. Just because the experts know about input problems, the purpose of this code is not to be bulletproof, but to learn mathematical concepts.

Right, I tried many cases, it came out fine. Thanks again :).

That's not what I get when I run you code.

Enter the amount of money:five bucks
Enter percentage:Enter the period of investment (years):0.000000

Now that you have learned how to make use of the & and the % in the scanf() function. ( That's just about what you get out of using scanf() for reading input from stdin )
It would be a deserver not to let you know that there are a lot of issues by using it. So many in fact, that the use of it for teaching purposes I place it at the same level of the use of gets()

Ask if you want to know more.

That's not what I get when I run you code.

Now that you have learned how to make use of the & and the % in the scanf() function. ( That's just about what you get out of using scanf() for reading input from stdin )
It would be a deserver not to let you know that there are a lot of issues by using it. So many in fact, that the use of it for teaching purposes I place it at the same level of the use of gets()

Ask if you want to know more.

Sorry, I didn't know what you mean, right five bucks wouldn't work. Would I have to use a string for that?

As I said, don't worry about it. Your task is not to learn how to bullet proof your input but to simply use proper input correctly.

Bulletproofing comes later in the course -- at least it better.

As I said, don't worry about it. Your task is not to learn how to bullet proof your input but to simply use proper input correctly.

Bulletproofing comes later in the course -- at least it better.

Hopefuly you will be here to help me out when I'm ready for it then.

Sorry, I didn't know what you mean, right five bucks wouldn't work. Would I have to use a string for that?

Eventually, obtaining the input as a string and parsing that string for the desired data would be the way of doing it.
Right now, the issue is that you do not check the return of scanf() for a minimum confirmation that it was successful converting the desired data type, before you make use of the variable.

I see people that accept the notion that it is OK to disregard the proper concern for check the return value of functions, "just because they are learning"
And many learning the language never get aware of what that is.

scanf()"on success, returns the number of items successfully read. This count can match the expected number of readings or fewer, even zero, if a matching failure happens.
In the case of an input failure before any data could be successfully read, EOF is returned."

You can use an int to receive that return.

``````int result = 0;
int number = 0;

result = scanf("%d", &number);``````

Now, before you use the variable number for something, you can be sure if it was able to receive data that could convert into an integer hold in variable number.

``````if result == 1;
/* success, I can use number */
else
/* no joy, program can not use number */``````

Guess what?
With that principle you can use a loop instead, to control the flow of the program until the right data is provided.

``````printf("Enter a number: ");
fflush(stdout) /* display the printf statement */
/*
* scanf() doesn't need an int assignment
* because while picks it for the evaluation
*/
while ( scanf("%d", &number) != 1 )
{
/* if we get here no proper integer was entered */
printf("Only numbers are allowed\n");
/* loop and ask again */
}``````

Of course, that doesn't clear the data that is still waiting in the standard input stream, if more that one character was entered and the while loop will spin many time until it empties.

To prevent that, a popular concoction would be this:

``````while ( scanf("%d", &number) != 1 )
{
int ch;
/* clear the stream behind so a new input can be entered */
while ( (ch = getchar()) != '\n' && ch != EOf ) ; /* the semicolon is on purpose */
printf("Only numbers are allowed\n");
/* loop and ask again */
}``````

While that doesn't "fool-proof" the proper functioning of your program, it does give it a better chance of getting the proper data.
These are some of the hurdles that you have to jump to make use of scanf()
And it is appropriated to know about it even from day one of learning, contrary to other opinion that you have heard already.

Being aware of it, already makes you better than many, even if you can not grasp the whole of it at this point.

Wow Aia, thank you so much. You're right, I should have learned it, and I can't fully understand what scanf() is able to do, but you explained a lot, and I'll be sure to use it. It makes much sense.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.