First C++ App!!!

This is a discussion on First C++ App!!! within the C++ Programming forums, part of the General Programming Boards category; Heres my first c++ app. Tell me what you think. Code: #include <iostream.h> int main() { char vPick1; char vPick2; ...

  1. #1
    Programmer in Training TWIXMIX's Avatar
    Join Date
    Feb 2004
    Posts
    57

    First C++ App!!!

    Heres my first c++ app.
    Tell me what you think.
    Code:
    #include <iostream.h>
    
    int main()
    {
    
    char vPick1;
    char vPick2;
    double vNum1;
    double vNum2;
    double vAnswer;
    
    start:
        cout << "\n\n***************************";
        cout << "\n*******Calc-4U-Later*******";
        cout << "\n*Written by : Ryan Nielson*";
        cout << "\n***************************\n\n";
    
    
        cout << "\n\nWould you like to [a]dd, [s]ubtract, [m]ultiply or [d]ivide : ";
        cin >> vPick1;
    if (vPick1 == 'a')
    {
        cout << "\n\nPlease enter 2 numbers spaced apart and the calculator will add them : ";
        cin >> vNum1;
        cin>> vNum2;
        vAnswer = vNum1 + vNum2;
        cout << "\n\nThe answer is : " << vAnswer;
    }
    if (vPick1 == 's')
    {
        cout << "\n\nPlease enter 2 numbers spaced apart and the calculator will subtract them : ";
        cin >> vNum1;
        cin>> vNum2;
        vAnswer = vNum1 - vNum2;
        cout << "\n\nThe answer is : " << vAnswer;
    }
    if (vPick1 == 'm')
    {
        cout << "\n\nPlease enter 2 numbers spaced apart and the calculator will multiply them : ";
        cin >> vNum1;
        cin>> vNum2;
        vAnswer = vNum1 * vNum2;
        cout << "\n\nThe answer is : " << vAnswer;
    }
    if (vPick1 == 'd')
    {
        cout << "\n\nPlease enter 2 numbers spaced apart and the calculator will divide them : ";
        cin >> vNum1;
        cin >> vNum2;
        vAnswer = vNum1 / vNum2;
        cout << "\n\nThe answer is : " << vAnswer;
    }
        cout << "\n\nWould you like to [c]ontinue or [e]xit ? : ";
        cin >> vPick2;
    if (vPick2 == 'c')
    {
        goto start;
    }
    else
    {
        return 0;
    }
    }

  2. #2
    The Defective GRAPE Lurker's Avatar
    Join Date
    Feb 2003
    Posts
    949
    BEFORE YOU DO ANYTHING ELSE, remove the word "goto" from your vocabulary.
    Do not make direct eye contact with me.

  3. #3
    Registered User
    Join Date
    Jun 2002
    Posts
    230
    Code:
        cout << "\n\nWould you like to [c]ontinue or [e]xit ? : ";
        cin >> vPick2;
    if (vPick2 == 'c')
    {
        goto start;
    }
    else
    {
        return 0;
    }
    use a loop for this. seems like you want to run the prog at least once so use a do while loop.
    C++ Rules!!!!
    ------------
    Microsoft Visual Studio .NET Enterprise

  4. #4
    Programmer in Training TWIXMIX's Avatar
    Join Date
    Feb 2004
    Posts
    57
    Whats wrong with goto?

  5. #5
    Programmer in Training TWIXMIX's Avatar
    Join Date
    Feb 2004
    Posts
    57
    Code:
    #include <iostream.h>
    
    int main()
    {
    
    
    char vPick1;
    char vPick2 = 'c';
    double vNum1;
    double vNum2;
    double vAnswer;
    
    while (vPick2)
    {
        cout << "\n\n***************************";
        cout << "\n*******Calc-4U-Later*******";
        cout << "\n*Written by : Ryan Nielson*";
        cout << "\n***************************\n\n";
    
    
        cout << "\n\nWould you like to [a]dd, [s]ubtract, [m]ultiply or [d]ivide : ";
        cin >> vPick1;
    if (vPick1 == 'a')
    {
        cout << "\n\nPlease enter 2 numbers spaced apart and the calculator will add them : ";
        cin >> vNum1;
        cin>> vNum2;
        vAnswer = vNum1 + vNum2;
        cout << "\n\nThe answer is : " << vAnswer;
    }
    if (vPick1 == 's')
    {
        cout << "\n\nPlease enter 2 numbers spaced apart and the calculator will subtract them : ";
        cin >> vNum1;
        cin>> vNum2;
        vAnswer = vNum1 - vNum2;
        cout << "\n\nThe answer is : " << vAnswer;
    }
    if (vPick1 == 'm')
    {
        cout << "\n\nPlease enter 2 numbers spaced apart and the calculator will multiply them : ";
        cin >> vNum1;
        cin>> vNum2;
        vAnswer = vNum1 * vNum2;
        cout << "\n\nThe answer is : " << vAnswer;
    }
    if (vPick1 == 'd')
    {
        cout << "\n\nPlease enter 2 numbers spaced apart and the calculator will divide them : ";
        cin >> vNum1;
        cin >> vNum2;
        vAnswer = vNum1 / vNum2;
        cout << "\n\nThe answer is : " << vAnswer;
    }
        cout << "\n\nWould you like to [c]ontinue or [e]xit ? : ";
        cin >> vPick2;
    
    }
    }
    There i added a while loop.
    Hows that now?

  6. #6
    Registered User
    Join Date
    Jun 2002
    Posts
    230
    Whats wrong with goto?
    There isnt anything WRONG w/ goto. It is perfectly legal in C++. the bad thing about it is that it is just bad programming. usually 99.9% of the time there is a easier way and a better way of doing something than using goto. it is also difficult to read code w/ goto b/c you have to look all over the place. for your sake just avoid it.
    C++ Rules!!!!
    ------------
    Microsoft Visual Studio .NET Enterprise

  7. #7
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,157
    Originally posted by TWIXMIX
    There i added a while loop.
    Hows that now?
    Well, since you asked....

    Generally, it's not bad at all. Now on to the nitpicking

    1) Format properly. Every { you use should indent everything after it to the matching } -- including the one after main()

    2) Every one of your if()'s is constructed:
    Code:
    if (vPick1 == val)
    {
        output "*** text about entering 2 numbers***: ";
        input Num1
        input Num2
        calculate answer
        output answer
    }
    This is very redundant. If you think about it a little, I'll bet you can shorten your code by over 75%.

    3) What happens if the CAPS-LOCK is on?

    4) Have you gotten to the switch() statement yet?
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  8. #8
    Registered User Paz_Rax's Avatar
    Join Date
    Jan 2004
    Posts
    16
    very well done, suggestions: drop goto, and use a switch statment.
    "Life, it's all in how you script it."

  9. #9
    ResurgentBarbecue UnclePunker's Avatar
    Join Date
    May 2002
    Posts
    128
    Looks like you have done really well mate, people are right about goto it will make your life easier not using it, but your layout looks fine to me, I found it well easy to read.
    Compiler == Visual C++ 6.0
    "Come Out Fighting."

  10. #10
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787

    Re: First C++ App!!!

    more nitpicking... most of this has been covered by other members, except (I think) the iostream part...
    Code:
    //#include <iostream.h>
    //should be
    #include <iostream>
    using namespace std; //good enough for now
    
    int main()
    {
    
    char vPick1;
    char vPick2;
    double vNum1;
    double vNum2;
    double vAnswer;
    
    start:
        cout << "\n\n***************************";
        cout << "\n*******Calc-4U-Later*******";
        cout << "\n*Written by : Ryan Nielson*";
        cout << "\n***************************\n\n";
    
    
        cout << "\n\nWould you like to [a]dd, [s]ubtract, [m]ultiply or [d]ivide : ";
        cin >> vPick1;
    if (vPick1 == 'a') //and if the enter 'A'?
    {
        cout << "\n\nPlease enter 2 numbers spaced apart and the calculator will add them : ";
        cin >> vNum1;
        cin>> vNum2;
        vAnswer = vNum1 + vNum2;
        cout << "\n\nThe answer is : " << vAnswer;
    }
    if (vPick1 == 's') //try an 'else if'
    {
        cout << "\n\nPlease enter 2 numbers spaced apart and the calculator will subtract them : ";
        cin >> vNum1;
        cin>> vNum2;
        vAnswer = vNum1 - vNum2;
        cout << "\n\nThe answer is : " << vAnswer;
    }
    if (vPick1 == 'm')
    {
        cout << "\n\nPlease enter 2 numbers spaced apart and the calculator will multiply them : ";
        cin >> vNum1;
        cin>> vNum2;
        vAnswer = vNum1 * vNum2;
        cout << "\n\nThe answer is : " << vAnswer;
    }
    if (vPick1 == 'd')
    {
        cout << "\n\nPlease enter 2 numbers spaced apart and the calculator will divide them : ";
        cin >> vNum1;
        cin >> vNum2;
        vAnswer = vNum1 / vNum2;
        cout << "\n\nThe answer is : " << vAnswer;
    } //and if they enter 'Z'?
        cout << "\n\nWould you like to [c]ontinue or [e]xit ? : ";
        cin >> vPick2;
    
    /***  Personal Preference: change this ***
    if (vPick2 == 'c')
    {
        goto start;
    }
    *** to this ***/
    
    if(vPick2=='c')
       goto start; //avoid goto like the plague
    
    /*** try not to encapsulate a return ***
    else
    {
        return 0;
    }
    *** this does the same thing ***/
    return 0;
    }
    Last edited by major_small; 02-26-2004 at 10:16 AM.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  11. #11
    Registered User cyberCLoWn's Avatar
    Join Date
    Dec 2003
    Location
    South Africa
    Posts
    124
    When you get to functions and switches a good place to practise them on is this program. Keep going

  12. #12
    Programmer in Training TWIXMIX's Avatar
    Join Date
    Feb 2004
    Posts
    57
    Alright guys thanks alot for the comments and help

  13. #13
    Programmer in Training TWIXMIX's Avatar
    Join Date
    Feb 2004
    Posts
    57
    Now it doesnt work.
    I shortened my code up and that works fine but now when you type 'e' and the end to exit it loops bad around instead of exiting.

    Also how do I make the program so it doesn't matter weither you type capital or lower-case latter?

    Can someone please help!

    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
    char vPick1;
    char vPick2 = 'c';
    double vNum1, vNum2, vAnswer;
    
    while (vPick2)
    {
        cout << "\n\n***************************";
        cout << "\n*******Calc-4U-Later*******";
        cout << "\n*Written by : Ryan Nielson*";
        cout << "\n***************************\n\n";
        cout << "\n\nWould you like to [a]dd, [s]ubtract, [m]ultiply or [d]ivide : ";
        cin >> vPick1;
    if (vPick1 == 'a')
    {
        cout << "\n\nPlease enter 2 numbers spaced apart and the calculator will add them : ";
        cin >> vNum1 >> vNum2;
        vAnswer = vNum1 + vNum2;
    }
    if (vPick1 == 's')
    {
        cout << "\n\nPlease enter 2 numbers spaced apart and the calculator will subtract them : ";
        cin >> vNum1 >> vNum2;
        vAnswer = vNum1 - vNum2;
    }
    if (vPick1 == 'm')
    {
        cout << "\n\nPlease enter 2 numbers spaced apart and the calculator will multiply them : ";
        cin >> vNum1 >> vNum2;
        vAnswer = vNum1 * vNum2;
    }
    if (vPick1 == 'd')
    {
        cout << "\n\nPlease enter 2 numbers spaced apart and the calculator will divide them : ";
        cin >> vNum1 >> vNum2;
        vAnswer = vNum1 / vNum2;
    }
        cout << "\n\nThe answer is : " <<vAnswer;
        cout << "\n\n\nWould you like to [c]ontinue or [e]xit ? : ";
        cin >> vPick2;
    }
    return 0;
    
    }

  14. #14
    Registered User cyberCLoWn's Avatar
    Join Date
    Dec 2003
    Location
    South Africa
    Posts
    124
    A quick glance at your code... made a few changes. Go through it and compare it to yours.

    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
       char vPick1;
       double vNum1, vNum2, vAnswer;
    
       do {
           cout << "\n\n***************************";
           cout << "\n*******Calc-4U-Later*******";
           cout << "\n*Written by : Ryan Nielson*";
           cout << "\n***************************\n\n";
           cout << "\n\nWould you like to [a]dd, [s]ubtract, [m]ultiply or [d]ivide: ";
           cin >> vPick1;
           
           if (vPick1 == 'a')
           {
             cout << "\n\nPlease enter 2 numbers spaced apart and the calculator will add them : ";
             cin >> vNum1 >> vNum2;
             vAnswer = vNum1 + vNum2;
           }
           else if (vPick1 == 's')
           {
              cout << "\n\nPlease enter 2 numbers spaced apart and the calculator will subtract them : ";
              cin >> vNum1 >> vNum2;
              vAnswer = vNum1 - vNum2;
           }
           else if (vPick1 == 'm')
           {
              cout << "\n\nPlease enter 2 numbers spaced apart and the calculator will multiply them : ";
              cin >> vNum1 >> vNum2;
              vAnswer = vNum1 * vNum2;
           }
           else if (vPick1 == 'd')
           {
              cout << "\n\nPlease enter 2 numbers spaced apart and the calculator will divide them : ";
              cin >> vNum1 >> vNum2;
              vAnswer = vNum1 / vNum2;
           }
           cout << "\n\nThe answer is : " <<vAnswer;
           cout << "\n\n\nWould you like to [c]ontinue or [e]xit ? : ";
           cin >> vPick1;
       }  while ( (vPick1 != 'e') && (vPick1 != 'E') );
       
       return 0;
    }
    Last edited by cyberCLoWn; 02-26-2004 at 01:13 PM.

  15. #15
    Programmer in Training TWIXMIX's Avatar
    Join Date
    Feb 2004
    Posts
    57
    nevermind it works.
    Thanks alot man i appretiate it

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. non-MFC DLL with MFC app question.
    By Kempelen in forum Windows Programming
    Replies: 10
    Last Post: 08-20-2008, 07:11 AM
  2. best program to start
    By gooddevil in forum Networking/Device Communication
    Replies: 4
    Last Post: 05-28-2004, 05:56 PM
  3. Need help migrating console app to windows app
    By DelphiGuy in forum C++ Programming
    Replies: 1
    Last Post: 03-14-2004, 06:05 PM
  4. pasword app
    By GanglyLamb in forum C Programming
    Replies: 2
    Last Post: 06-07-2003, 10:28 AM
  5. How do I make my Linux app standalone?
    By Joda in forum C++ Programming
    Replies: 2
    Last Post: 11-27-2002, 03:53 AM

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