Thread: brand new to c++ and having a problem wit some experimental coding

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    11

    brand new to c++ and having a problem wit some experimental coding

    im trying to make a program which lets you input 2 numbers and then select what to do to them (add subract or multiply). and as i have been reading the beginers tutorials on this site have been changing how i am doing it. currently i have it set up with 3 prototypes and some if rules to make each action work. it it however only registering the first if rule for some reason and no matter what numbers i input it always adds the 2 numbers. if any1 is willing to look at my coding coudl you pls post here so i can send it to you, or should i just put my code on a post?

    thanks

  2. #2
    Registered User
    Join Date
    Jan 2006
    Posts
    11
    ill post the code here to see if any1 can quickly see anything.
    just to warn you i am literally brand new to c++ (as in started today) and so any advice on how to inprove my methods would be appreciated.

    Code:
    #include <iostream>
    
    using namespace std;
    
    int add (int num1,int num2);
    int sub (int num1,int num2);
    int mult(int num1,int num2);
    
    
    int main()
    {
        int num1, num2, num3, num4;
        
            
        cout<<"please enter a  number: ";
        cin>> num1;
        
        cout<<"and another number: ";
        cin>>num2;
        
        cout<< "please pick a funtion(add=1, subtract=2, multiply=3) : ";
        cin>>num3;
        cin.ignore();
        
        if ( num3 = 1) {
                    num4=num1+num2;
                    cout<<"answer: "<<add (num1,num2)<<"\n";
                    }
        
        else if ( num3 = 2) {
                    num4=num1-num2;
                    cout<<"answer: "<<sub (num1,num2)<<"\n";
                    }
       
        else if ( num3 = 3 ) {
                    num4=num1*num2;
                    cout<<"answer: "<<mult (num1,num2)<<"\n";
                    }
       
                  num4 = num1+num2;
                  
        
        cin.get();
        }
        
        int add(int num1, int num2)
        {
            return num1+num2;
            }
          
        int sub (int num1, int num2)
        {
            return num1-num2;
            };
            
        int mult (int num1, int num2)
        {
            return num1*num2;
            }

  3. #3
    Registered User
    Join Date
    Jan 2006
    Posts
    63
    if(something = nothing)

    What you are doing there is not checking if something equals nothing, you are making something equal nothing.

    One equals sign "=" put what is on the right into the left.

    Two equals signs "==" means check if what is on the left is equal to what is on the right.

    Your code should look like

    if(something == nothing)

  4. #4
    Madly in anger with you
    Join Date
    Nov 2005
    Posts
    211
    use == instead of = to test for equality. = is the C assignment operator, and == tests for equality and returns true if equal and false if not. otherwise the rest of your program looks ok.

    edit: looks like someone beat me to it.
    Last edited by Bleech; 01-26-2006 at 01:32 AM.

  5. #5
    Registered User
    Join Date
    Jan 2006
    Posts
    11
    :P thanks. ill try to remember that one. it seems to work fine now.

  6. #6
    Registered User
    Join Date
    Jan 2006
    Posts
    11
    just 1 more thing, would it be simpler to do this with switches? (i just got to tutorial 5 :P)

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > just 1 more thing, would it be simpler to do this with switches?
    Try it and see.
    There's always another way, feel free to experiment with different solution ideas to the same problem.

  8. #8
    Madly in anger with you
    Join Date
    Nov 2005
    Posts
    211
    in your case a switch is un-necessary IMO. you are not testing alot of expressions, meaning the else if is probably a better bet. although yes, once you go testing lots of expressions it is better to use a switch to make things clearer (several nested ifs can get cryptic and hard to follow) and more compact.

    will also tell you for future use that the test:

    if(!something)

    is the equivalent of:

    if(something == 0)

    not important which one you use (no real difference in efficiency), but I'm telling you this just incase you see the ! conditional in use and wonder what it is. just like this:

    something == 0 ? something++ : something--;

    is the equivalent of:

    if(something == 0)
    something++;
    else
    something--;

    just some C stuff that the tutorial doesn't tell you about if else.
    Last edited by Bleech; 01-26-2006 at 01:46 AM.

  9. #9
    Registered User
    Join Date
    Jan 2006
    Posts
    11
    hmm, i should probably know this but what do the ++, --, ? and : do ?

  10. #10
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    increment and decrement opperators: add one to number (++) minus one from number (--); they can be used before or after a variable name, like this:

    something++;

    or this

    ++something;

    there position is only relevent when used in a loop's operation (i.e. the third statement in the for loop), and specifiy whether you want to increment/decrement before the loop starts or at the end of a loop. standalone as lines of code, it doesnt matter which position.
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  11. #11
    60% Braindead
    Join Date
    Dec 2005
    Posts
    379
    Ok,

    Code:
    a++;
    Returns the value of a, then adds one.

    So this will type in "yesyes"

    Code:
    a=2;
    if(a++==2)
     cout << "yes";
    if(a==3)
     cout << "yes";
    However, if you apply the ++'s to the end you get only "yes"

    Code:
    a=2;
    if(++a==2)
     cout << "yes";
    if(a==3)
     cout << "yes";
    Because:
    Code:
    ++a;
    returns a's value + 1.

    Same idea with a-- and --a.
    == Test for equity,
    = set to value.

    They're a bunch of other operators like <<, >>, and other things, but the most commonly used are ++', --', ==', and =.

    Have fun programming .
    Last edited by Blackroot; 01-26-2006 at 06:00 PM.
    Code:
    Error W8057 C:\\Life.cpp: Invalid number of arguments in function run(Brain *)

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > if(a++=2)
    wow
    Increment and assignment....

  13. #13
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    You could give your numbers more descriptive names. Like, method instead of num3, result instead of num4, ...
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  14. #14
    Registered User
    Join Date
    Jan 2006
    Posts
    11
    lol i just do that cos there is no way for me to get confused. i cant confuse num1 with num2 in a small program like this but im sure i will have to start making better names if i make any larger programs.

  15. #15
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Richie T
    there position is only relevent when used in a loop's operation (i.e. the third statement in the for loop), and specifiy whether you want to increment/decrement before the loop starts or at the end of a loop. standalone as lines of code, it doesnt matter which position.
    Using pre-increment (++x) vs post-increment (x++) doesn't affect behaviour of a loop as such.
    Code:
    for (int i = 0; i < some_max; ++i)
    {
        // whatever
    }
    achieves the same as
    Code:
    for (int i = 0; i < some_max; i++)
    {
        // whatever
    }
    with modern compilers. Technically there is a subtle difference (the second form can be slower due to need to hold a temporary) but modern compilers are able to recognise the fact there is no net difference, and optimise the difference out.

    Where pre-increment and post-increment are different is if the results of the expression are stored or used. So, for example;
    Code:
    #include <iostream>
    
    void something(int x)
    {
        std::cout << x << ',';
    }
    
    int main()
    {
         int value = 5;
         something(++value);
         std::cout << value << ',';
    
         value = 5;    // reset
         something(value++);
         std::cout << value << '\n';
    }
    will print the values 6,6,5,6 where the first and third values printed are the results of the different increment operators, and the second and third values output are the actual value in main().

Popular pages Recent additions subscribe to a feed