Thread: Newbie here, need a hand

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

    Newbie here, need a hand

    howdy guys, I've been messing with this program for the last couple of days for my intro to c++ class. I know I'm doing stuff wrong in here but not sure what it is. Anyone have suggestions on maybe how I can do the calculations more efficiently and maybe even the whole program?
    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;
    	char* rugDisplay;
    	char* discountPercent;
    	int roomArea;
    	double endDiscount;
    	double paidDiscount;
    	double rugCost;
    	double carpetCost;
    
    	cout<<setw(20)<<"Welcome to..."<<endl;
    	cout<<endl;
    	cout<<setw(40)<<">>>Toupay's Warehouse<<<<"<<endl;
    	cout<<endl;
    	cout<<setw(35)<<"The Best in rugs"<<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;
        cout<<"will calculate and print a bill to the screen"<<endl;
    
    
    	cout<<"Please enter the customers name: ";
    	cin.getline(customerName,25);
    
    	cout<<endl;
    
    	cout<<"Please enter rug color (R)ed (B)lue (G)reen: ";
        cin>>rugColor;
    
    	cout<<endl;
    
    	if (rugColor == 'R')
    	{
    		rugDisplay = "RED";
    	}
    	else
    		if (rugColor == 'B')
    	{
    		rugDisplay = "BLUE";
    	}
    	else
    		if(rugColor == 'G')
    	{
    		rugDisplay = "GREEN";
    	}
        else
    		rugDisplay = "N/A";
    
    	cout<<endl;
    
        cout<<"Please enter the method of payment"<<endl;
    	cout<<"(C)ash (Z)ippy or (O)ther: ";
    	cin>>paymentMethod;
    
    	if (paymentMethod == 'C')
    	{
    		 discount = "Cash and Carry";
    	}
    	else if (paymentMethod == 'Z')
    	{
        	 discount = "Zippy Discount";
    	}
    	     
    	else if (paymentMethod == 'O')
    	{
    	     discount = "No Discount";
    	}
    	    
    	else 
    	     discount = "N/A";
    		
    
    	if (paymentMethod == 'C')
    	{
    		discountPercent = "Cash and Carry 10%";
    	}
    	else if (paymentMethod == 'Z')
    	{
    		discountPercent = "Zippy Discount 5%";
    	}
    	else if (paymentMethod == 'O')
    	{
    		discountPercent = "No Discount";
    	}
    	else
    	{
    		discountPercent = "N/A - No discount type specified";
    	}
    
    	cout<<endl;
    
    	cout<<"Enter the Length of your room: ";
    	cin>>roomLength;
    
    	cout<<endl;
    
    	cout<<"Enter the Width of your room: ";
    	cin>>roomWidth;
    
    	roomArea = (roomWidth * roomLength);
    
    
    
    	cout<<endl;
    
    	cout<<setw(40)<<">>>Toupay's Warehouse<<<<"<<endl;
    	cout<<endl;
    	cout<<setw(35)<<"The Best in rugs"<<endl;
    	cout<<endl;
    
    	cout<<"Customer Name:"<<setw(26)<<customerName<<endl;
    
    	cout<<endl;
    
    	cout<<"Rug Color:"<<setw(30)<<rugDisplay<<endl;
    
    	cout<<endl;
    
    	cout<<"Payment Method:"<<setw(25)<<discount<<endl;
    	
    	cout<<endl;
    
    	cout<<"Discount:"<<setw(18)<<discountPercent<<endl;
    
    	cout<<endl;
    
    	if (paymentMethod == 'C')
    		paidDiscount = .10;
    
    	else if (paymentMethod == 'Z')
    	    paidDiscount = .05;
    
    	else 
    	    paidDiscount = 1;
    
        if (rugDisplay == "RED")
    		rugCost = RED_RUG;
    
    	else if (rugDisplay == "BLUE")
    	    rugCost = BLUE_RUG;
    
    	else if (rugDisplay == "GREEN")
    		rugCost = GREEN_RUG;
    
    	else 
    		rugCost =6.99;
    
        endDiscount = (roomArea * rugCost * paidDiscount);
    
    	if (paidDiscount == '0')
    		endDiscount = "0"
    
        cout<<"Discount: "<<setw(25)<<"$"<<endDiscount<<endl;
    
        carpetCost = (roomArea * rugCost);
    
    	cout<<"Final Cost:"<<setw(23)<<"$"<<carpetCost<<endl;
    
    
    
    	
    	return 0;

  2. #2
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    What is wrong with the program? are you getting any errors...be more specific.

    One suggestion I have is that instead of having the progra in main() I would have an individual function for each set of instructions/operations/calculations. This will make the code easier to read, edit, and debug. Also if this is a class assignment I suggest you include plenty of comments, because as of right now I don't see a single one. Having comments in your program is essential, even more so if it is for a grade!

    axon

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  3. #3
    Registered User C of Green's Avatar
    Join Date
    Jan 2002
    Location
    Calgary/Canada
    Posts
    59
    Perhaps check for both Upper and Lower case letters ...

    Code:
       if (rugColor == 'R' || rugColor == 'r')
       ...

  4. #4
    Registered User
    Join Date
    Sep 2003
    Posts
    33
    I cleaned the program up a bit and found a lot of errors actually whille putting the comments in B). One last question when I output my results to the screen it's going all the way out to thousandths. How do I make the program round the numbers off

    ex: 25.252 when I want 25.25

    heres my updated code:

    Code:
    #include <iostream>
    #include <iomanip>
    using namespace std;
    
    
    const double RED_RUG = 4.99;
    const double GREEN_RUG = 5.99;  //constants
    const double BLUE_RUG = 6.99;
    
    
    
    
    
    
    
    int main(void)
    
    {
        char* discount;            
    	char customerName[25];
    	char rugColor;
        char paymentMethod;
    	int roomLength;              //variables
        int roomWidth;
    	char* rugDisplay;
    	char* discountPercent;
    	int roomArea;
    	double endDiscount;
    	double paidDiscount;
    	double rugCost;
    	double carpetCost;
    	double savings;
    
    	cout<<setw(20)<<"Welcome to..."<<endl;            //output to screen
    	cout<<endl;
    	cout<<setw(40)<<">>>Toupay's Warehouse<<<<"<<endl;
    	cout<<endl;
    	cout<<setw(35)<<"The Best in rugs"<<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;
        cout<<"will calculate and print a bill to the screen"<<endl;
    
    
    	cout<<"Please enter the customers name: ";     
    	cin.getline(customerName,25);                            //input customer name
    
    	cout<<endl;
    
    	cout<<"Please enter rug color (R)ed (B)lue (G)reen: ";
        cin>>rugColor;                                           //input rug color
    
    
        if (rugColor == 'R' || rugColor == 'r')                  //set rugDisplay variable to output rug color                           
    	{             
    		rugDisplay = "RED";
    	}
    	else
    		if (rugColor == 'B' || rugColor == 'b')
    	{
    		rugDisplay = "BLUE";
    	}
    	else
    		if(rugColor == 'G' || rugColor == 'g')
    	{
    		rugDisplay = "GREEN";
    	}
        else
    		rugDisplay = "N/A";
    
    	cout<<endl;
    
    
    	cout<<endl;
    
        cout<<"Please enter the method of payment"<<endl;       //input payment type
    	cout<<"(C)ash (Z)ippy or (O)ther: ";
    	cin>>paymentMethod;
    
        if (paymentMethod == 'C' || 'c')                        //set discount
    		paidDiscount = .10;
    
    	else if (paymentMethod == 'Z' || 'z')
    	    paidDiscount = .05;
    
    	else 
    	    paidDiscount = 1;
                                                        
    	    
    	if (paymentMethod == 'C' || paymentMethod == 'c')        //set discount variable for message output
    	{
    		 discount = "Cash and Carry";                     
    	}
    	else if (paymentMethod == 'Z' || paymentMethod == 'z')
    	{
        	 discount = "Zippy Discount";
    	}
    	     
    	else if (paymentMethod == 'O' || paymentMethod == 'o')
    	{
    	     discount = "No Discount";
    	}
    	    
    	else 
    	     discount = "N/A";
    
        	
    
    	if (paymentMethod == 'C' || paymentMethod == 'c')         //set second discount message
    	{
    		discountPercent = "Cash and Carry 10%";             
    	}
    	else if (paymentMethod == 'Z' || paymentMethod == 'z')
    	{
    		discountPercent = "Zippy Discount 5%";
    	}
    	else if (paymentMethod == 'O' || paymentMethod == 'o')
    	{
    		discountPercent = "No Discount";
    	}
    	else
    	{
    		discountPercent = "N/A - No discount type specified";
    	}
    
    	cout<<endl;
    
    	cout<<"Enter the Length of your room: ";                   //input length of room
    	cin>>roomLength;
    
    	cout<<endl;
    
    	cout<<"Enter the Width of your room: ";                    //input width of room
    	cin>>roomWidth;
    
    	
    	cout<<endl;
    
    	cout<<setw(40)<<">>>Toupay's Warehouse<<<<"<<endl;         
    	cout<<endl;
    	cout<<setw(35)<<"The Best in rugs"<<endl;
    	cout<<endl;
    
    	cout<<"Customer Name:"<<setw(26)<<customerName<<endl;      //output customer information
    
    	cout<<endl;
    
    	cout<<"Rug Color:"<<setw(30)<<rugDisplay<<endl;            //output rug color
    
    	cout<<endl;
    
    	cout<<"Payment Method:"<<setw(25)<<discount<<endl;         //output payment method
    	
    	cout<<endl;
    
    	cout<<"Discount:"<<setw(18)<<discountPercent<<endl;        //output Discount message
     
    	cout<<endl;
    
    	
        if (rugDisplay == "RED")                                    //set rug cost
    		rugCost = RED_RUG;
    
    	else if (rugDisplay == "BLUE")
    	    rugCost = BLUE_RUG;
    
    	else if (rugDisplay == "GREEN")
    		rugCost = GREEN_RUG;
    
    	else 
    		rugCost = 6.99;
    
            
        roomArea = (roomWidth * roomLength);                       //calculate room length and width and set variable
    
        carpetCost = (roomArea * rugCost);                         //calculate carpet cost
    
        endDiscount = (carpetCost * paidDiscount);                 //calculate discount
    
        savings = (carpetCost - endDiscount);                      //calculate final cost
    
    	cout<<"Discount: "<<setw(26)<<"$"<<endDiscount<<endl;      //output discount amount
    
    	cout<<endl;
    
        cout<<"Rug Price: "<<setw(25)<<"$"<<carpetCost<<endl;      //output total rug cost
    
    	cout<<endl;
          
        cout<<"Final Price: "<<setw(22)<<"$"<<savings<<endl;       //output Final price after discount
    
    
    
    	
    	return 0;

  5. #5
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    Originally posted by Furious_George
    when I output my results to the screen it's going all the way out to thousandths. How do I make the program round the numbers off
    Simple. Printf.

    I'm a bit rusty because I haven't coded in a few months, but I believe it goes like this...

    if you had this originally:

    Code:
    float x = 123.23412;
    cout << x << endl;
    And you only wanted two decimal places:

    [code]
    float x = 123.23412;
    printf(".2%f", x);
    [code]

    I can't remember where the .2 goes... it might go in between the $ and the f (float), or it might go after. Try all three ways, one should work

  6. #6
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    You can format output using funtions in the iomanip library.
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  7. #7
    Registered User
    Join Date
    Sep 2003
    Posts
    33
    I'm not that advanced yet hehe this is just intro to C++ =/ . I thought you could make the number "float" by adding an "f" to the variable?

  8. #8
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    Don't make your variables float. Keep them as doubles.
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  9. #9
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    Originally posted by Furious_George
    I'm not that advanced yet hehe this is just intro to C++ =/ . I thought you could make the number "float" by adding an "f" to the variable?
    Actually, I would say that the I/O manipulators in C++ are not any more advanced that the format specifiers in printf, and since you seem to be learning C++ why not learn the C++ way to do it?

    Example:
    Code:
    #include<iostream>
    #include<iomanip>
    
    int main()
    {
        float flt = 123.4567f;
        double dbl = 123.4567;
        std::cout << std::fixed << std::setprecision(2) << flt << std::endl;
        std::cout << std::fixed << std::setprecision(2) << dbl << std::endl;
    }

  10. #10
    I am he who is the man! Stan100's Avatar
    Join Date
    Sep 2002
    Posts
    361
    Once you're done this, maybe you can join my project
    (SEE BELOW). It's designed specifically for noobs or anyone else who isn't programming with graphics libraries yet. You look like the perfect candidate.

    One question: Is the code you showed us from a book, schoolwork, or something you made to test out your current skills?
    Stan The Man. Beatles fan

    When I was a child,
    I spoke as a child,
    I thought as a child,
    I reasoned as a child.
    When I became a man,
    I put childish ways behind me"
    (the holy bible, Paul, in his first letter to the Cor. 13:11)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie Question - fflush(stdin) & fpurge(stdin) on Mac and PC
    By tvsinesperanto in forum C Programming
    Replies: 34
    Last Post: 03-11-2006, 12:13 PM
  2. Newbie in problem with looping
    By nrain in forum C Programming
    Replies: 6
    Last Post: 11-05-2005, 12:53 PM
  3. Newbie Programmer
    By Extropian in forum C++ Programming
    Replies: 3
    Last Post: 05-18-2004, 01:17 PM
  4. Cribbage Game
    By PJYelton in forum Game Programming
    Replies: 14
    Last Post: 04-07-2003, 10:00 AM
  5. Newbie Game Develpoers Unite!
    By Telenosis in forum Game Programming
    Replies: 10
    Last Post: 06-22-2002, 02:02 PM