Problem with setprecision

Hey guys,

I've made a code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
 
//code code
cout<<"\nBefore we start, to how many numbers should the result be rounded? ";
    int round;
    cin >> round;
//code code
cout << "\n****** Time to do some basic math!******" << endl;
         cout << "_________Addition & Substraction________" << endl;
         cout << endl;
         cout << "Enter number 1: ";
         cin >> num1;
         cout<<"\nEnter number 2: ";
         cin >> num2;
         
         result = num1 + num2;
         cout<<num1<<" + "<<num2<<" = "<<result<<endl;
         cout<<"The rounded result = "<<setprecision(round)<<result<<endl;
//code code 


So basicly what this part of the code does is (1) ask the user to how many numbers the result should be rounded. And (2) show the result, rounded to the amount of numbers given by the number in 1.

However when I execute it I get something like this:


Before we start, to how many numbers should the result be rounded? 2

Enter number 1: 1234565
Enter number 2: 2334
1.2e+006 + 2.3e+003 = 1.2e+006
The rounded result = 1.2e+006


Whereas, before I aded the part with
1
2
3
 cout<<"\nBefore we start, to how many numbers should the result be rounded? ";
    int round;
    cin >> round;
That wouldn't happen, and the numbers would be displayed normally (yet rounded properly where it's supposed).

So my question is: how can I make the user choose to how many numbers they want the results to be rounded? (or what's wrong in what I did here?)


I'm not going to post the full code, because that is about 1200 lines... (which don't add anything extra to this post :)


Thanks!
Last edited on
Well it would be helpful to see some more code. Why not write a simple, compilable function that demonstrates the problem so that we can run it? Somehow you are in scientific notation mode but I don't see any code that would have caused that. How did that happen? You don't need to post the original program. You copy and paste the relevant parts into a new project and modify it to duplicate the problem and study the results of the set precision.
I know that using system is bad, however, this is just a temporary sollution for this compilable program, in the full code i've done something else :)
Note that in the switch case there's only one case, because posting the other ones wouldn't make a big difference :)

I hope this will do (otherwise please tell me :)


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
50
51
52
53
54
55
56
57
#include <iostream>
#include <iomanip>
using namespace std;
int main(int argc, char * argv[])
        
{
    double num1, num2, result;
    int choice;
    char choice1, choice2, choice9;
    char y, n, Y, N;
cout<<"\nBefore we start, to how many numbers should the result be rounded? ";
    int round;
    cin >> round;
while((choice1 != 'n')||(choice1 !='N'))
    {
    
    //welcome the user and header
    cout << "****** Time to do some basic math!******" << endl;
    cout << "________________________________________" << endl;
    cout << endl;
    //give options
    cout<<"\nPress:\n*\t1 to add and substract: ";
	cin>>choice;

	switch(choice)
	{
//.......................Adding + Substracting.....................................                  
	case 1:
         begin1:
         cout << "\n****** Time to do some basic math!******" << endl; //subheader
         cout << "_________Addition & Substraction________" << endl;  //subheader
         cout << endl;
         cout << "Enter number 1: ";
         cin >> num1;
         cout<<"\nEnter number 2: ";
         cin >> num2;
         // the function for additions/ substractions
         result = num1 + num2;
         cout<<num1<<" + "<<num2<<" = "<<result<<endl;
         cout<<"The rounded result = "<<setprecision(3)<<result<<endl;

         break;
 default:
		cout<<"Invalid input."<<endl;
      }
system("pause");
system("cls");
cout << "Would you like to continue using this calculator? (y/n) "; //continue calc?
cin >> choice1;
if ((choice1 == 'n')||(choice1 =='N')) //if no, terminate program
{
        return 0;
}
}
return 0;

}
Topic archived. No new replies allowed.