Thread: Need a hand formatting numbers.

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    33

    Need a hand formatting numbers.

    Here is my program it works how I would like it to but I can't seem to get formatting down. I want to round the numbers off in the dollar output. In some cases it is giving me 3 numbers in the cents column. I know there is a way to do it using floats but I can't seem to find the info on how to do it. My book isn't much help. Also in a couple cases I am setting a variable from another and I am unsure how to use ( char variable[25] ). You can see some of my variables have pointers and my teacher doesn't want me to use them yet. Thanks in advance.

    Code:
    #include <iostream>
    #include <iomanip>
    using namespace std;
    
    
    const double RED_RUG = 4.99;
    const double GREEN_RUG = 5.99;
    const double BLUE_RUG = 6.99;
    
    
    
    
    
    
    
    int main(void)
    
    {
        char* discount;
    	char customerName[25];
    	char rugColor;
        char paymentMethod;
    	int roomLength;
        int roomWidth;                                            //variables
    	char* rugDisplay;
    	char* discountPercent;
    	int roomArea;
    	double endDiscount;
    	double paidDiscount;
    	double rugCost;
    	double carpetCost;
    	double trueCost;
    
    	cout<<setw(20)<<"Welcome to..."<<endl;
    	cout<<endl;
    	cout<<setw(40)<<">>>Phils Floors<<<<"<<endl;                     //business header-output to screen
    	cout<<endl;
    	cout<<setw(37)<<"Our Rugs Rock"<<endl;
    	cout<<endl;
    
    
    	cout<<"If you enter the customers name, type of rug purchased,"<<endl;
        cout<<"room dimensions, and type of payment, this program"<<endl;       //program explaination
        cout<<"will calculate and print a bill to the screen"<<endl;
    	cout<<endl;
    	
    
    	cout<<"Please enter the customers name: ";
    	cin.getline(customerName,25);                                //input customers name
    
    	cout<<endl;
    
    	cout<<"Please enter rug color (R)ed (B)lue (G)reen: ";
        cin>>rugColor;                                               //input rug color
    
    	cout<<endl;
    
    	if (rugColor == 'R' || rugColor == 'r')
    	{
    		rugDisplay = "RED";
    		rugCost = RED_RUG;
    	}
    	else
    		if (rugColor == 'B' || rugColor == 'b')
    	{
    		rugDisplay = "BLUE";                                     //set rugdisplay variable and cost of rug
    		rugCost = BLUE_RUG;                                          
    	}
    	else
    		if(rugColor == 'G' || rugColor == 'g')
    	{
    		rugDisplay = "GREEN";
    		rugCost = GREEN_RUG;
    	}
        else
    	{
    		rugDisplay = "N/A";
    		rugCost = 6.99;
    	}
    	 
        cout<<endl;
    
        cout<<"Please enter the method of payment"<<endl;
    	cout<<"(C)ash (Z)ippy or (O)ther: ";
    	cin>>paymentMethod;                                           //input payment method
     
        if (paymentMethod == 'C' || paymentMethod == 'c')
    		paidDiscount = .10;
    
    	else if (paymentMethod == 'Z' || paymentMethod == 'z')        //set paidDiscount variable to use in calculation
    	    paidDiscount = .05;
    
    	else 
    	    paidDiscount = 1;
    
    
    	if (paymentMethod == 'C' || paymentMethod == 'c')
    	{
    		 discount = "Cash and Carry";
    		 discountPercent = "10% -- Cash and Carry";
    	}
    	else if (paymentMethod == 'Z' || paymentMethod == 'z')
    	{
        	 discount = "Zippy Discount";                             //set discount variables (output messages)
             discountPercent = "5% -- Zippy Discount";
    
    	}
    
    	     
    	else if (paymentMethod == 'O' || paymentMethod == 'o')
    	{
    	     discount = "No Discount";
    		 discountPercent = "No Discount";
    	}
    	    
    	else 
    	     discount = "No discount type specified";
    	
    	
    	cout<<endl;
    
    	cout<<"Enter the Length of your room: ";                        //input room length
    	cin>>roomLength;
    	cout<<endl;
    
    	cout<<"Enter the Width of your room: ";
    	cin>>roomWidth;                                                 //input room width
    
    
         
    	roomArea = (roomWidth * roomLength);                            //calculate room area
    
        carpetCost = (roomArea * rugCost);                              //calculate rug cost
    
        endDiscount = (roomArea * rugCost * paidDiscount);              //calculate discount
    
        trueCost = (carpetCost - endDiscount);                          //calculate carpet cost minus the discount (truecost)
    
    
    
    	
    	cout<<setw(40)<<">>>Phil's Floors<<<<"<<endl;
    	cout<<endl;                                                     //output header
    	cout<<setw(37)<<"Our Rugs Rock"<<endl;
    	cout<<endl;
    
    	cout<<"Customer Name:"<<setw(26)<<customerName<<endl;           //output customer name
    	cout<<endl;
    
    	cout<<"Rug Color:"<<setw(30)<<rugDisplay<<endl;                 //output rugcolor
    	cout<<endl;
    
    	cout<<"Payment Method:"<<setw(25)<<discount<<endl;              //output payment method
    	cout<<endl;
    
    	cout<<setw(30)<<discountPercent<<endl;                          //output discount percent message
    	cout<<endl;
    
    	
        
        	
        cout<<"Discount: "<<setw(26)<<"$"<<endDiscount<<endl;           //output discount
    	cout<<endl;
        
    	cout<<"Rug Cost: "<<setw(25)<<"$"<<carpetCost<<endl;            //output carpet cost (before discount)
    
    
    	cout<<"Final Cost:"<<setw(24)<<"$"<<trueCost<<endl;             //output carpet cost(final cost-after discount)
    
    
    
    	
    	return 0;

  2. #2
    Registered User kiss_psycho's Avatar
    Join Date
    Feb 2003
    Posts
    49
    use strcpy or CString objects to avoid pointers. and could you please rephrase your roundoff problem?
    Definition of Programmer : A red-eyed mumbling mammal capable of conversing with inanimate objects.

    Happy Hunting...
    The Root

  3. #3
    Registered User
    Join Date
    Sep 2003
    Posts
    33
    Welcome to...

    >>>Phils Floors<<<<

    Our Rugs Rock

    If you enter the customers name, type of rug purchased,
    room dimensions, and type of payment, this program
    will calculate and print a bill to the screen

    Please enter the customers name: Phil KCustomer

    Please enter rug color (R)ed (B)lue (G)reen: r


    Please enter the method of payment
    (C)ash (Z)ippy or (O)ther: c

    Enter the Length of your room: 6

    Enter the Width of your room: 8
    >>>Phil's Floors<<<<

    Our Rugs Rock

    Customer Name: Phil KCustomer

    Rug Color: RED

    Payment Method: Cash and Carry

    10% -- Cash and Carry

    Discount: $23.952

    Rug Cost: $239.52
    Final Cost: $215.568
    Press any key to continue

    here is the output...as you can see in the discount and final cost I am getting 3 numbers as opposed to two like it should be when talking about money. I'm not sure how to get these rounded off. I am not familiar with the command that you suggested to use for the pointer also. I am really only supposed to use char variable[25] command. How would I use that when I set a variable from another one?

  4. #4
    Registered User kiss_psycho's Avatar
    Join Date
    Feb 2003
    Posts
    49
    try using cout.precision(precision_value);
    eg:

    cout<<setiosflags(ios::fixed);
    cout.precision(2);
    cout<<123.4567;

    O/P is 123.46

    for rounding off floating point nos. and for copying character arrays, use strcpy(destination_arrayname,source_arrayname);
    Last edited by kiss_psycho; 10-14-2003 at 10:16 AM.
    Definition of Programmer : A red-eyed mumbling mammal capable of conversing with inanimate objects.

    Happy Hunting...
    The Root

  5. #5
    Registered User
    Join Date
    Sep 2003
    Posts
    33
    Code:
    cout<<setprecision(2)<<"Final Cost:"<<setw(24)<<"$"<<trueCost<<endl;

    that's what I was looking for...thanks. My book tells me this is the correct format to to make sure I have #include <iomanip> which I do. The problem now is when I stick that in there I get some really funky output: Any thoughts about what I am doing wrong now?
    Welcome to...

    >>>Phils Floors<<<<

    Our Rugs Rock

    If you enter the customers name, type of rug purchased,
    room dimensions, and type of payment, this program
    will calculate and print a bill to the screen

    Please enter the customers name: Phil Customer

    Please enter rug color (R)ed (B)lue (G)reen: r


    Please enter the method of payment
    (C)ash (Z)ippy or (O)ther: c

    Enter the Length of your room: 6

    Enter the Width of your room: 8
    >>>Phil's Floors<<<<

    Our Rugs Rock

    Customer Name: Phil Customer

    Rug Color: RED

    Payment Method: Cash and Carry

    10% -- Cash and Carry

    Discount: $23.952

    Rug Cost: $239.52
    Final Cost: $2.2e+002
    Press any key to continue


    I just put the setprecision command in the final cost line and that is what I am getting as output =/

  6. #6
    Registered User
    Join Date
    Sep 2003
    Posts
    33
    just saw your edited post...gonna give that a shot.

  7. #7
    Registered User
    Join Date
    Sep 2003
    Posts
    33
    that did it...thanks a million needed that cout<<setiosflags(ios::fixed); line in there. My other problem might be a bit tough. I am just intro to C++ in college and not supposed to be using arrays just cin.getline. We haven't gotten into strcpy or CString yet. So should I rewrite my code to avoid setting variable off of other ones?

  8. #8
    Registered User kiss_psycho's Avatar
    Join Date
    Feb 2003
    Posts
    49
    So should I rewrite my code to avoid setting variable off of other ones?
    could you explain a bit clearly.
    Definition of Programmer : A red-eyed mumbling mammal capable of conversing with inanimate objects.

    Happy Hunting...
    The Root

  9. #9
    Registered User
    Join Date
    Sep 2003
    Posts
    33
    basically I am looking for a variable type that can store about 40 characters with spaces for the output messages and I am not allowed to use pointers or char*. If ya look at the code I am setting variables from the user selection. For example when they hit the c (for cash) key I set the discount variable (which is just text output) to "10% -- cash and carry". I need a variable type that can store this.

  10. #10
    Registered User kiss_psycho's Avatar
    Join Date
    Feb 2003
    Posts
    49
    strange -- no arrays, no * rs, no cstrings too???

    check if you can use CString objects in your code if you use MSVC++, you can try something like this.

    Code:
    CString str1,str2;
    cin>>str1;
    cout<<str1;
    str2=str1;
    cout<<str2;
    try it out.
    Definition of Programmer : A red-eyed mumbling mammal capable of conversing with inanimate objects.

    Happy Hunting...
    The Root

  11. #11
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    Just use string. You already #include <string> and use it in your code from other posts, so use it here.

    string discount;
    string discountPercent;

    ...

    discount = "Cash and Carry";
    discountPercent = "10% -- Cash and Carry";

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question about random numbers
    By Kempelen in forum C Programming
    Replies: 2
    Last Post: 07-02-2008, 06:28 AM
  2. Help with Rational Numbers (C++)
    By cloudjc in forum C++ Programming
    Replies: 3
    Last Post: 04-28-2008, 04:03 PM
  3. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 05:20 AM
  4. the definition of a mathematical "average" or "mean"
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 12-03-2002, 11:15 AM
  5. A (complex) question on numbers
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 02-03-2002, 06:38 PM