Doing some little calculation and tabulating the answers

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;

int price;
const float fivepercent (0.05);
const float tenpercent (0.1) ; 
const float 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;
	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");
	
}
Last edited on
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.
1
2
3
4
while(price != -1){

// Allow user input and display output
}
Last edited on
Thanks @Henry, inputting
1
2
3
4
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.
Last edited on
Show your latest code.
Last edited on
Here is the latest code...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;

int price;
const float fivepercent (0.05);
const float tenpercent (0.1) ; 
const float 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;
	cout<<fixed<<setprecision(2);
	cin>>price;
	 
   n = price * fivepercent;
   m = price * tenpercent;
   a = price * fifteenpercent;
   
    while (price != -1)
     
   {
   
   cout<<setw(27)<<n<<setw(21)<<m<<setw(21)<<a<<endl;
   
}

	system("pause");
	
}


And the output is generating repeated values (answers)
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().



Last edited on
Thanks AbstractionAnon, it seems I now have something more presentable. Here is the latest code...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;

int price;
const float fivepercent (0.05);
const float tenpercent (0.1) ; 
const float 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!
Last edited on
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!
Last edited on
Really, you can't figure this out?
1
2
3
4
5
6
7
8
9
10
11
12
// 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; 

Okay, here is my latest code.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;

int price (0);
const float fivepercent (0.05);
const float tenpercent (0.1) ; 
const float 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!
Last edited on
Here is the output

   MAXWELL MIXER



   QTC     SHEET


   Please Enter The Price of Each Material to Calculate its Required Percentage:


Price                   5%                  10%                  15%
________________________________________________________________________
500
                      25.00                50.00                75.00
________________________________________________________________________
470
                      23.50                47.00                70.50
________________________________________________________________________
5425
                     271.25               542.50               813.75
________________________________________________________________________
875
                      43.75                87.50               131.25
________________________________________________________________________
900
                      45.00                90.00               135.00
________________________________________________________________________
745
                      37.25                74.50               111.75
________________________________________________________________________
867
                      43.35                86.70               130.05
________________________________________________________________________
140
                       7.00                14.00                21.00
________________________________________________________________________
470
                      23.50                47.00                70.50
________________________________________________________________________
-1
                      -0.05                -0.10                -0.15
________________________________________________________________________
________________________________________________________________________
Total               519.55              1039.10              1558.65


The -1 is affecting the total.
Last edited on
Lines 34-42 should not be executed if price is -1.

34
35
36
37
38
39
40
41
42
    if (price != -1)
    {   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;
    }

Is this what you mean please?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;

int price (0);
const float fivepercent (0.05);
const float tenpercent (0.1) ; 
const float 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;

   if (price != -1)
   {
   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");
}}


If yes, the output is not interesting. Check and edit it once and for all please. Thanks a lot!
Last edited on
edit it once and for all please

It's not my job to write every line of code for you. You need to figure out what is wrong.

Lines 44-48 are INSIDE your while loop. They should be AFTER your while loop.
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?
Topic archived. No new replies allowed.