Code Help

Discussion in 'C++' started by carvin317, Feb 6, 2009.

  1. carvin317

    carvin317 New Member

    Joined:
    Feb 6, 2009
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    Hey guys im new I was wondering how to do something in my new program and im stumped.

    I have a program that prices and i have 5 states

    GA
    FL
    SC
    TN
    AL

    the program asks the user what state they live in. I want to associate the tax to state so Ga=.06, FL=.04, SC=.05, TN=.06, AL=.02, & Other=No Tax Applied.

    So my question is how do i declare the states to represent a numeric value.

    Im going to plug it into a formula that i have for example.

    Ftotalprice=(Fgallons*Fprice)State

    Any Help would be great thanks
     
  2. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    You want a lookup table that associates the state ID with its tax bracket. This could be in the form of a couple of arrays, e.g. one that lists the states and one that lists the taxes:
    Code:
    char *states[]={"GA","FL","SC"};
    float taxes[]={0.06,0.04,0.05};
    
    then a loop that looks for the state and picks out the tax:
    Code:
    float tax=0.0;
    for (int i=0; i<3; i++)
      if (!strcmp(test_state, states[i]))
        tax=taxes[i];
    
     
  3. carvin317

    carvin317 New Member

    Joined:
    Feb 6, 2009
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    okay that kind of makes sense to me, like i said im a beginner programmer so where would the


    "char *states[]={"GA","FL","SC"};
    float taxes[]={0.06,0.04,0.05};"

    be placed ?

    and what about if the state is something other than that like TX will that just assume it as zero ?
     
  4. carvin317

    carvin317 New Member

    Joined:
    Feb 6, 2009
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    i guess there isnt an edit button, I found where to put, but i guess my question is now how do i add all the rest of the states without really adding them ? is there way i can say if it is not listed do not tax ? or is that implied
     
  5. cpulocksmith

    cpulocksmith New Member

    Joined:
    Jul 23, 2008
    Messages:
    289
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    student
    Location:
    canada
    i think you could just put... under your if statement ...

    Code:
    else {tax = 0};
    i guess it really depends on how you did the rest of your code. if you post the whole code i could tell ya... i love debugging <3... its like a hobby ^^
     
  6. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    No, you wouldn't put "else tax=0" after the if, that would just set tax(which is already 0) to 0 again unnecessarily. The way my suggestion code works is that it sets tax to 0 then looks at states it knows about, only setting the tax rate if it finds one. If it doesn't find one then tax is unchanged from the default zero.

    You would place this code iin the function that calculates the tax, perhaps, or maybe make it a global, depending on the design. It depends really how many functions need access to that data. Global variables are generally considered evil because of the dependencies they create, but this would be global static data so I think the rules are probably not as strict. But get used to localisation of data, it's good practice. So for example (untested code):

    Code:
    double calc_tax(double price,char *state)
    {
      char *states[]={"GA","FL","SC"};
      double taxes[]={0.06,0.04,0.05};
    
      double tax=0.0;
      for (int i=0; i<3; i++)
        if (!strcmp(test_state, states[i]))
          tax=taxes[i];
    
      double total_price=price*(1.0+tax);
      return total_price;
    }
    
    To extend this to more states just add the states and the tax rate to the arrays and increase the loop termination constant from 3.
    Code:
      char *states[]={"GA","FL","SC","UK"};
      double taxes[]={0.06,0.04,0.05,0.15};
    ...
      for (int i=0; i<4; i++) // note change from 3 to 4
    ...
    
    Of course, keeping the constant in line with the arrays is tedious, perhaps you might use an array entry to indicate end of the array:
    Code:
      char *states[]={"GA","FL","SC","UK","X"};
      double taxes[]={0.06,0.04,0.05,0.15};
    ...
      for (int i=0; states[i][0]!='X'; i++)
    ...
    
    Note the difference between "X" and 'X'. "X" is equivalent to 'X','\0' - because strings are char constants plus a terminating zero. 'X' is an integer constant and can be compared with operators like !=, but "X" cannot be compared in the same way, you must use strcmp. "X"[0]='X' so we use states[0]!='X' to check for the end of the array.

    "X"[0] is actually a valid C expression, try printf("%c","X"[0]); and maybe some variations like printf("%c","Hello"[2]); and because of the way the square brackets work "Hello"[2] is identical to 2["Hello"] (try it! you won't break your computer, although you might confuse the hell out of people trying to read your code).

    Lots for you to have a play with there :)
     
  7. carvin317

    carvin317 New Member

    Joined:
    Feb 6, 2009
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    Okay here is my whole code. I went with an if else statement to do it, but for some weird reason it is not working how i want it. when i run the program it says with tax 0 and i know that is not right. The problem with setting the tax as 0 is that when it runs i multiplies the cost of the paint by 0 resulting in the price being zero. so i guess i would need for any other state not listed to skip that step. If any of you could help me i would literally donate to the site.







    Code:
    /*Program: PaintModule
      Programmer: Nathan Marcarelli
      Date: 01/22/09
      Prgoram:ModuleProgram.cpp
      Version:1
    */
    
    /*-------------------------PREPROCESSING DIRECTIVES-----------------------------------------------------*/
    #include <iostream>
    #include <iomanip>
    #include <cmath>
    #include <cctype>
    #include <cstdlib>
    #include <cstring>
    
    	using namespace std;
    
    /*-------------------------FUNCTION PROTOTYPES----------------------------------------------------------*/
     void GetRoomDimensions(void);
     void PerformCalc(void);
     void PerformSalesCalc(void);  //tc prototype
     void PrintOutput(void);
     void GetName(void);
     void RepeatCode (void);
     void LoopQuestion (void);
     void DisplayHeader(void);			
     void Dashes(void);			
     void SalesDayScreen (void);
     void Center (char *s);
     
    
     
    /*-----------------------Global Variables--------------------------------------------------------------*/
    
    	//input variables
    	float Flength;
    	float Fwidth;
    	float Fheight;
    	char sCname[25];
    	char sRepeat;
    	char sSales='Y';
    	char sState[3];
    
    	//Calculated Data
    	float Farea;				// = wall 1 + wall 2
    	float Fwall1;				// Length * Height *2
    	float Fwall2;				// Width * Height *2
    	float Fareagallon;			// Amount of area a gallon will cover
    	float Fgallons;				// Total Number of gallons it will take to paint
    	float Fbeforediscount;		//before discount
    	float Fafterdiscount;		//After discount
    	float Ftax;					//With Tax
    	float Fcostper;				//Cost per gallon of paint
    	float Famountoff;			//amount off discount
    
    /*--------------------Main Module------------------------------------------------------------------*/
    
    	int main ()
    	{
    		SalesDayScreen();
    
    		//tc I would consider moving this logical comparison further
    		//down the line.  In other words, you do not need an entire
    		//routine called SalesPath.  All you really need is the if
    		//statement surrounding the check to see if you need to call PerformSalesCalc
    			
    		GetRoomDimensions();
    		if (sSales=='Y')
    			
    				{
    					PerformSalesCalc();
    				}
    		
    		PerformCalc();  //tc note: Fafterdiscount will need to be set to
    						//the cost before discount if it is not a sales day.
    		PrintOutput();
    		LoopQuestion();
    		system("pause");
    			return 0;
    
    
    		}
    
    	
    
    
    /*-----------------------------------------------------------------------------------------------------
    Name: SalesDayScreen
    Desc: Asks user if it is a sales day
    Return: Void
    Parameters: Void
    ---------------------------------------------------------------------------------------------------*/
    void SalesDayScreen (void)
    {	
    	DisplayHeader();
    	Center("Acme Paint Shop - Sales Day Question");
    	Dashes();
    	cout<<"Is it a sales day (Y/N)?";
    	cin>>sSales;
    	cin.ignore(); 
    	return;
    }
    
    
    
    
    /*-----------------------------------------------------------------------------------------------------
    Name: GetName	
    Desc: Gets Name
    Return: Void
    Parameters: Void
    ---------------------------------------------------------------------------------------------------*/
    void GetName (void)
    {
    	
    
    	cout<<"Enter the customer's first and last name?";
    	cin.getline(sCname, 25);
    	
    	return;
    }
    /*-----------------------------------------------------------------------------------------------------
    Name: GetRoomDimensions
    Desc: Gets Room Dimensions
    Return: Void
    Parameters: Void
    ---------------------------------------------------------------------------------------------------*/
    
    	void GetRoomDimensions(void)
    	{
    		
    		DisplayHeader();
    		Center("Acme Paint Shop - Input");
    		Dashes();
    
    		GetName();
    
    		cout<<"What is the length of the room in feet?";
    		cin>> Flength ;
    
    		cout<<"What is the width of the room in feet?";
    		cin>>Fwidth;
    
    		cout<<"What is the height of the room in feet?";
    		cin>>Fheight;
    
    		cout <<"What is the area covered by a gallon of paint?";
    		cin>>Fareagallon;
    
    		cout <<"What is the cost of paint per gallon?";
    		cin >> Fcostper;
    
    		cout<<"What state are you located?";
    		cin>>sState;
    		
    
    		system("cls");
    
    		
    
    		
    		return;
    	}
    	
    
    /*---------------------------------------------------------------------------------------------------------
    Name:PerformCalc
    Desc:Calculate Area
    Return: Void
    Parameters:Void
    ----------------------------------------------------------------------------------------------------------*/
    
    	void PerformCalc (void)
    	{
    	Fwall1 = (Flength * Fheight)* 2;
    	Fwall2 = (Fwidth * Fheight) * 2;
    	Farea = (Fwall1+ Fwall2);
    	Fgallons = Farea/Fareagallon;
    	Fbeforediscount = ceil(Fgallons) *  Fcostper;
    	
    
    		if (sState=="GA") 
    	{
    	
    			Ftax = Fbeforediscount * 0.06f;
    	}
    		
    
    	else if (sState=="FL")
    	{	
    			Ftax=Fbeforediscount*0.04f;
    	}	
    
    	else if (sState=="SC") 
    	{		
    			Ftax=Fbeforediscount*0.05f;
    	}		
    
    	else if (sState=="TN")
    	{		
    			Ftax=Fbeforediscount*0.06f;
    
    	}
    	else if (sState=="AL")
    	{
    		Ftax=Fbeforediscount*0.02f;
    	}
    
    	Ftax *= Fbeforediscount;
    
    	return;
    	}
    /*---------------------------------------------------------------------------------------------------------
    Name:PerformSalesCalc
    Desc:Calculate Area
    Return: Void
    Parameters:Void
    ----------------------------------------------------------------------------------------------------------*/
    void PerformSalesCalc (void)
    {
    	Fwall1 = (Flength * Fheight)* 2;
    	Fwall2 = (Fwidth * Fheight) * 2;
    	Farea = (Fwall1+ Fwall2);
    	Fgallons = Farea/Fareagallon;
    	Fbeforediscount = ceil(Fgallons) *  Fcostper;
    
    
    	if (Fgallons==1) 
    	{
    	
    			Famountoff = Fcostper * .05f;
    	}
    		
    
    	else if (Fgallons <=3 && Fgallons >=2) 
    	{	
    			Famountoff = Fcostper * .10f;
    	}	
    
    	else if (Fgallons == 4) 
    	{		
    			Famountoff = Fcostper * .15f;
    	}		
    
    	else if (Fgallons >=5)
    	{		
    			Famountoff = Fcostper * .25f;
    
    	}
    	//tc 1. move the call to after the Famountoff calculation
    	//tc the calculation is not correct.  Look at Famoutoff. Haven't
    	//you already multiplied the cost per can by the discount amount?
    	Fafterdiscount=ceil(Fgallons)* (Fcostper-Famountoff);
    
    
    
    	//State Tax
    
    		if (sState=="GA") 
    	{
    	
    			Ftax=Fafterdiscount*0.06f;
    	}
    		
    
    	else if (sState=="FL")
    	{	
    			Ftax=Fafterdiscount*0.04f;
    	}	
    
    	else if (sState=="SC") 
    	{		
    			Ftax=Fafterdiscount*0.05f;
    	}		
    
    	else if (sState=="TN")
    	{		
    			Ftax=Fafterdiscount*0.06f;
    
    	}
    	else if (sState=="AL")
    	{
    		Ftax=Fafterdiscount*0.02f;
    	}
    
    
    	Ftax *= Fafterdiscount;
    			
    	return;
    }
    
    
    /*-----------------------------------------------------------------------------------------------------------
    Name:PrintOutput
    Desc:Display Output on Monitor
    Return:Void
    Parameter: Void
    
    ------------------------------------------------------------------------------------------------------------*/
    
    	void PrintOutput (void)
    {
    	DisplayHeader();
    	Center("Acme Paint Shop - Single Customer Calculation");
    	Dashes();
    
    	cout <<endl;
    	cout<<"Customer Name (first and last):"<<sCname;
    	cout<<endl;
    
    
    	cout <<"Number of gallons needed: ";
    	cout <<ceil(Fgallons);
    	cout <<endl;
    
    	cout <<"Cost before discount $";
    	cout <<Fbeforediscount;
    	cout << endl;
    
    	cout<<"Cost after discount $";
    	cout <<Fafterdiscount;
    	cout<<endl;
    
    	cout<<"Cost with Tax $";
    	cout<<Ftax;
    	cout<<endl;
    	
    
    	return;
    	}
    
    /*-----------------------------------------------------------------------------------------------------
    Name: RepeatCode	
    Desc: Code Inside Loop
    Return: Void
    Parameters: 
    ---------------------------------------------------------------------------------------------------*/
       void RepeatCode ()
    {
    	DisplayHeader();
    	GetName();
    	GetRoomDimensions();
    	PerformSalesCalc ();
    	PrintOutput();
    	LoopQuestion();
    				
      		
    	return;
       }
    	
    	
    /*-----------------------------------------------------------------------------------------------------------
    Name:Loop
    Desc:Loops Program
    Return:Void
    Parameter: Void
    
    ------------------------------------------------------------------------------------------------------------*/
    	void LoopQuestion (void)
    {
    	cout<<endl;
    	cout <<"Do you want to continue (Y/N)?";
    	cin >>sRepeat;
    	sRepeat = toupper(sRepeat);
    	system("cls");
    	cin.ignore(); //tc
    	return;
    	
    	}
    
    /*-----------------------------------------------------------------------------------------------------------
    Name:Display Header
    Desc:Display Header
    Return:Void
    Parameter: Void
    
    ------------------------------------------------------------------------------------------------------------*/
    
    	void DisplayHeader (void)
    {
    	system("cls");
    	Dashes();
    	return;
    	}
    
    /*-----------------------------------------------------------------------------------------------------------
    Name:Dashes
    Desc:Displays Dashes
    Return:Void
    Parameter: Void
    
    ------------------------------------------------------------------------------------------------------------*/
    
    	void Dashes (void)
    {
    	int iCounter=0;
    	
    	for(iCounter = 0; iCounter <=79; ++iCounter)
    	cout<< "*";
    
    	return;
    	}
    
    
    
    /*-----------------------------------------------------------------------------------------------------------
    Name:Center
    Desc:Centers Text
    
    
    ------------------------------------------------------------------------------------------------------------*/
    
    void Center(char *s)
    {
      int len;
    
      len = 42+(strlen(s)/2);
    
      cout.width(len);
      cout << s << '\n';
    }
     
  8. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    Code:
    if (sState=="GA")
    
    Yep, that's precisely what I said you CAN'T do. To compare strings you have to use strcmp:
    Code:
    if (!strcmp(sState,"GA"))
    
     
  9. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    There is ONE scenario in which state=="GA" will work, and I've used it in the past as a space optimisation technique. If state is a char* and NOT a char[], AND you specify a compiler flag that merges (i.e. stores at the same location in static data) duplicate strings so "GA" will always evaluate as same memory location, AND you initialise state with those same strings, e.g. char *state="GA", THEN AND ONLY THEN can you expect state=="GA" to return TRUE.

    The only other way to make something like state=="GA" return TRUE is to declare state as a C++ std::string. This class implements operator==(char*) (or something similar) and basically performs a strcmp behind the scenes for you.
     
  10. carvin317

    carvin317 New Member

    Joined:
    Feb 6, 2009
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    okay so now im onto data validation. When it asks for how much a gallon of pain is i want the numeric number to be between 1-50 if it is not i want a message to display enter a number between 1-50 and have the question asked again and then when they do it move onto the next question. would that be a do while loop ? am i on the right track?


    Code:
    
    do{
    	cout <<"What is the cost of paint per gallon?";
    		cin >> Fcostper;
    		cout<<endl;	
    }	
    while (Fcostper < 0 || Fcostper >50)
    		cout<<"What state are you located?";
    		cin>>sState;
    		cout<<endl;
    
     
  11. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    Well, does it work? If so why do you need to ask if it's right? If not, what does it do wrong?
     
  12. carvin317

    carvin317 New Member

    Joined:
    Feb 6, 2009
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    the code works, but i dont know where to put the error message please enter a number between 1-50.?

    so if they entered 0 the message would say Error:Enter a number 1 -50 than ask for the input again.
     
  13. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    Probably you need some code that works along these lines:
    Code:
    for(;;)
    {
      get number
      if in range break
      else print error
    }
    
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice