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:
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:#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("%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; }
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.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; }
I'm somewhat at a loss for what to do ... =/



LinkBack URL
About LinkBacks


