Thread: Here is my code..what did i do?

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    18

    Here is my code..what did i do?

    This is the code i wrote for a simple calculator. The problem is that the choices aren't going through...if you compile it, you'll see.
    if you compile it, make sure to keep on trying numbers, because it goes through the different functions 1 at a time, the selection part is what isn't working, look:

    // calculator.cpp : Defines the entry point for the console application.
    //
    #include <iostream.h>
    #include <windows.h>

    int subtraction ()
    {

    int r,a,b;
    cout<<"Enter First Number"<<endl;
    cin>>a;
    cout<<"Enter Second Number"<<endl;
    cin>>b;
    r=a-b;
    cout<<"Subtraction Results:"<<r<<endl;
    int main();
    }
    int addition ()
    {
    int ad,c,d;
    cout<<"Enter First Number"<<endl;
    cin>>c;
    cout<<"Enter Second Number"<<endl;
    cin>>d;
    ad=c+d;
    cout<<"Addition Results:"<<ad<<endl;
    int main();
    }

    int multi ()
    {
    int mul,e,f;
    cout<<"Enter First Number"<<endl;
    cin>>e;
    cout<<"Enter Second Number"<<endl;
    cin>>f;
    mul=e*f;
    cout<<"Multiplication Results:"<<mul<<endl;
    int main();
    }


    int div ()
    {
    int quot,g,h;
    cout<<"Enter First Number (number to divide)"<<endl;
    cin>>g;
    cout<<"Enter Second Number (divided by)"<<endl;
    cin>>h;
    quot=g/h;
    cout<<"The Quotient Is:"<<quot<<endl;
    int main();
    }

    int squarert ()
    {
    int squa,i;
    cout<<"Enter the number to be Square rooted"<<endl;
    cin>>i;
    squa=sqrt(i);
    cout<<"The Square Root of"<<i<<"is:"<<squa<<endl;
    int main();
    }


    int main(int argc, char* argv[])
    {
    cout<<"Welcome to Smoose777's Calculator, version 1.0"<<endl;

    int com;
    cout<<"Type 1 now for a list of commands"<<endl;
    cin>>com;

    if (com = 1) {
    cout<<"List of commands:"<<endl;
    cout<<"Type 1 to access the addition part of the calculator"<<endl;
    cout<<"Type 2 to access subtraction part of the calculator"<<endl;
    cout<<"Type 3 for multiplication part of the calculator"<<endl;
    cout<<"Type 4 to access the division part of the calculator"<<endl;
    cout<<"Type 5 to access the square root part of the calculator"<<endl;

    int functio;
    cout<<"Please enter the function you would like to use now."<<endl;
    cin>>functio;

    if (functio = 1)
    {
    addition ();
    }
    if (functio = 2)
    {
    subtraction ();
    }
    if (functio = 3)
    {
    multi ();
    }
    if (functio = 4)
    {
    div ();
    }
    if (functio = 5)
    {
    squarert ();
    }
    else {
    cout<<"That is not a valid function"<<endl;
    }

    }
    return 0;
    }
    im new

  2. #2
    Hamster without a wheel iain's Avatar
    Join Date
    Aug 2001
    Posts
    1,385
    your problem is where you check for equality, you use a single = which is for assignment, to test for equality you must use ==

    for example where you have

    if (functio = 3)

    you should have

    if (functio == 3)


    hope this helps
    Monday - what a way to spend a seventh of your life

  3. #3
    Registered User
    Join Date
    Dec 2001
    Posts
    194
    you should NOT have the line "int main();" at the end of your functions. That should not compile, and if it does compile, your program will recursevly call itself which is not a good thing

  4. #4
    Registered User
    Join Date
    Nov 2001
    Posts
    162
    Also, since your functions aren't returning any values, you can set their return type to "void". EX:

    void subtraction()
    {
    ...
    return; //returns to the place where the function was called from
    //you don't need this unless you want to return in the middle of a function, but it works at the end just the same
    }

    Also, you can set your "int"s to "float"s and the user will be able to use decimals. Or even better is a double, which is twice as big.

    A little thing I learned by error was that if you enter 0 as your second division number (divisor), it will crash your program.

    You don't need to include windows.h, but it includes math.h which you do need to include for sqrt().

  5. #5
    Unregistered
    Guest

    pointlessly picky

    Have a look into switch statements, to get rid of all your ifs and it will make your code easier to read.

  6. #6
    Registered User
    Join Date
    Jan 2002
    Posts
    18
    THANKS a LOT!! it works now..the only thing is, how can i make the program go back to a certain spot in the code so that the program won't quit after the user multiplies a number the app won't quit. thanks. that is why i had the int main() because i had a wierd idea that it would go to the int main() part of the program over again guess it doesn't. any help is apreciated
    thanks again
    im new

  7. #7
    Registered User
    Join Date
    Jan 2002
    Posts
    559
    In main, put your code in a while loop, and give a choice, six maybe, to quit the program. If the user enters six, voila, the program ends.
    For readability, on your if statements, you don't need brackets if the code after the if statement is only one line.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Extended ASCII Characters in an RTF Control
    By JustMax in forum C Programming
    Replies: 18
    Last Post: 04-03-2009, 08:20 PM
  2. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 07:34 AM
  3. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 04:17 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 06:05 PM