Hi guys,
Pls, I've been trying to write a programming code that will calculate different percentage (5%, 10%, 15%) of different amount of money inputted by the user such that as the amount is inputted by the user, immediately the "Enter" button is click, the code calculates the 5%, 10% and 15% of the amount inputted,tabulating the result in line with the amount inputted and the cursor shifts to next column of amount,the user input another value (amount), click enter, calculate and tabulate....like that like that.
Here is the code I've written so far...pls help
What problem are you having? Please be specific. Your code appears to run.
If you want to user to be able to enter another amount, then you need a loop.
PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post. http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.
Thanks for your quick response. Yeah! I want the user to enter another amount, and as the user is entering the several amounts, the code calculate it and tabulate its percentage. I want the user to enter as many amounts as possible until probably when a value is entered, say -1, to break the (percentage) calculations and total (addition) of every calculated (percentage) value is displayed at the bottom of each tabulated percentage, that is, 5%, 10%, 15%. I know a loop will be used, I know a sentinel value will probably be used also. I know there are still some codes to be written. This my code is not sufficient enough to do what's required. But I don't know which way to go anymore. Pls help.
while(price != -1){
// Allow user input and display output
}
doesn't let the user to input another amount. It's generating a repeated (calculated percentage) figures automatically as the output (on the screen) and doesn't stop. The "while" loop above seems not enough. Pls help.
And the output is generating repeated values (answers)
Well, yeah. What you have in your while loop is only a cout statement.
That's not what hengry suggested.
Note his comment with emphasis on input:
// Allow user input and display output
Lines 19 through 32 should be within the while loop.
Note that if you put that while statement after line 18, you should initialize price to something other than -1. Otherwise you are going to be testing an uninitialized variable the first time through the loop with undefined results.
Lines 7,11-13: It's best to avoid the use of globals. There is no reason these variables can not be declared inside main().
#include <iostream>
#include <iomanip>
#include <cmath>
usingnamespace std;
int price;
constfloat fivepercent (0.05);
constfloat tenpercent (0.1) ;
constfloat fifteenpercent (0.15);
float n;
float m;
float a;
int main()
{
cout<<"\n\n MAXWELL MIXER\n\n\n\n\n"<<endl;
cout<<" QTC\tSHEET\n\n"<<endl;
cout<<" Please Enter The Price of Each Material to Calculate its Required Percentage:"<<endl;
cout<<"\n\nPrice"<<setw(21)<<"5%"<<setw(21)<<"10%"<<setw(21)<<"15%"<<endl;
while (price != -1)
{
cout<<fixed<<setprecision(2);
cin>>price;
n = price * fivepercent;
m = price * tenpercent;
a = price * fifteenpercent;
cout<<setw(27)<<n<<setw(21)<<m<<setw(21)<<a<<endl;
}
system("pause");
}
It seems to be tabulating the answers neatly now. But I'll be glad if you can help me to edit the code to make it more and more professional. And I don't know if it's possible to do the the addition of each column and when the -1 is inputted, the sum of the calculated values will be presented or displayed under each column. If it's possible, can you please help me to edit it to it.
Thanks to everyone!
But I'll be glad if you can help me to edit the code to make it more and more professional.
Two points I made before which you have ignored:
1) n, m, and a should NOT be globals. They should be locals in main().
2) price is an uninitialized variable. The first time through the loop, at line 23 you're testing an uninitialized variable. The result will be undefined behavior.
One additional point. n, m, and a are not meaningful variable names. Use meaningful variable names.
And I don't know if it's possible to do the the addition of each column and when the -1 is inputted, the sum of the calculated values will be presented or displayed under each column.
Of course it is possible. You need to allocate and initialize variables to hold the totals. Each time through the loop, add to the totals. Then after the loop print the totals.
Yeah! I got all your points now. Thanks so much AbstractionAnon. I've done the two points above but my major problem now is that can you please help me to translate this your statement into code that I'll add to my code please.
You need to allocate and initialize variables to hold the totals. Each time through the loop,
add to the totals. Then after the loop print the totals.
Please assist me in putting down the required code to make things easier for me. Thanks!
// Before your loop, allocate and initialize the variables
float tot5 = 0;
float tot10 = 0;
float tot15 = 0;
// In your loop, add to the totals
tot5 += n;
tot10 += m;
tot15 += a;
// After your loop, print the totals
cout<<setw(27)<<tot5<<setw(21)<<tot10<<setw(21)<<tot15<<endl;
#include <iostream>
#include <iomanip>
#include <cmath>
usingnamespace std;
int price (0);
constfloat fivepercent (0.05);
constfloat tenpercent (0.1) ;
constfloat fifteenpercent (0.15);
float calc5;
float calc10;
float calc15;
float tot5 (0);
float tot10 (0);
float tot15 (0);
int main()
{
cout<< "\n\n MAXWELL MIXER\n\n\n"<<endl;
cout<<" QTC\tSHEET\n\n"<<endl;
cout<<" Please Enter The Price of Each Material to Calculate its Required Percentage:"<<endl;
cout<<"\n\nPrice"<<setw(21)<<"5%"<<setw(21)<<"10%"<<setw(21)<<"15%"<<endl;
while (price != -1)
{
cout<<"________________________________________________________________________"<<endl;
cout<<fixed<<setprecision(2);
cin>>price;
calc5 = price * fivepercent;
calc10 = price * tenpercent;
calc15 = price * fifteenpercent;
tot5 += calc5;
tot10 += calc10;
tot15 += calc15;
cout<<setw(27)<<calc5<<setw(21)<<calc10<<setw(21)<<calc15<<endl;
}
cout<<"________________________________________________________________________"<<endl;
cout<<"________________________________________________________________________"<<endl;
cout<<"Total"<<setw(21)<<tot5<<setw(21)<<tot10<<setw(21)<<tot15<<endl;
system ("pause");
}
The only problem I'm having now is that when the sentinel value (-1) is entered, the code still calculates for the percentage of -1, tabulates it and add to my overall sum of the above calculated values thereby altering the correct answer for the sum.
Meanwhile I've tried what you said that
1) n, m, and a should NOT be globals. They should be locals in main().
2) price is an uninitialized variable. The first time through the loop, at line 23 you're testing an
uninitialized variable. The result will be undefined behavior.
This same problem is still persisting even if done. Check the codes please. What do you think is needed to solve this issue.
Thanks for your great contributions!
Waw! Finally got it!!! Thanks so much AbstractionAnon. You're such a blessing to this forum. Keep it up! Thanks to everyone that contributed. You're blessed!
I don't know if it's possible to run it on Android as an Android App, such that I can just open the already executable code on my Android and works just like normal App. I have the C4droid I used to code on my Android phone I do use if I'm bored to use computer. But the C4droid works just like normal DevC++, it doesn't convert the executable file to an Android App. Perhaps I need to download a software, install it and copy my source code to its development environment to make it work as an Android App. Any idea on this please?