Very Small First Program

Very Small First Program

Post by -Ninjex- on Tue Jan 22, 2013 2:30 am
([msg=72519]see Very Small First Program[/msg])

So yeah, I got bored, and I made a very small program in C++ in about 45m-1h
Just started learning C++ and I'm not so l337 at programming.

Anyways, by the end of my studies I will make a version 2 which will be a full blown calculator, or way better than this.
I plan on letting the user input how many numbers they would like to do math with, have a number selection instead of an operator selection for ease, (I could do that now) storing some of the variables in a enum and writing a switch statement for the inputs, and some other nerdy stuff that I either couldn't / didn't want to do the first go around.
Anywho, I am just posting this so that if you are starting in C++ you can look at this and ponder.
God this program is rough haha:

Code: Select all
#include <iostream>
#include <string>

/*All of the functions (a, b, c, d, e, and f) are aligned on the far left of the page by theirself for easy identification

*******************************************************************************
* _______        ____     _\   Fear no man     /_                             *
* ||||\\\\       ||||   _OOO\      that       /OOO_                           *
* |||| \\\\      ||||   OOOOO\    bleeds     /OOOOO                           *
* ||||  \\\\     ||||    OOO    __________     OOO  @_Ninjex_   ___     ___   *
* ||||   \\\\    ||||   ||||   ||||||||||||   |||||||||||||||   \\\\   ////   *
* ||||    \\\\   ||||   ||||   ||||    ||||   ||||   ||____||    \\\\ ////    *
* ||||     \\\\  ||||   ||||   ||||    ||||   ||||   ||||||||     |||||||     *
* ||||      \\\\ ||||___||||___||||    ||||   ||||   ||________  //// \\\\    *
* ||||       \\\\||||||||||||||||||    ||||   ||||   |||||||||||////   \\\\   *
***************************************||||___||||*****************************
                                      *|||||||||||*
                                      *************
*/

int main()
{
   using namespace std;
string x; //This variable will store answer variable for the password prompt
string y; //This variable will be used to store user input for the operator choice

/////////////////////////////////////////////////////////////////////////////////////
//    The following variables are used to store the operator & string values   /////
///////////////////////////////////////////////////////////////////////////////////
string plus = "+";                  //////////////////////////////////////////////
string minus = "-";                //////////////////////////////////////////////
string multi = "*";               //////////////////////////////////////////////
string sBack = "back";           //////////////////////////////////////////////
string sReturn = "exit";        //////////////////////////////////////////////
string sQuit = "quit";         //////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////

a: //Login function - Here we ask the user for the password, and give the option to exit the program

cout << "If you wish to close the program, type: 'quit'"; cout << endl;
cout << "Please enter the password to continue!"; cout << endl;
cin >> x;   //Setting x equal to user input, for password validation checking

if (x == sQuit) //If user input is 'quit' we go to f: the exit function
{
goto f;
}

   if (x != "ninjex") //If user input is not 'ninjex' we go to a: the login function
      {
         goto a;
      }
   else
      {
         cout << "How did you know the password was "; cout << x; cout << "?"; cout << endl;   

b: //Selection function - Here we get user input of an operator that they whish to carry out

         cout << "Please enter the operator you wish to use: +, -, or *"; cout << endl;
         cout << "You may type 'exit' to leave or 'back' to return to the login"; cout << endl;
         cin >> y;
      if (y == plus) // // // //If user input is '+' we go to c: the addition function
         {
            goto c;
         }
      else if (y == minus)   //If user input is '-' we go to d: the subtraction function
      {
         goto d;
      }
      else if (y == multi)   //If user input is '*' we go to e: which is the multiplication function
      {
         goto e;
      }
      else if (y == sBack)   //If user input is 'back' we go to a: the login function
      {
         goto a;
      }
      else if (y == sReturn) //If user input is 'exit' we go to f: the exit function
      {
         goto f;
      }
      else                   //If user input was not a valid choice, we loop them back to b: the selection function
         {
            goto b;   
         }
      }
c: //addition function - anum and anum2 store the value of the user's numbers
   cout << "Please enter a number: ";
   int anum;
   cin >> anum;
   cout << "Please enter another number: ";
   int anum2;
   cin >> anum2;
   cout << "The sum of: "; cout << anum; cout << " and "; cout << anum2; cout << " is: "; cout << anum + anum2; cout << endl; //Printing - The sum of: anum and anum2 is: anum + anum2
   goto b;

d: //substraction function - bnum and bnum2 store the value of the user's numbers

   cout << "Please enter a number: ";
   int bnum;
   cin >> bnum;
   cout << "Please enter another number: ";
   int bnum2;
   cin >> bnum2;
   cout << bnum; cout << " subtracted by "; cout << bnum2; cout << " is: "; cout << bnum - bnum2; cout << endl; //Printing - bnum subtracted by bnum2 is: bnum - bnum2
   goto b;

e: //multiplication function - mnum and mnum2 store the value of the user's numbers

   cout << "Please enter a number: ";
   int mnum;
   cin >> mnum;
   cout << "Please enter another number: ";
   int mnum2;
   cin >> mnum2;
   cout << mnum; cout << " multiplied by "; cout << mnum2; cout << " is: "; cout << mnum * mnum2; cout << endl; //Printing - mnum multiplied by mnum2 is: mnum * mnum2
   goto b;

f: //Exit function
return 0;   //Exits the program
}
If you're not willing to learn, no one can help you. If you're determined to learn, no one can stop you.⠠⠵
The absence of evidence is not evidence of absence.
I can explain it for you, but I can't understand it for you.
User avatar
-Ninjex-
Addict
Addict
 
