Thread: If statement overusage?

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    22

    If statement overusage?

    Hi, I'm a n00b and have been doing the tutorials and I have many questions and you'll rpbably find me annoying because I really ask alot of questions daily. At the moment I have 3, I'm just gonna quote them cause I posted them on a pretty much dead board.


    I was making a rock paper scissors game and suddenly realised I had about 15 if statements and realised that this can't be the way your meant to do it. Is there another way to do it? or is it all done with if and else statements?

    When I was making all those if statements I wondered how you close a program? I know that it stops when you get to the bottom but how do you tell it to close after the users presses enter at a certain point, say in a if statment?

    Someone told me that this is how you make x a random number srand(time(0));
    int x = rand();

    But I have no idea where I'm mean to put it? Where do I put it?

    By the way, I'm up to lesson 9 on the tutorials if that means anything?

    Thanks

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >this can't be the way your meant to do it
    Well, there's nothing wrong with it, and by definition the game will require several choices.

    >how do you tell it to close after the users presses enter at a certain point, say in a if statment?
    If you want to return from the current function, use a return statement:
    Code:
    if (something)
      return value;
    If you want to terminate the program and you're in main, you can use the exit function from <cstdlib>:
    Code:
    if (something)
      exit(value);
    >Where do I put it?
    srand is called once, as soon as you can manage it. For example:
    Code:
    int main()
    {
      srand(static_cast<unsigned int>(time(0)));
    
      // The rest of your program here
    }
    rand is called whenever you need a random number after that.
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    if what you compare in those if-statements are integertype values you can use switch and case instead. Example of this:
    Code:
    int main()
    {
        int a=0;
        switch(a)
        {
        case 1:
            // do something
            break;    // This is needed otherwise it will "fall through", explenation below.
        case 2:
            // do something else
            break;
        case 0:
            return 0;
    }
    What is meant with fall through:
    Consider this code:
    Code:
    #include <iostream>
    
    int main()
    {
        int a;
        std::cin >> a;
        switch(a)
        {
        case 1:
            std::cout << "a = 1" << std::endl;
            break;
        case 2:
            std::cout << "a = 2" << std::endl;
            // notice: no break statement here.
        case 0:
            std::cout << "a = 0" << std::endl;
            break;
        default:
            std::cout << "Default action taken." << std::endl;
            break;
    }
    If you in this piece of code input 1 output will be:
    a = 1
    If you input 2 ouput will be:
    a = 2
    a = 0
    If you input something else output will be:
    Default action taken.

    Basicly the program will just start at the first case that matches and then it will go through all the other cases (even if they arent correct) and do what is in those until it hits a break or until no more cases are given.

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    1) If you're testing the same integerer value a lot, you can use a siwtch / case statement. Chars can also be tested. Other than that, if / else statements are fine.

    2) If you're in your main function, just return a value (make sure you're using int main and not void main) and that'll be it.

    3) Look in the FAQ, it shows you how to do random numbers. You need to seed the generator once before you assign random numbers. AS for WHEN you use rand(), just the same place as you would if you assigning some other value to it. Also make sure you do it after you use srand(). See the FAQ...

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    22
    Ok, I think I got it all. Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Usefulness of the "else if" statement
    By gn17 in forum C Programming
    Replies: 7
    Last Post: 08-12-2007, 05:19 AM
  2. Meaning of this statement?
    By @nthony in forum C Programming
    Replies: 7
    Last Post: 07-16-2006, 02:57 AM
  3. if/break statement
    By Apropos in forum C++ Programming
    Replies: 7
    Last Post: 02-22-2005, 02:33 PM
  4. string & if statement
    By Curacao in forum C++ Programming
    Replies: 4
    Last Post: 05-02-2003, 09:56 PM
  5. Uh-oh! I am having a major switch problem!
    By goodn in forum C Programming
    Replies: 4
    Last Post: 11-01-2001, 04:49 PM