Help with... (a loop I believe)

This is a discussion on Help with... (a loop I believe) within the C++ Programming forums, part of the General Programming Boards category; Hello...This is my first post here so it is a question and an introduction! Well I have recently started learning ...

  1. #1
    Registered User MoonMan's Avatar
    Join Date
    Nov 2011
    Posts
    19

    Smile Help with... (a loop I believe)

    Hello...This is my first post here so it is a question and an introduction!

    Well I have recently started learning about programming in general, so the whole thing is relatively new to me.. I am not currently taking any classes for programming so no need to worry about "doing my homework"... I do this because I want to so in turn I am learning it all on my own so I apologize in advance for any questions that may seem rather silly to a veteran programmer.. I have read part of the tutorial here and a book but I wanted to start to get experience with programming rather than continually just read about it.. (I learn much better actually doing it rather than reading it).

    So onto my program (that I need some help with!)
    I am trying to just make a basic calculations program for income/expenses using mainly numbers as input, I haven't quite learned about using chars well..

    After the first incomechoice I am using if and else if for input of choice 1 or 2, I have else if ( incomechoice > 2 ) to display a message of Invalid entry... but I am having trouble getting it to loop back to the original function of the Please pick 1 or 2.

    Any help is greatly appreciated and any comments on anything I should do/not do differently would be great.. This is my first program ever and so far I have written it with no help so please be gentle!

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        cout << "                            Welcome to Liquid Money!\n"<< endl;
        cout << "Please press enter to get started...\n";
    
    
    cin.get();
    
        float paycheck;
        float salary;
        int incomechoice;
        float expenses;
    
        cout<<"Is your income Hourly (1) or Salary (2)?\n"<< endl;
    
        cout<<"Please pick 1 or 2: ";
        cin>> incomechoice;
        cout<<"\n\n\n";
    
    
    
    
        if    ( incomechoice == 1 )
        {
    
        cout<<"What is your NET pay on an average week?\n";
        cin>> paycheck;
        cin.get();
        }
    
    
        else if ( incomechoice == 2 )
    {
    
            cout<<"What is your NET income for the year?\n";
            cin>> salary;
            cin.get();
    }
    
    
        else if ( incomechoice > 2 )
        {
            cout<<"Invalid entry...\n";
        }

  2. #2
    Registered User MoonMan's Avatar
    Join Date
    Nov 2011
    Posts
    19
    Okay well I figured out how to do it by using x < 4 and using a climbing number so I think it will repeat a few times then not work... But for now I added while ( x < 4 ) and for option 1 or 2 it will do x = x + 5 and for an invalid entry it will do x++..

    It works but is there some other way that I should do it? I basically just want to know if I am on the right track or not! Thanks

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        cout << "                            Welcome to Liquid Money!\n"<< endl;
        cout << "Please press enter to get started...\n";
    
    
    cin.get();
    
        float paycheck;
        float salary;
        int incomechoice;
        float expenses;
        int x = 0;
    
    while ( x < 4 )
    
    {
        cout<<"Is your income Hourly (1) or Salary (2)?\n"<< endl;
    
        cout<<"Please pick 1 or 2: ";
        cin>> incomechoice;
        cout<<"\n\n\n";
    
    
    
    
        if    ( incomechoice == 1 )
        {
    
        cout<<"What is your NET pay on an average week?\n";
        cin>> paycheck;
        cin.get();
        x = x + 5;
    
        }
    
    
        else if ( incomechoice == 2 )
    {
    
            cout<<"What is your NET income for the year?\n";
            cin>> salary;
            cin.get();
            x = x + 5;
    }
    
    
        else if ( incomechoice > 2 )
        {
            cout<<"Invalid entry...\n";
         x++;
    
        }
    
    }

  3. #3
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    Quote Originally Posted by MoonMan View Post
    Okay well I figured out how to do it by using x < 4 and using a climbing number so I think it will repeat a few times then not work... But for now I added while ( x < 4 ) and for option 1 or 2 it will do x = x + 5 and for an invalid entry it will do x++..

    It works but is there some other way that I should do it? I basically just want to know if I am on the right track or not! Thanks
    Some suggestions:
    1. Read up on the break and continue statements to control loops ...It will make your code not depend on 'magic numbers' .
    2. Indent properly.(Your current style is consistent ..but somewhat difficult to read)
    3. You're just taking input here...nothing is done with the data.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  4. #4
    Registered User MoonMan's Avatar
    Join Date
    Nov 2011
    Posts
    19
    Quote Originally Posted by manasij7479 View Post
    Some suggestions:
    1. Read up on the break and continue statements to control loops ...It will make your code not depend on 'magic numbers' .
    2. Indent properly.(Your current style is consistent ..but somewhat difficult to read)
    3. You're just taking input here...nothing is done with the data.
    Thank you very much for the help...Will definitely work on suggestions 1 and 2, but I have a question about 3..

    I will eventually use the input to do further calculations in the program, but as of now is the data that is being input by the user properly being stored for later use?

    IE: If the user puts 400.50 for the Net weekly check will that data store (as per the code now) so later I can do something like ( paycheck * 52 ) / 12 to get a monthly income?

    Thanks so much!

    Edit: Can break be used with numbers or does it have to be a keyword? So rather I would have the user type either Hourly or Salary instead of 1 or 2?
    Last edited by MoonMan; 12-09-2011 at 10:14 PM.

  5. #5
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    Quote Originally Posted by MoonMan View Post
    Edit: Can break be used with numbers or does it have to be a keyword? So rather I would have the user type either Hourly or Salary instead of 1 or 2?
    It is totally up to you how you use it..
    Only remember that it does ONE thing...that is break out from pretty much anything...
    So..as long as your logic is robust..you can use it without problems.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Nested while loop inside for loop
    By Sonny in forum C Programming
    Replies: 71
    Last Post: 07-31-2011, 09:38 PM
  2. Replies: 23
    Last Post: 04-05-2011, 04:40 PM
  3. for loop ignoring scanf inside loop
    By xIcyx in forum C Programming
    Replies: 2
    Last Post: 04-17-2007, 02:46 AM
  4. loop the loop - feeling foolish
    By estos in forum C Programming
    Replies: 2
    Last Post: 04-07-2007, 03:45 AM
  5. While loop ..crating an infanat loop
    By fmchrist in forum C Programming
    Replies: 7
    Last Post: 01-14-2006, 10:52 AM

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