Posts: 1253
Joined: Sun Sep 02, 2012 8:02 pm
Blog: View Blog (0)


Re: Very Small First Program

Post by Assassian360 on Tue Jan 22, 2013 8:38 am
([msg=72522]see Re: Very Small First Program[/msg])

Couple of suggestions you should follow:
* Don't use goto at all.
* Instead use functions and loops.
* Perhaps if you want to try practice your skills at programming in the sense of writing a calculator you could try and work toward writing it to use reverse polish notation.

"Goto" is a command that is rarely used by developers in C++ for quite a long time now. It's more in there to support older software still. The big problem with using goto is that it doesn't follow the conventions of program flow that you will need to be using for anything more complex. It also makes it far more difficult to reuse code. If you want to reuse code with a goto you either have to duplicate the code or place lots of additional code to redirect back to another section after the code has completed. This becomes very very messy.

Functions and loops are essential basic programming concepts. Based on the code you have included you are familiar with how to use if statements. If you are following a course than typically loops will follow if statements in curriculum. And functions/classes soon after that. Somewhere in there too will be something about using arrays. If you're in a class learning this material it wouldn't hurt to try and go ahead if you are feeling confident so you can get a head start on these sorts of things.

You can look up what reverse polish notation is, but it is a typical simple programming task that is often used for combining concepts when learning C++. Once you have used concepts such as arrays you could have your code very easily handle multiple inputs on the single line to determine a result. For example in reverse polish notation you could enter:
5 6 +
And the result would be 11. This is because the 5 and the 6 would be placed into an array and then when the operator appears you would take the first two elements in the array and add them to get the result.

Hope this helps.
Assassian360
Poster
Poster
 
Posts: 135
Joined: Sat Jun 26, 2010 1:37 am
Blog: View Blog (0)


Re: Very Small First Program

Post by fashizzlepop on Tue Jan 22, 2013 9:54 am
([msg=72524]see Re: Very Small First Program[/msg])

What assassian said. Also, I highly suggest you start learning c/c++ without using the string data type and instead use char arrays. You will learn a lot more.

It is incredibly improper to use Goto statements.
The glass is neither half-full nor half-empty; it's merely twice as big as it needs to be.
User avatar
fashizzlepop
Developer
Developer
 
Posts: 2303
Joined: Sat May 24, 2008 1:20 pm
Blog: View Blog (0)


Re: Very Small First Program

Post by -Ninjex- on Tue Jan 22, 2013 12:10 pm
([msg=72526]see Re: Very Small First Program[/msg])

Thanks for the words.
I am not in a class, I study this at home on my spare time, I have been learning for right at 3 days now.

I was planning on trying to make an array or a enum and then use a switch statemnet for the operator cases in the next version. However, I will look up polish notation, and how to use it.

My next version will be better, I'll put more than a hour of effort into it.
If you're not willing to learn, no one can help you. If you're determined to learn, no one can stop you.⠠⠵
The absence of evidence is not evidence of absence.
I can explain it for you, but I can't understand it for you.
User avatar
-Ninjex-
Addict
Addict
 
Posts: 1253
Joined: Sun Sep 02, 2012 8:02 pm
Blog: View Blog (0)


Re: Very Small First Program

Post by fashizzlepop on Tue Jan 22, 2013 12:18 pm
([msg=72527]see Re: Very Small First Program[/msg])

If you want to practice enums, sure, use a switch, but a simple if, else if block would do the trick especially since you have to call various functions based on the sign anyways.
The glass is neither half-full nor half-empty; it's merely twice as big as it needs to be.
User avatar
fashizzlepop
Developer
Developer
 
Posts: 2303
Joined: Sat May 24, 2008 1:20 pm
Blog: View Blog (0)


Re: Very Small First Program

Post by -Ninjex- on Tue Jan 22, 2013 3:34 pm
([msg=72528]see Re: Very Small First Program[/msg])

fashizzlepop wrote:If you want to practice enums, sure, use a switch, but a simple if, else if block would do the trick especially since you have to call various functions based on the sign anyways.


I am doing it all for practice / fun... I might even go with the macro defines...
I just started a youtube channel, and I am posting videos as I learn. It helps, since it makes sure I learn everything step by step and correctly. Thanks for the input again.
If you're not willing to learn, no one can help you. If you're determined to learn, no one can stop you.⠠⠵
The absence of evidence is not evidence of absence.
I can explain it for you, but I can't understand it for you.
User avatar
-Ninjex-
Addict
Addict
 
