My if statements are screwing up

This is a discussion on My if statements are screwing up within the C++ Programming forums, part of the General Programming Boards category; There is something wrong with my program. Every time I run it it tells me that I have made an ...

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    6

    Question My if statements are screwing up

    There is something wrong with my program. Every time I run it it tells me that I have made an improper choice. I think it is a problem with my if statements, but could it be that there is something wrong somewhere else???? (I left out my "other_choice" function, so it would be easier to find the problem)

    I am using Dev C++ 4.9.8.0 on WinXP
    Code:
    #include <iostream>
    #include <cstdlib>
    #include <stdio.h>
    
    
    using namespace std;
    
    int main_choice(int choice)
    {
        cout<< "Please make a choice\n";
        cout<< "1) box\n";
        cout<< "2) cylinder\n";
        cout<< "3) sphere\n";
        cin>> choice;
        return choice;
        }
    
    
    
    
    int main()
    {
      int x;
      main_choice(x);
      if(x==1)
      {
        cout<< "You chose to make a box!!!\n";
        int y;
        other_choice(y);
        }
      else if(x==2)
      {
        cout<< "You chose to make a cylinder!!!\n";
        int y;
        other_choice(y);
        }
      else if(x==3)
      {
        cout<< "You chose to make a sphere!!!\n";
        int y;
        other_choice(y);
        }
      else 
      {
        cout<< "You did not make a proper choice\n";
        system("Pause");
        return 0;
        }       
      system("PAUSE");	
      return 0;
    }
    Thanx in advance
    Last edited by gooey kablooey; 03-18-2004 at 08:23 PM.

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    you are passing x by value, not by reference. In essence the value of x is being passed to main_choice, not x itself. the value is changed in main_choice but it is never sent back to main(). Change this:

    int main_choice(int choice)

    to this:

    int main_choice(int & choice)

    and everything should work. Of course then there would be no need to return an int from main_choice (unless you want to send back a success flag of some sort). Alternatively, don't sent x at all. Just make main_choice have no parameters. Then in main do this:

    x = main_choice();

    That should work, too.
    Last edited by elad; 03-18-2004 at 09:00 PM.

  3. #3
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    [edit]

    n/m

    [/edit]

  4. #4
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,156
    Don't do this:
    Originally posted by elad
    you are passing x by value, not by reference. In essence the value of x is being passed to main_choice, not x itself. the value is changed in main_choice but it is never sent back to main(). Change this:

    int main_choice(int choice)

    to this:

    int main_choice(int & choice)

    and everything should work. Of course then there would be no need to return an int from main_choice (unless you want to send back a success flag of some sort).
    Do this:
    Originally posted by elad
    Alternatively, don't sent x at all. Just make main_choice have no parameters. Then in main do this:

    x = main_choice();

    That should work, too.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  5. #5
    Registered User
    Join Date
    Mar 2004
    Posts
    220
    Your passing in x....to the function main_choice... And you haven't declared x's value yet? Check that.

    Your initializing a variable of int type x, and attempting to pass x to a function that takes a value of int, when you haven't defined x's value yet? Check that.
    OS: Windows XP Pro CE
    IDE: VS .NET 2002
    Preferred Language: C++.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unknown memory leak with linked lists...
    By RaDeuX in forum C Programming
    Replies: 6
    Last Post: 12-07-2008, 03:09 AM
  2. newbie question - if statements without conditions
    By c_h in forum C++ Programming
    Replies: 2
    Last Post: 07-18-2008, 10:42 AM
  3. Efficiency of case statements
    By Yasir_Malik in forum C Programming
    Replies: 26
    Last Post: 05-23-2006, 11:36 AM
  4. Class Function Members and If Statements
    By Team Shadow V2 in forum C++ Programming
    Replies: 10
    Last Post: 02-03-2005, 02:00 PM
  5. Switch statements for strings
    By cxs00u in forum C++ Programming
    Replies: 5
    Last Post: 04-17-2002, 03:38 PM

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