Setprecision command

Feb 7, 2011 at 4:05pm
Hi all,

what i've here it's part of a program, i just want want someone to tell me what command i can used with Setprecision and setw to input a name and display the date;
when i try the command below, he just make the report unwritable
cout << "Person name: " <<setw(4) << Person name << endl;



// Display Tom figures.
cout << "\nTOM REPORT\n";
cout << "----------------\n";
cout << setprecision(2) << fixed;

cout << "Sales: $" <<setw(8) << Sales << endl;
cout << "Cost: $" <<setw(8) << Cost << endl;
cout << "Gain : $" <<setw(8) << Gain << endl;

return 0;

}
Feb 7, 2011 at 7:59pm
You're being a little unclear. Does your code compile, or does an error/unexpected output occur at runtime?
Feb 7, 2011 at 10:05pm
yes the code compile, but when i plug in this line: cout << "Person name: " <<setw(4) << Person name << endl; to input the person name, the compiler give me garbage
Feb 7, 2011 at 10:16pm
Are you expecting << Person name << to prompt the user to input the person's name or are you expecting it to output the person's name? Either way, read http://www.cplusplus.com/doc/tutorial/basic_io/
Feb 7, 2011 at 10:40pm
the prompt should be like: "enter the person name"; i can have the prompt to come but it wont let the remaining part of the report appear which are:
cout << "Sales: $" <<setw(8) << Sales << endl;
cout << "Cost: $" <<setw(8) << Cost << endl;
cout << "Gain : $" <<setw(8) << Gain << endl;

that's why i was wondering if there's a command setw who can let set the name
Feb 9, 2011 at 11:10pm
Hi all,

i've this program below, and i'm not able to set up the program to asked for the sales person name using:
cout << "\nEnter the sales Person name: ";
cin >> salesPerson;while

i'm also using the setprecision manipulator to print the report. can someone review this and let me know what i'm missing. without the name problem the program is working fine.

Thanks,

#include "stdafx.h"
#include <iostream>
#include <iomanip>
using namespace std;



int main()
{


double TotalSales,
CostSales,
NetProfit;

int salesPerson;

cout << "\nEnter the sales Person name: ";
cin >> salesPerson;

cout << "\nEnter the Total Sales: ";
cin >> TotalSales;

cout << "\nEnter the cost Sales: ";
cin >> CostSales

// Get the sales


// calculate NetProfit.
; NetProfit = TotalSales - CostSales;

// Display the sales figures.
cout << "\n SALES REPORT\n";
cout << "----------------\n";
cout << setprecision(2) << fixed;
cout << "sales Person: " <<setw(20) << salesPerson << endl;
cout << "Total Sales: $" <<setw(8) << TotalSales << endl;
cout << "Cost Sales: $" <<setw(8) << CostSales << endl;
cout << "NetProfit: $" <<setw(8) << NetProfit << endl;

return 0;
Feb 10, 2011 at 1:40am
closed account (zwA4jE8b)
ummm.... your salesPerson variable is an int, but the cout tells the user to enter a name?!?
Feb 10, 2011 at 2:20am
that's correct, but when the user enter a name the following lines are just garbage, but when i take the salesPerson out the program works fine. I guess my problem is to have the lines appears normaly with the salesPerson
Feb 10, 2011 at 3:50am
closed account (zwA4jE8b)
im not sure if it makes a difference or not but try setting fixed before setprecision. setprecision acts differently based on the flag of the ostream..

cout << fixed << setprecision(2);

what does your output look like?

oh, you are missing a ';' after cin >> CostSales

also you need to initialize netprofit or else c++ will make it a garbage number when you try to output it.
Last edited on Feb 10, 2011 at 3:54am
Feb 10, 2011 at 3:56am
closed account (zwA4jE8b)
#include <string>

string SalesPerson;

are you ending your program with a

}
Last edited on Feb 10, 2011 at 4:05am
Feb 10, 2011 at 4:41am
Yes my program has a } at end.
Feb 10, 2011 at 10:59am
closed account (z05DSL3A)
CreativeMFS wrote:
ummm.... your salesPerson variable is an int, but the cout tells the user to enter a name?!?
akounga2000 wrote:
that's correct, but when the user enter a name the following lines are just garbage,...
It will be, you are trying to put a string into an int. "I am not a number, I'm a free man!"

change int salesPerson; to string salesPerson; and see how you get on.
Feb 10, 2011 at 3:58pm
string salesPerson; solved the problem, thanks guys it worked.
Topic archived. No new replies allowed.