Posts: 1253
Joined: Sun Sep 02, 2012 8:02 pm
Blog: View Blog (0)


Re: Very Small First Program

Post by fashizzlepop on Tue Jan 22, 2013 4:01 pm
([msg=72529]see Re: Very Small First Program[/msg])

Macros? For a switch? That sounds like a terrible idea.
The glass is neither half-full nor half-empty; it's merely twice as big as it needs to be.
User avatar
fashizzlepop
Developer
Developer
 
Posts: 2303
Joined: Sat May 24, 2008 1:20 pm
Blog: View Blog (0)


Re: Very Small First Program

Post by LoGiCaL__ on Tue Jan 22, 2013 7:25 pm
([msg=72538]see Re: Very Small First Program[/msg])

Since it seems like you are sticking with C++ and not in a class I'll suggest a decent book. It's:

http://www.amazon.com/C-Ground-Up-Third-Edition/dp/0072228970/ref=sr_1_1?ie=UTF8&qid=1358900701&sr=8-1&keywords=c%2B%2B+from+the+ground+up

Yeah, I know it says C in the url but it's a c++ book.
User avatar
LoGiCaL__
Addict
Addict
 
Posts: 1060
Joined: Sun May 30, 2010 12:33 pm
Blog: View Blog (0)


Re: Very Small First Program

Post by -Ninjex- on Tue Jan 22, 2013 8:05 pm
([msg=72539]see Re: Very Small First Program[/msg])

LoGiCaL__ wrote:Since it seems like you are sticking with C++ and not in a class I'll suggest a decent book. It's:

http://www.amazon.com/C-Ground-Up-Third-Edition/dp/0072228970/ref=sr_1_1?ie=UTF8&qid=1358900701&sr=8-1&keywords=c%2B%2B+from+the+ground+up

Yeah, I know it says C in the url but it's a c++ book.


Thanks, Log. Maybe I can pull this from the Tor Library

-- Wed Feb 13, 2013 6:51 am --

Version number two, a bit better than the first lol

Still not the greatest, but shows my improvements.

Code: Select all
#include <iostream>
using namespace std;

int a;
int *pointA;

int b;
int *pointB;

int getA()
    {
        cout << "Enter the first number: ";
        pointA = &a;
        cin >> a;
        return a;
    }

int getB()
    {
        cout << "Enter the second number: ";
        cin >> b;
        pointB = &b;
        return b;
    }

int main()
    {
        char choice;
        while (choice != 'E')
        {

        cout << "[M]ultiplication - [D]ivision - [A]ddition - [S]ubtraction - [E]xit" << endl;
        cin >> choice;

        switch (choice)
            {
                case 'M':
                {
                    cout << "You have chosen multiplication" << endl;
                    getA();
                    getB();
                    cout << *pointA << " multiplied by " << *pointB << " is ";
                    cout << *pointA * *pointB;
                    cout << endl;
                }

                    break;
                case 'D':
                    {
                        cout << "You have chosen division" << endl;
                        getA();
                        getB();
                        cout << *pointA << " divided by " << *pointB << " is ";
                        cout << *pointA / *pointB;
                        cout << endl;
                    }
                    break;
                case 'A':
                    {
                        cout << "You have chosen addition" << endl;
                        getA();
                        getB();
                        cout << "The sum of " << *pointA << " and " << *pointB << " is ";
                        cout << *pointA + *pointB;
                        cout << endl;
                    }
                    break;
                case 'S':
                    {
                        cout << "You have chosen subtraction" << endl;
                        getA();
                        getB();
                        cout << *pointA << " subtracted by " << *pointB << " is ";
                        cout << *pointA - *pointB;
                        cout << endl;
                    }
                    break;
                case 'E':
                    {
                      cout << "You have chosen to exit" << endl;
                        int exit = 0; // adding a cin set to zero for a sort of pause
                        cin >> exit;
                    }
                    break;
                default:
                    {
                     cout << "Invalid entry" << endl;
                    }
                    break;
            }
        }
    }
If you're not willing to learn, no one can help you. If you're determined to learn, no one can stop you.⠠⠵
The absence of evidence is not evidence of absence.
I can explain it for you, but I can't understand it for you.
User avatar
-Ninjex-
Addict
Addict
 
Posts: 1253
Joined: Sun Sep 02, 2012 8:02 pm
Blog: View Blog (0)


Re: Very Small First Program

Post by limdis on Thu Feb 14, 2013 1:15 pm
([msg=73769]see Re: Very Small First Program[/msg])

Image
"The quieter you become, the more you are able to hear..."
"Drink all the booze, hack all the things."
User avatar
limdis
Moderator
Moderator
 
Posts: 1321
Joined: Mon Jun 28, 2010 5:45 pm
Blog: View Blog (0)


Next

Return to C and C++

Who is online

Users browsing this forum: No registered users and 0 guests

cron