if statement is evaluating incorrectly ...

This is a discussion on if statement is evaluating incorrectly ... within the C Programming forums, part of the General Programming Boards category; I'm in an intro to programming class is this is the 3rd assignment we've had. I had a very simple ...

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    11

    if statement is evaluating incorrectly ...

    I'm in an intro to programming class is this is the 3rd assignment we've had. I had a very simple task with this program, and is done, for the most part. I'm running in to a small problem though. The code is as follows:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define total_to_win 270
    int main(void) {
        int votes_won, state_b_votes;
        int state_a_votes, both_states;
        int bigger, smaller;
        int needs_to_win, ways_to_win;
        char state_a[20], state_b[20];
        printf("How many electoral votes has your candidate won?\n");
        scanf("&#37;d", &votes_won);
        printf("What is the name of the first state in contention?\n");
        scanf("%s", &state_a);
        printf("How many electoral votes does it have?\n");
        scanf("%d", &state_a_votes);
        printf("What is the name of the second state in contention?\n");
        scanf("%s", &state_b);
        printf("How many electoral votes does it have?\n");
        scanf("%d", &state_b_votes);
        
    
    printf("state a votes is %d\n", state_a_votes);
    printf("state b votes is %d\n", state_b_votes);
        
        
        if (state_a_votes > state_b_votes){
        bigger = state_a_votes;
        smaller = state_b_votes;
        }
        else {
             bigger = state_b_votes;
             smaller = state_a_votes;         
        }
             
        needs_to_win = total_to_win - votes_won;
        both_states = bigger + smaller;
        
        if (smaller >= needs_to_win) {
           if (smaller = state_a_votes){
           ways_to_win = 3;
           printf("Your candidate wins if he/she wins %s.\n", state_a);
           printf("Your candidate wins if he/she wins %s.\n", state_b);
           printf("Your candidate wins if he/she wins %s and %s.\n", state_a, state_b);
           printf("Your candidate can win in %d ways.\n", ways_to_win);}
           else {
           ways_to_win = 3;
           printf("Your candidate wins if he/she wins %s.\n", state_b);
           printf("Your candidate wins if he/she wins %s.\n", state_a);
           printf("Your candidate wins if he/she wins %s and %s.\n", state_b, state_a);
           printf("Your candidate can win in %d ways.\n", ways_to_win);}
    }
        else if (bigger >= needs_to_win) {
             if (bigger = state_a_votes){
             ways_to_win = 2;
             printf("Your candidate wins if he/she wins %s.\n", state_a);
             printf("Your candidate wins if he/she wins %s and %s.\n", state_a, state_b);
             printf("Your candidate can win in %d ways.\n", ways_to_win);}
             else {
             ways_to_win = 2;
             printf("Your candidate wins if he/she wins %s.\n", state_b);
             printf("Your candidate wins if he/she wins %s and %s.\n", state_b, state_a);
             printf("Your candidate can win in %d ways.\n", ways_to_win);}
    }
    
        else if (both_states >= needs_to_win){
        ways_to_win = 1;
        printf("Your candidate wins if he/she wins %s and %s.\n", state_a, state_b);
        printf("Your candidate can win in %d way.\n", ways_to_win);
    }
    
        else {
        ways_to_win = 0;
        printf("Your candidate is a loser.\n");
    }
    printf("bigger is %d.\n", bigger);
    printf("needs to win is %d.\n", needs_to_win);
    printf("state a votes is %d\n", state_a_votes);
    printf("state b votes is %d\n", state_b_votes);
         
        system("pause");
        return 0;
    }
    Note the addition of the printf's to make sure state_a_votes, state_b_votes, and bigger are evaluating correctly. My problem lies in this part of the code:

    Code:
    if (state_a_votes > state_b_votes){
        bigger = state_a_votes;
        smaller = state_b_votes;
        }
        else {
             bigger = state_b_votes;
             smaller = state_a_votes;         
        }
    This if statement is constantly evaluating to true and setting bigger to state_a_votes regardless of my input. The printf's have show me the compiler is adjusting state_a_votes and state_b_votes according to user input, but when state_a_votes is smaller than state_b_votes, that first conditional evaluates to true regardless.

    I'm somewhat at a loss for what to do ... =/

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    "This if statement is constantly evaluating to true and setting bigger to state_a_votes regardless of my input."

    My input must have better resonance, because:
    Code:
    How many electoral votes has your candidate won?
    4
    What is the name of the first state in contention?
    here
    How many electoral votes does it have?
    5
    What is the name of the second state in contention?
    mo
    How many electoral votes does it have?
    9
    state a votes is 5
    state b votes is 9
    Your candidate is a loser.
    bigger is 9.
    needs to win is 266.
    state a votes is 5
    state b votes is 9
    sh: pause: command not found
    It would appear the statement did evaluate correctly.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,652
    Code:
    if (smaller = state_a_votes){...
    }
    if (bigger = state_a_votes){ ...
    }
    Both of those statements reassign the value of smaller and bigger to state_a_votes.
    You mean to use == (equivalence) rather than = (assignment).

  4. #4
    Registered User
    Join Date
    Sep 2008
    Posts
    11
    You mean to use == (equivalence) rather than = (assignment).
    Yey! It worked!

    Thank you very much ^_^

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Evaluating multiple expressions in an If statement.
    By bitslap in forum C Programming
    Replies: 4
    Last Post: 11-01-2008, 04:38 AM
  2. Usefulness of the "else if" statement
    By gn17 in forum C Programming
    Replies: 7
    Last Post: 08-12-2007, 05:19 AM
  3. Meaning of this statement?
    By @nthony in forum C Programming
    Replies: 7
    Last Post: 07-16-2006, 02:57 AM
  4. If Else statement problem
    By doofusboy in forum C Programming
    Replies: 2
    Last Post: 11-09-2005, 06:18 AM
  5. Uh-oh! I am having a major switch problem!
    By goodn in forum C Programming
    Replies: 4
    Last Post: 11-01-2001, 03:49 PM

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