for statement

This is a discussion on for statement within the C Programming forums, part of the General Programming Boards category; this for statement is supposed to return a number other than one, but will always return one, showing that it ...

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    18

    for statement

    this for statement is supposed to return a number other than one, but will always return one, showing that it is not getting past the first try i believe. I changed this statement multiple times to about five lines of logic statements, this is its simplest form.
    Code:
    for(mchoice = 1; mchoice != choice && mchoice != car; mchoice++);
    the rest of the code is as follows:
    Code:
    #include<stdio.h>
    
    #include<stdlib.h>
    
    
    
    int main(void){
    
        int car, choice, mchoice;
    
        car = rand() % 3 + 1;
    
        printf("Which door would you like to pick? (1-3) ");
    
        scanf("%i", &choice);
    
        
    
        for(mchoice = 1; mchoice != choice && mchoice != car; mchoice++);
    
        
    
        printf("Monty shows you a goat behind door number %i\n", mchoice);
    
        
    
        return 0;
    
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Is there a reason you have a ; at the end of your for loop? I'm not even sure why you are using a loop here.
    Code:
    if( choice == car )
        ... you win a car
    else
       ... you get a goat behind choice door

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Sep 2010
    Posts
    18
    Quote Originally Posted by quzah View Post
    Is there a reason you have a ; at the end of your for loop? I'm not even sure why you are using a loop here.
    Code:
    if( choice == car )
        ... you win a car
    else
       ... you get a goat behind choice door

    Quzah.
    That loop is to generate a number other than the car and the persons choice, not respond with an answer. I am not sure how that if else logic would work for this. Can you elaborate?

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by spazx View Post
    That loop is to generate a number other than the car and the persons choice, not respond with an answer. I am not sure how that if else logic would work for this. Can you elaborate?
    Code:
    for each number
        if number not car and number not choice
            you see a goat

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Sep 2010
    Posts
    18
    Code:
    #include<stdio.h>
     
    #include<stdlib.h>
     
     
     
    int main(void){
     
        int car, choice, mchoice;
     
        car = rand() % 3 + 1;
     
        printf("Which door would you like to pick? (1-3) ");
     
        scanf("%i", &choice);
     
         
     
        for(mchoice = 1; mchoice == 3; mchoice++){
    	if (mchoice != car && mchoice != choice) {
                 printf("Monty shows you a goat behind door number %i\n", mchoice);
    	}
        }
        return 0;
    The code still does not work. What am i doing wrong?

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    mchoice is 1, so the truth test for your loop fails immediately, because you are testing to see if it's 3.


    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    Sep 2010
    Posts
    18
    Quote Originally Posted by quzah View Post
    mchoice is 1, so the truth test for your loop fails immediately, because you are testing to see if it's 3.


    Quzah.
    Wow. I see exactly what is wrong now. Thank you for all your help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. If Statement
    By Invecta in forum C Programming
    Replies: 3
    Last Post: 10-02-2010, 12:17 AM
  2. Statement inside a statement.
    By JOZZY& Wakko in forum C Programming
    Replies: 15
    Last Post: 11-05-2009, 02:18 PM
  3. if else statement ????
    By pczafer in forum C++ Programming
    Replies: 11
    Last Post: 04-29-2009, 08:45 AM
  4. if statement
    By Joe100 in forum Game Programming
    Replies: 1
    Last Post: 12-07-2003, 04:57 AM
  5. If Statement
    By boontune in forum C++ Programming
    Replies: 2
    Last Post: 09-17-2003, 07:56 AM

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