6 Replies - 3936 Views - Last Post: 08 July 2012 - 10:12 PM Rate Topic: -----

#1 robertc  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 20
  • Joined: 29-June 12

Format output to display 2 decimal places

Posted 08 July 2012 - 05:20 PM

i am very new to programing. I am taking an intro to c++ class and my current assignment asks to make a simultaneous equation solver. I'm pretty close to finishing it but i can't seem to figure out how to display the output to have 2 decimal places. If anybody can help me figure that out it would be awesome


#include <iostream>
#include <string>
using namespace std;

int main() {
    //initialize variables
    int a1;
    int b1;
    int c1;
    int a2;
    int b2;
    int c2;
    double x;
    double y;
    
    
    //prompt and get input
    cout << "Welcome to Robert's Simultaneous Equation Solver! " << "\n";
    cout << "Please enter a1, b1, c1 (seperated by spaces): ";
    cin >> a1; cin >> b1; cin >> c1;
    cout << "Please enter a2, b2, c2 (seperated by spaces): ";
    cin >> a2; cin >> b2; cin >> c2;
    
if ((a1 == a2) &&
    (b1 == b2) &&
    (c1 == c2)) { cout << "Your inputs create identical lines."; }
else if ((a1 * b2 - a2 * b1) == 0) { cout << "Your inputs create parallel lines.";
     }
else 
    {
     
    //do math
    x = ( c1 * b2 - c2 * b1 ) / ( a1 * b2 - a2 * b1 ); 
    y = ( a1 * c2 - a2 * c1 ) / ( a1 * b2 - a2 * b1 ); 
    
    //output
    cout << "The solutions to the equations: " << "\n"; 
    cout << a1 << "x + " << b1 << "y = " << c1 << "\n"; 
    cout << a2 << "x + " << b2 << "y = " << c2 << "\n";  
    cout << "is x = " << x << " and y = " << y;
 
    
    //pause and exit
    getchar();
    getchar();
    return 0;
    }}




needs to do this

Welcome to Robert’s Simultaneous Equation Solver.
Please enter a1, b1, c1 (separated by spaces): input: 1 2 2
Please enter a2, b2, c2 (separated by spaces): input:1 -2 -12
The solution of the equations:
1x + 2y = 2
1x + -2y = -12
is x = -5.00 and y = 3.50

Is This A Good Question/Topic? 0
  • +

Replies To: Format output to display 2 decimal places

#2 Aphex19  Icon User is offline

  • Born again Pastafarian.
  • member icon

Reputation: 614
  • View blog
  • Posts: 1,873
  • Joined: 02-August 09

Re: Format output to display 2 decimal places

Posted 08 July 2012 - 05:51 PM

See std::setprecision. You could also use (the C style) printf.

e.g.
float x = 5.3643;

// round x to 2 decimal places
std::printf("%.2f", x);


Mixing C and C++ styles might not be best though, depending on your viewpoint.

This post has been edited by Aphex19: 08 July 2012 - 05:55 PM

Was This Post Helpful? 0
  • +
  • -

#3 robertc  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 20
  • Joined: 29-June 12

Re: Format output to display 2 decimal places

Posted 08 July 2012 - 08:11 PM

here's my new code but it's still not working. i guess i just don't really get how to format it.

#include <iostream>
#include <string>
#include <iomanip>
using namespace std;


int main() {
    //initialize variables
    int a1;
    int b1;
    int c1;
    int a2;
    int b2;
    int c2;
    double x;
    double y;
    
    
    //prompt and get input
    cout << "Welcome to Robert's Simultaneous Equation Solver! " << "\n";
    cout << "Please enter a1, b1, c1 (seperated by spaces): ";
    cin >> a1; cin >> b1; cin >> c1;
    cout << "Please enter a2, b2, c2 (seperated by spaces): ";
    cin >> a2; cin >> b2; cin >> c2;
    
if ((a1 == a2) &&
    (b1 == b2) &&
    (c1 == c2)) { cout << "Your inputs create identical lines."; }
else if ((a1 * b2 - a2 * b1) == 0) { cout << "Your inputs create parallel lines.";
     }
else 
    {
     
    //do math
    x = ( c1 * b2 - c2 * b1 ) / ( a1 * b2 - a2 * b1 ); 
    y = ( a1 * c2 - a2 * c1 ) / ( a1 * b2 - a2 * b1 ); 
    
    //output
    cout << "The solutions to the equations: " << "\n"; 
    cout << a1 << "x + " << b1 << "y = " << c1 << "\n"; 
    cout << a2 << "x + " << b2 << "y = " << c2 << "\n";  
    cout << "is x = ";
    cout << setprecision(3) << x << " and y = "; 
    cout << setprecision(3) << y <<endl;
    cout << "Thanks for using Robert's Simultaneous Equation Solver! ";
        
        
 
    
    //pause and exit
    getchar();
    getchar();
    return 0;
    }}


Was This Post Helpful? 0
  • +
  • -

#4 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3461
  • View blog
  • Posts: 10,669
  • Joined: 05-May 12

Re: Format output to display 2 decimal places

Posted 08 July 2012 - 08:22 PM

What do you mean by "it's not working"? Either describe the problem, paste in the output, and tell us what you were expecting to see.

Looking at the code, you are currently telling it that you want 3 meaningful digits to be displayed. Your intent based on your original post is that you want 2 decimal digits. Why are you then passing in 3? Additionally, aren't you forgetting to do something before calling setprecision()? Read the documentation linked above more closely, and take a look at the sample code in the documentation. I believe that it will reveal what you are looking for.
Was This Post Helpful? 0
  • +
  • -

#5 robertc  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 20
  • Joined: 29-June 12

Re: Format output to display 2 decimal places

Posted 08 July 2012 - 08:36 PM

ok here is what i have

//output
    cout << "The solutions to the equations: " << "\n"; 
    cout << a1 << "x + " << b1 << "y = " << c1 << "\n"; 
    cout << a2 << "x + " << b2 << "y = " << c2 << "\n";  
    cout << "is x = ";
    cout << fixed;
    cout << setprecision (2) << x << " and y = "; 
    cout << fixed;
    cout << setprecision (2) << y <<endl;




now it gives me an output of 2 decimal places.

The solutions to the equations:
1x + 2y = 2
1x + -2y = -12
is x = -5.00 and y = 3.00


but y should equal 3.50
Was This Post Helpful? 0
  • +
  • -

#6 robertc  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 20
  • Joined: 29-June 12

Re: Format output to display 2 decimal places

Posted 08 July 2012 - 09:06 PM

it worked after i changes all the variables to doubles but the assignment said that a1 - c2 should be int variables. it works so it should be fine to turn in but it's just kinda confusing. thanks for the help
Was This Post Helpful? 0
  • +
  • -

#7 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3461
  • View blog
  • Posts: 10,669
  • Joined: 05-May 12

Re: Format output to display 2 decimal places

Posted 08 July 2012 - 10:12 PM

On lines 35-36, try casting the numerator or denominator to double.
int a = 1;
int b = 3;
double x = a / b;
double y = double(a) / b;
double z = a / double(B)/>;


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1