1
    int p;
    int j;
    printf("Who is Palpatine?\n");
    scanf("%s", &j);
    if (j = "Senate") 
        {
        printf("Your number sir?\n");
        scanf("%d", &p);
        printf("CT %d, the time has come. Execute Order Sixty-Six.\n", p);
        }
    else
        {
        printf("Incorrect\n");
        }
     return 0;


}

I have been testing these lines of code and right now if I write anything in the prompt, it goes through the section where if I said Senate, even if I did not type Senate, and skips my else statement. The example in executed code picture is when I typed in Sen instead of Senate.

Also crossed out sections of the code in the executed code picture do not deal with the question, and my spacing is atrocious.

Thanks and all feedback is appreciated.

Executed Code

Picture of code

Jstinii
  • 23
  • 4

2 Answers2

0

declare j as a string to hold the senate, then in your scanf(), only do "j", not &j, since it is a string. Also, do if j == the Senate, rather than j = the senate. One = is assignment but two is checking equality.

Hope this helps!

edit: use strcmp(j, "the Senate")

0

If you noticed in your code, you scanf statement is wrong.
This is your scanf statement:

scanf("%s", &j);

If you noticed you are using %s in the statement with &j. Earlier in the code, you stated that j is an integer. Therefore, you should be using %d. To fix your code, make your j into a string to store letters. To do this, click this link and it will take you to a StackOverflow question on to create a string-type variable.

Also, in you if statement change: if (j = "Senate") to if (j = 'Senate'). You WILL get an error when if you do it with "".

Community
  • 1
  • 1
Manav Dubey
  • 780
  • 11
  • 26
  • 1
    Hey thanks for the answer while I did look at the link and it helped I did if(j == 'Senate'), to show for equality, I did char j[7]; and scanf("%6s", j); but I get the error "too many characters in an characters constant." – Jstinii Mar 22 '17 at 03:15
  • You are getting that error because it contains way too many letters in it. It has to contain one character for some reason. You could look up your problem online for a solution. – Manav Dubey Mar 22 '17 at 20:43