Thread: Help for a newcomer please.

  1. #1
    No Idea Programmer gasgastxtpro's Avatar
    Join Date
    Aug 2005
    Location
    I think I'm rather lost....
    Posts
    8

    Question Help for a newcomer please.

    Okay, i've only just started learning programming yesterday.

    After reading a few articles and tutorials on your site I have myself a compiler (Bloodshed DevC++) and an idea.

    I want to make a simple wages calculator, where you enter your hours worked and then your hourly rate, the programme multiplys these together and gives you your wages.

    I have got this far:

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        cout<<"Wages Calculator Version 0.1.1 by Richard Longstaff";
        
        cin.ignore();
        
        int thisisthehours;
        
        cout<<"Please enter your hours: ";
        cin>> thisisthehours;
        cin.ignore();
        
        int thisistherate;
        
        cout<<"Please enter your hourly rate: ";
        cin>> thisistherate;
        cin.ignore();
        
        int thisisthewages;
        
        ;thisisthewages = thisisthehours * thisistherate;
        cout<<"Your wages are: "<< thisisthewages <<"\n";
        cin.get();
    }
    But I noticed when i run the programme I have to press enter at the beginning after the title to start the programme.

    How can i make it so that it goes straight into asking your hours?

    I realise this question probably sounds pretty elemental to most of you, and I'm sorry if its too obvious an answer.

    Any help greatly appreciated.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Remove the cin.ignore(); between them
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    No Idea Programmer gasgastxtpro's Avatar
    Join Date
    Aug 2005
    Location
    I think I'm rather lost....
    Posts
    8
    Yeh, I was there before.

    The I wanted the sections to be on different lines so i typed that in and it worked but it takes me back to square one.

    I hope you understand this

    Any help appreciated.

  4. #4
    Registered Usurer
    Join Date
    Apr 2005
    Location
    upstate NY
    Posts
    79
    Use this:
    Code:
    cout<<"Wages Calculator Version 0.1.1 by Richard Longstaff\n";
    or this
    Code:
    cout<<"Wages Calculator Version 0.1.1 by Richard Longstaff"<<endl;

  5. #5
    No Idea Programmer gasgastxtpro's Avatar
    Join Date
    Aug 2005
    Location
    I think I'm rather lost....
    Posts
    8
    Thanks. Seems obvious now.

    Although you still have to press enter, I would have liked it as just a sort of title.

    But thanks for your help anyway

    Richard Longstaff

  6. #6
    No Idea Programmer gasgastxtpro's Avatar
    Join Date
    Aug 2005
    Location
    I think I'm rather lost....
    Posts
    8

    Unhappy

    why wont this work

    Code:
    #include <iostream>
    
    using namespace std;
    
    float main()
    {
          cout<<"Wages Calculator Version 0.2 by Richard Longstaff\n";
          cin.ignore();
          
          float thisisthehours;
          
          cout<<"Please enter your hours - for example 4.5 = 4 and a half hours: ";
          cin>> thisisthehours;
          cin.ignore();
          
          float thisistherate
          
          cout<<"Please enter your hourly rate - for example 4.5 = £4.50: ";
          cin>> thisistherate;
          cin.ignore();
          
          float thisisthewages;
          
          ;thisisthewages = thisisthehours * thisistherate;
          cout<<"Your wages are: "<< thisisthewages <<"\n";
          cin.get();
    }
    Richard Longstaff - Sorry for asking, I'll probably buy a book sometime soon, any suggestions for a good cheapish book in the uk?

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    It won't work because you didn't remove the cin.ignore() after the title.

    BTW, it should be int main(), not float main(). The main function must always specify int as its return type.

  8. #8
    No Idea Programmer gasgastxtpro's Avatar
    Join Date
    Aug 2005
    Location
    I think I'm rather lost....
    Posts
    8

    Talking

    Cheers mate

    Richard.

  9. #9
    *this
    Join Date
    Mar 2005
    Posts
    498
    I know the float has already been pointed out but there were a couple very small things that you might have missed. Unless you just caught it...

    Also as Daved pointed out the cin.ignore(), I compiled with dev-c++ and it works, although it doesnt need to be there.

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main() //main must return an int
    {
          cout<<"Wages Calculator Version 0.2 by Richard Longstaff\n";
          // dont need cin.ignore() here
          
          float thisisthehours;
          
          cout<<"Please enter your hours - for example 4.5 = 4 and a half hours: ";
          cin>> thisisthehours;
          cin.ignore();
          
          float thisistherate; // missing ";"
          
          cout<<"Please enter your hourly rate - for example 4.5 = £4.50: ";
          cin>> thisistherate;
          cin.ignore();
          
          float thisisthewages;
          
          thisisthewages = thisisthehours * thisistherate; // dont need the extra ";" here
          cout<<"Your wages are: "<< thisisthewages <<"\n";
          cin.get();
    }
    Last edited by JoshR; 08-20-2005 at 12:40 PM.

  10. #10
    No Idea Programmer gasgastxtpro's Avatar
    Join Date
    Aug 2005
    Location
    I think I'm rather lost....
    Posts
    8
    I now have this working:

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
          cout<<"Wages Calculator Version 1.0 by Richard Longstaff\n";
          
          //Monday//
          
          float thisisthehours1;
          
          cout<<"Please enter your hours for Monday: ";
          cin>> thisisthehours1;
          cin.ignore();
          
          float thisistherate1;
          
          cout<<"Please enter your hourly rate for Monday: ";
          cin>> thisistherate1;
          cin.ignore();
          
          float thisisthewages1;
          
          ;thisisthewages1 = thisisthehours1 * thisistherate1;
          cout<<"Your wages for Monday are: "<< thisisthewages1 <<"\n";
          cin.get();
          
          //Tuesday//
          
          float thisisthehours2;
          
          cout<<"Please enter your hours for Tuesday: ";
          cin>> thisisthehours2;
          cin.ignore();
          
          float thisistherate2;
          
          cout<<"Please enter your hourly rate for Tuesday: ";
          cin>> thisistherate2;
          cin.ignore();
          
          float thisisthewages2;
          
          ;thisisthewages2 = thisisthehours2 * thisistherate2;
          cout<<"Your wages for Tuesday are: "<< thisisthewages2 <<"\n";
          cin.get();
          
          //Wednesday//
          
          float thisisthehours3;
          
          cout<<"Please enter your hours for Wednesday: ";
          cin>> thisisthehours3;
          cin.ignore();
          
          float thisistherate3;
          
          cout<<"Please enter your hourly rate for Wednesday: ";
          cin>> thisistherate3;
          cin.ignore();
          
          float thisisthewages3;
          
          ;thisisthewages3 = thisisthehours3 * thisistherate3;
          cout<<"Your wages for Wednesday are: "<< thisisthewages3 <<"\n";
          cin.get();
          
          //Thursday//
          
          float thisisthehours4;
          
          cout<<"Please enter your hours for Thursday: ";
          cin>> thisisthehours4;
          cin.ignore();
          
          float thisistherate4;
          
          cout<<"Please enter your hourly rate for Thursday: ";
          cin>> thisistherate4;
          cin.ignore();
          
          float thisisthewages4;
          
          ;thisisthewages4 = thisisthehours4 * thisistherate4;
          cout<<"Your wages for Thursday are: "<< thisisthewages4 <<"\n";
          cin.get();
          
          //Friday//
          
          float thisisthehours5;
          
          cout<<"Please enter your hours for Friday: ";
          cin>> thisisthehours5;
          cin.ignore();
          
          float thisistherate5;
          
          cout<<"Please enter your hourly rate for Friday: ";
          cin>> thisistherate5;
          cin.ignore();
          
          float thisisthewages5;
          
          ;thisisthewages5 = thisisthehours5 * thisistherate5;
          cout<<"Your wages for Friday are: "<< thisisthewages5 <<"\n";
          cin.get();
          
          //Saturday//
          
          float thisisthehours6;
          
          cout<<"Please enter your hours for Saturday: ";
          cin>> thisisthehours6;
          cin.ignore();
          
          float thisistherate6;
          
          cout<<"Please enter your hourly rate for Saturday: ";
          cin>> thisistherate6;
          cin.ignore();
          
          float thisisthewages6;
          
          ;thisisthewages6 = thisisthehours6 * thisistherate6;
          cout<<"Your wages for Saturday are: "<< thisisthewages6 <<"\n";
          cin.get();
          
          //Sunday//
          
          float thisisthehours7;
          
          cout<<"Please enter your hours for Sunday: ";
          cin>> thisisthehours7;
          cin.ignore();
          
          float thisistherate7;
          
          cout<<"Please enter your hourly rate for Sunday: ";
          cin>> thisistherate7;
          cin.ignore();
          
          float thisisthewages7;
          
          ;thisisthewages7 = thisisthehours7 * thisistherate7;
          cout<<"Your wages for Sunday are: "<< thisisthewages7 <<"\n";
          cin.get();
          
          //Week//
          
          float thisistheweekswages;
          
          ;thisistheweekswages = thisisthewages1 + thisisthewages2 + thisisthewages3 + thisisthewages4 + thisisthewages5 + thisisthewages6 + thisisthewages7;
          cout<<"Your total earnings this week come to: "<< thisistheweekswages <<"\n"
          ;cin.get();
          
          //End//
    }
    Ive probably done this in an overly long way. Opinions on my first ever programme anyone lol

    Richard.

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Use an array and a for loop.

    Unless those are in the next chapter showing how to make this program much shorter
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  12. #12
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    It's definitely a start, but something that is good to know is a loop, and an array. This prevents you from writing out every single possible combination, and makes your code a lot smaller. For example, I made a program that's pretty similar to yours and, and I think it's still pretty readable for a beginner and gets the point across. Here's how it came out:

    Code:
    #include <iostream>
    using namespace std;
    
    const int daysPerWeek = 7;
    char* days[] = 
    {
    		"Sunday",
    		"Monday",
    		"Tuesday",
    		"Wednesday",
    		"Thursday",
    		"Friday",
    		"Saturaday"
    };
    
    int main(int argc, char* argv[])
    {
    	float hours[daysPerWeek];
    	float rate[daysPerWeek];
    	long int totalEarnings = 0;
    
    	// Get user input in a loop.
    	for(int i = 0; i < daysPerWeek; i++)
    	{
    		cout << "Enter hours worked on " << days[i] << ": ";
    		cin >> hours[i];
    		cout << "Enter hourly rate for " << days[i] << ": ";
    		cin >> rate[i];
    	}
    
    	// After they finish, give them some feedback
    	for(i = 0; i < daysPerWeek; i++)
    	{
    		cout << "Your wages for " << days[i] << " are: " << hours[i] * rate[i] << endl;
    		totalEarnings += hours[i] * rate[i];
    	}
    
    	cout << "Total money earned: " << totalEarnings;
    		
    	return 0;
    }
    If you don't understand anything, try looking up at some cplusplus.com tutorials, see if you can find anything similar, and if it really stumps you, youc an come back and ask here. A lot of the concepts I introduced aren't something that will just come to you at once, and will more just build as you make more and more example programs. Edit: Oh Salem getting to it before me....

  13. #13
    No Idea Programmer gasgastxtpro's Avatar
    Join Date
    Aug 2005
    Location
    I think I'm rather lost....
    Posts
    8
    Thanks i shall now move onto next chapter its true... :$

    Rich

  14. #14
    No Idea Programmer gasgastxtpro's Avatar
    Join Date
    Aug 2005
    Location
    I think I'm rather lost....
    Posts
    8
    Hey again everyone

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        cout<<"Wages Calculator Version 1.1 by Richard Longstaff\n";
        
       int days;
       cout<<"How many days have you worked this week?: ";
       cin>> days;
       cin.ignore();
       
       if (days = 1)
       
       float hours;
       
       cout<<"Please enter your hours: ";
       cin>> hours;
       cin.ignore();
       
       float rate;
       
       cout<<"Please enter your hourly rate: ";
       cin>> rate;
       cin.ignore();
       
       float wages;
       
       wages = hours * rate;
       cout<<"Your total wages come to: <<wages <<";
       cin.ignore();
    }
    I dont understand how are hours undeclared?

    Thats exactly how i've done it in my other programmes.

    Thanks

    Richard.

  15. #15
    Banned Yuri's Avatar
    Join Date
    Aug 2005
    Location
    Breukelen, The Netherlands
    Posts
    133
    Its because of "if (days = 1)". You should use have it like this:
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
       cout<<"Wages Calculator Version 1.1 by Richard Longstaff\n";
        
       int days;
       cout<<"How many days have you worked this week?: ";
       cin>> days;
       cin.ignore();
       
       if (days = 1){//use the {, other thing; I don't get this expresion.
       
       float hours;
       
       cout<<"Please enter your hours: ";
       cin>> hours;
       cin.ignore();
       
       float rate;
       
       cout<<"Please enter your hourly rate: ";
       cin>> rate;
       cin.ignore();
       
       float wages;
       
       wages = hours * rate;
       cout<<"Your total wages come to: <<wages <<";// this is wrong, should be: cout<<"Your total wages come to: "<<wages;//( <<endl; )
       cin.ignore();
       }
    }
    Or, is this what you want to do?
    Code:
    #include <iostream>
    
    using namespace std;
    
    int days;
    float hours;
    float rate;
    float wages;
    
    int main()
    {
       cout<<"Wages Calculator Version 1.1 by Richard Longstaff\n";
    
       cout<<"How many days have you worked this week?: ";
       cin>> days;
    
       cout<<"Please enter your hours: ";
       cin>> hours;
       cin.ignore();
       
       if ( days == 1 ){
       cin.get();
       }
    
       else{
    
       cout<<"Please enter your hourly rate: ";
       cin>> rate;
    
       wages = hours * rate;
       cout<<"Your total wages come to: "<<wages <<endl;
       cin.ignore();
       cin.get();
       }
    }
    Last edited by Yuri; 08-24-2005 at 10:12 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A Newcomer Question
    By anirban in forum Windows Programming
    Replies: 2
    Last Post: 04-18-2007, 01:26 PM
  2. Resource ICONs
    By gbaker in forum Windows Programming
    Replies: 4
    Last Post: 12-15-2003, 07:18 AM
  3. Python newcomer learning C
    By caroundw5h in forum C Programming
    Replies: 0
    Last Post: 10-22-2003, 03:11 PM
  4. Help out this newcomer
    By a_learner in forum C Programming
    Replies: 6
    Last Post: 10-06-2001, 02:10 PM