variable types

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • compsci
    New Member
    • Jan 2007
    • 32

    variable types

    I have my program but Im having problems printing my variables to the screen.


    Code:
    #include<iostream>
    #include <string>
    
    using namespace std;
    
    int overtime(int ); 
    
    int federal(int );
     
    int main ()
    {
    	//overtime(int hours);
    	int extra, hours, x, y, z, a, b,d;
    	
    	
    	cout << "Enter first name" <<endl;
    	cin >> x ;
    	
    	cout << "Enter last name" <<endl ;
    	cin >> y;
    
    	cout<< "Enter ssn"  <<endl;
    	cin >> z;
    
    	cout << "Enter phone number"  <<endl;
    	cin >> a;
    
    	cout << "Enter address" <<endl;
    	cin >> b;
    
    	cout << "Enter hours worked" <<endl;
    	cin >> hours;
    
    	cout << "Enter hourly wage" <<endl;
    	cin >>d;
    
    	cout << "You have worked more than 40 hours"<<endl;
    	cin>>extra;
    	
    	cout << overtime(hours)<<endl;
    
    return 0;
    }
    
    int overtime(int hours)
    {
    int extra;
    if(hours>40)
    extra = hours - 40;
    return extra;
    
    }
    
    int federal(int pay)
    {
    	int taxes;
    	int d;
    	int hours;
    	pay = d*hours;
    	taxes = pay * .08;
    	return taxes;
    
    }
  • horace1
    Recognized Expert Top Contributor
    • Nov 2006
    • 1510

    #2
    you main problem was that some of the data which you were trying to store in int variables was strings (sequences of characters), e.g. name, address, etc. This now works out the hours of overtime
    Code:
    #include<iostream>
    #include <string>
    
    using namespace std;
    
    int overtime(int ); 
    
    int federal(int );
     
    int main ()
    {
    	//overtime(int hours);
    	int extra, hours,d;
    	string x, y, z, a, b;  // ** these are strings not numbers!
    	
    	
    	cout << "Enter first name" <<endl;
    	cin >> x ;
    	
    	cout << "Enter last name" <<endl ;
    	cin >> y;
    
    	cout<< "Enter ssn"  <<endl;
    	cin >> z;
    
    	cout << "Enter phone number"  <<endl;
    	cin >> a;
    
    	cout << "Enter address" <<endl;
    	cin >> b;
    
    	cout << "Enter hours worked" <<endl;
    	cin >> hours;
    
    	cout << "Enter hourly wage" <<endl;
    	cin >>d;
    
    	cout << "You have worked more than 40 hours"<<endl;
    	//cin>>extra;  // ** not required
    	
    	cout << overtime(hours)<<endl;
         system("pause");
    return 0;
    }
    
    int overtime(int hours)
    {
    int extra;
    if(hours>40)
    extra = hours - 40;
    return extra;
    
    }
    
    int federal(int pay)
    {
    	int taxes;
    	int d;
    	int hours;
    	pay = d*hours;
    	taxes = pay * .08;
    	return taxes;
    
    }

    Comment

    • compsci
      New Member
      • Jan 2007
      • 32

      #3
      Thanks. I understand now.

      Comment

      Working...