Hey how do I do this...

Round to 2 decimal places... Ill show you the code quick...
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
#include<iostream>
using namespace std;
int main()
    {
          double barack = 0;
          double mccain = 0;
         
          int x;
          int y;
          int vote = 0;
          Vote:
          system("cls");
          Votey:
          cout << "Who will you vote for???" << endl << "(R)Mccain, (D)Obama...\n" << "1 for Mccain, 2 for Obama\n";
          cin >> x;
          cout << "\n\n";
          system("cls");
          if (x==1){
                    mccain = mccain + 1;
                    vote = vote + 1;
                    cout << "You have voted for John Mccain(R)!!!\n\n";}
                    
                    
          else if (x==2){ 
                    barack = barack + 1;
                    vote = vote + 1;
                    cout << "You have voted for Barack Obama(D)!!!\n\n";}
          else {
               cout << "Error... enter 1 or 2, corresponding to choice...\n";
               goto Votey;
               }
          cout << "      Mccain   Obama" << endl;
          cout << "#       " << mccain << "       " << barack << endl;
          cout << "%      " << Setprecision((mccain/vote * 100),2) << "      " << 
          setprecision(2) << (barack / vote * 100)  << endl;
          
          cout << "\n\nWould you like to vote again???" << endl;
          cout << "Press 5 to end or any other number to keep going" << endl;
          cin >> y;
          if (y==5)
          goto end;
          else 
          goto Vote;
          
          end:
         return 0;
          }
                    

I just cant get that printing of percentages going correctly... thx much
Instead of using setprecision, you should use type casting. Basically what static_cast<int> says is that when those numbers are divided and multiplied only show the whole numbers and not the decimal places. So, for example, if the percentage is 67.4 it only shows 67.

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
#include<iostream>
using namespace std;
int main()
    {
          double barack = 0;
          double mccain = 0;
         
          int x;
          int y;
          int vote = 0;
          Vote:
          system("cls");
          Votey:
          cout << "Who will you vote for???" << endl << "(R)Mccain, (D)Obama...\n" << "1 for Mccain, 2 for Obama\n";
          cin >> x;
          cout << "\n\n";
          system("cls");
          if (x==1){
                    mccain = mccain + 1;
                    vote = vote + 1;
                    cout << "You have voted for John Mccain(R)!!!\n\n";}
                    
                    
          else if (x==2){ 
                    barack = barack + 1;
                    vote = vote + 1;
                    cout << "You have voted for Barack Obama(D)!!!\n\n";}
          else {
               cout << "Error... enter 1 or 2, corresponding to choice...\n";
               goto Votey;
               }
          cout << "      Mccain   Obama" << endl;
          cout << "#       " << mccain << "       " << barack << endl;
          cout << "%      " << static_cast<int>(mccain/vote * 100) << "      "    //type casting with static_cast
			   << static_cast<int>(barack / vote * 100)  << endl;                 //type casting with static_cast   
          
          cout << "\n\nWould you like to vote again???" << endl;
          cout << "Press 5 to end or any other number to keep going" << endl;
          cin >> y;
          if (y==5)
          goto end;
          else 
          goto Vote;
          
          end:
         return 0;
          }
closed account (z05DSL3A)
If you want 2 decimal places you will need to set the precision to four:

1
2
3
4
    cout.precision(4); 
    cout << "      Mccain   Obama" << endl;
    cout << "#       " << mccain << "       " << barack << endl;
    cout << "%      " << (mccain/vote * 100) << "      "  << (barack / vote * 100)  << endl;
Last edited on
Topic archived. No new replies allowed.