1

I'm trying to make a program that calculates how many states you need to win. I have the main part of it done but now I'm trying to make a FOR loop that basically checks if the sum of how many votes you have now plus the number of votes the state has is greater than 270, the number to win. After that I'm trying to print that you will win if you win this state. At the end it will count and print out how many ways you can win. This what I have so far but it doesn't say that name of that States when I call them, and it also doesn't give me a 1, 2, or 3 number for how many ways you can win, just a large number that I'm not storing.

Can anyone help me?

#include <stdio.h>

int main(void){

    //Initialize your variables
    int current_votes, num_votes_1, num_votes_2, x;
    char name_state_1, name_state_2;


    //ask the user for the current number of electoral votes their candidate has
    printf("How many electoral votes has your candidate won?\n");
    scanf("%d", &current_votes);

    //Now ask for the first state in contention
    printf("What is the name of the first state in contention?\n");
    scanf("%s", &name_state_1);

    //now ask hiw many electoral votes the first state has
    printf("How many electoral votes does it have?\n");
    scanf("%d", &num_votes_1);

    //now ask for the second state in contention
    printf("What's the name of the second state in contention?\n");
    scanf("%s", &name_state_2);

    //now ask how many electoral votes the second state has
    printf("How many electoral votes does it have?\n");
    scanf("%d", &num_votes_2);

    //Now here's the formula that checks to see if you win in each state
    //and counts how many ways you can win
    for(x = 0; x < 3; x++) {
        if(current_votes + num_votes_1 >= 270);
            printf("Your candidate wins if he/she wins %s", &name_state_1);
            x++;
        if(current_votes + num_votes_2 >= 270);
            printf("Your candidate wins if he/she wins %s", &name_state_2);
            x++;
        if(current_votes + num_votes_1 + num_votes_2 >= 270);
            printf("your candidate wins if he/she wins %s", &name_state_1, &name_state_2);
            x++;

    }
    //now call to tell how many ways the candidate can win overall
    printf("your candidate can win %d ways", &x);
    return 0;

}
2
  • 1
    Start with declaring name_state_1 and name_state_2 as char arrays. Commented Mar 1, 2014 at 4:43
  • It seems to be a mix including python and shell ... Commented Mar 1, 2014 at 4:45

1 Answer 1

5

Your if contains empty statement

if(current_votes + num_votes_1 >= 270);

Remove the ;

Also c is not like python, indentation does not make code part of the if block.

if(current_votes + num_votes_1 >= 270)
    printf("Your candidate wins if he/she wins %s", &name_state_1);
    x++;

will always execute x++. Only the printf part is part of the if block. Use {} to enclose the code.

At the end, you are also printing the address of x which is why it is a big number. Finally, using x to count ways to win and also as your loop condition is not a good idea. The way I see it, you don't even need the loop.

This seems sufficient:

x = 0;
if(current_votes + num_votes_1 >= 270);
{
    printf("Your candidate wins if he/she wins %s", &name_state_1);
    x++;
}

if(current_votes + num_votes_2 >= 270);
{
    printf("Your candidate wins if he/she wins %s", &name_state_2);
    x++;
}

if(current_votes + num_votes_1 + num_votes_2 >= 270);
{
    printf("your candidate wins if he/she wins %s", &name_state_1, &name_state_2);
    x++;
}
Sign up to request clarification or add additional context in comments.

2 Comments

Also initialize the variables like the comment intends.. int x = 0;
@Gishu Yeah missed than one hehe

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.