What am I doing wrong?!

This is a discussion on What am I doing wrong?! within the C++ Programming forums, part of the General Programming Boards category; I'm supposed to grab user input for name, hrs worked, pay rate, and tax bracket: all for four different employees. ...

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    8

    Unhappy What am I doing wrong?!

    I'm supposed to grab user input for name, hrs worked, pay rate, and tax bracket: all for four different employees. My code compiles all right, but when it runs, it doesn't wait for the user's name. It outputs like this:

    Please enter your full name
    Name:
    Enter your total hours worked

    Then loops endlessly when I enter hours.

    Could someone tell me what I'm missing?
    (code below)

    Code:
    #include <iostream.h>
    #include <iomanip.h>
    #include <string.h>
    
    
    
    
    
    float gross (float a, float b) //a=hrs, b=rate
    {
      float r;
      r=a*b;
      return r;
      
    }
    float tax (float c, float d) //c=gross, d=rate
    {
    	float t;
    	t=c*(d/100);
    	return t;
    
    }
    float net (float e, float f) //e=gross, f=tax
    {
    	float n;
    	n=e-f;
    	return n;
    }
    
     main()
    
    {
                
    
    	int num;
    	cout<<"Run Program? (1 for yes, 0 for no)"<<endl;
    	cin>>num;
    	while (num=1)
    		
    	{
    		  
    //input.......................
    		float hours;
    		float taxp;
    		float rate;
    		
    		
    		char mybuffer [30];
            cout<<"Please enter your full name"<<"\n";
    		cin.getline (mybuffer,30,'\n');
    		cout<<"Name: "<<mybuffer<<endl;
    		
    		cout<<"Enter your total hours worked"<<endl;
    
    		cin>>hours;
    	
    		cout<<"Enter your tax percentage"<<endl;
    
    		cin>>taxp;
    			
    		cout<<"Enter your pay rate"<<endl;
    	
    		cin>>rate;
    
    
    
    //output...............
    		
    		
    		
    		float z;
    		z= gross (hours,rate);
    		cout<<"Gross Pay: "<<gross<<endl;
    	
    
            float y;
    		y= tax ( z,taxp);
    		cout<<"Tax Paid: "<<tax<<endl;
    
    //totals.........................
    ;		
    	}	
    	
    
    
    					{
    	cout<<"Thank you!  Please come again!";
    	return 0;
    			}
    
    
    
    
    
    
    
     }

  2. #2
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,139
    If I start a program and get asked if I really wanted to start the program, I would be confused. Check your program flow, skip the first question and simply set that number to 1 the first time. Ask later in the loop. This solves your problem, but is not the reason for it. When you press enter for the first question it stays in the buffer and the getline gets it as the first char and stops there. Consult your helpfile for cin and how to clear input streams. I guess it's something like cin.clear();

    "while (num=1)" should be "while(num == 1)"

    float z;
    z= gross (hours,rate);
    cout<<"Gross Pay: "<<gross<<endl; // ?? should be z ??

    Same in the next block with tax.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    8
    cout<<"Gross Pay: "<<gross<<endl; // ?? should be z ??

    I cannot believe I just spent 2 and 1/2 hours on this because I forgot to reference my own variable. I'm off to die now.

    Oh, and thanks alot for pointing that out.

  4. #4
    Banned
    Join Date
    Jan 2003
    Posts
    1,708
    what ever happened to int in front of main. you have it returning 0 as it should, but I don't see an int anywhere, WHERE IS YOUR INT, AHHH...

  5. #5
    Registered User
    Join Date
    Oct 2002
    Posts
    291

    Re: What am I doing wrong?!

    Code:
    main()
    {
      //
    }
    change this to
    Code:
    int main( void )
    {
    
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 07-15-2004, 03:30 PM
  2. Debugging-Looking in the wrong places
    By JaWiB in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 11-03-2003, 09:50 PM
  3. Confused: What is wrong with void??
    By Machewy in forum C++ Programming
    Replies: 19
    Last Post: 04-15-2003, 12:40 PM
  4. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 11:01 AM
  5. Whats wrong?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 07-14-2002, 01:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21