Function not calling

This is a discussion on Function not calling within the C++ Programming forums, part of the General Programming Boards category; Hey, so I've been just practicing writing some simple programs trying to use loops and calling on functions. So I ...

  1. #1
    Registered User lpaulgib's Avatar
    Join Date
    May 2010
    Posts
    65

    Question Function not calling

    Hey, so I've been just practicing writing some simple programs trying to use loops and calling on functions. So I wrote this thing up real quick, and tried to get it to run, and I don't understand what's wrong. It's compiling with no errors or warnings, but every time I run it, the program just adds the two numbers no matter what I do. So I feel really stupid, but here it is. I've been staring at it for about 20 minutes making sure my FOR/ELSE loop is good and I've not forgotten anything obvious (I hope!)

    I wrote an add function and a subtract function. I know you guys can probably figure out what I'm trying to do, but this is an explanation of how I see it running.

    1. The user selects whether he wants to add or subtract.
    2. Based on the selection it's a for loop that calls on the correct function.
    3. The functions imports the stored numbers, does the math and posts it
    4. The program finishes.

    Code:
    #include <iostream>
    using namespace std;
    
    //subtract function
    int Subtract(int alpha, int omega){
        cout<<"The total of " << alpha << " minus " << omega << " is...\n";
        cout<< alpha - omega << endl;
        return 0;
    }
    
    //addition function
    int Additup(int apple, int orange){
        cout<<"The total of " << apple << " plus " << orange << "  is...\n";
        cout<< apple + orange << endl;
        return 0;
    }
    
    //main function time!
    
    int main(){
    
    int choice, num1, num2, num3;
    cout << "1 for addition, 2 for subtraction: ";
    cin >> choice;
    cin.ignore();
    
    if(choice = 1){
        cout<<"Enter the two numbers with a space inbetween then press enter: ";
        cin >> num1;
        cin >> num2;
        num3=Additup(num1,num2);
        }
    
    else{
        cout<<"Enter the two numbers then press enter: ";
        cin >> num1;
        cin >> num2;
        num3=Subtract(num1, num2);
        }
    
    
    return 0;
    }

    Also, one more thing. I understand that the code is both A) probably very inefficient for it's purpose, and B) might be a little sloppy. I know this, and I'm just working on the foundations right now. Thanks again for help!

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> if(choice = 1)

    Careful!
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  3. #3
    Registered User lpaulgib's Avatar
    Join Date
    May 2010
    Posts
    65
    Bah! I'm an idiot! Thank you!



    An explanation for the solution. My original if loop was this.

    Code:
    if(choice = 1){
        cout<<"Enter the two numbers with a space inbetween then press enter: ";
        cin >> num1;
        cin >> num2;
        num3=Additup(num1,num2);
        }
    The first line of that my operator was supposed to be if(variable equals value) { I did a Noob mistake and forgot that the relational operator for equal is not 1 equal sign, but two signs. So a comparison of right and wrong was....


    Wrong..
    if(choice = 1){

    Right
    if(choice == 1){


    Thanks Seba
    Last edited by lpaulgib; 06-10-2010 at 05:35 PM. Reason: Added explanation for my solution.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,668
    Which compiler are you using?

    If it's gcc, then try
    g++ -Wall prog.cpp

    For visual studio (recent flavours)
    cl /w4 prog.cpp
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,784
    Or more generally, project properties -> C/C++ -> General -> Warning Level -> Set to 4.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 02:07 AM
  2. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  3. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 08:56 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Question on function syntax and calling function
    By cbrman in forum C Programming
    Replies: 10
    Last Post: 10-05-2003, 05:32 PM

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