Need help in c programming

andrej192

Prominent
Jul 7, 2017
1
0
510
0
#include <stdio.h>
#include <stdlib.h>

int main()
{//online test, 3 questions

char capital;

printf("Question number 1: \n");
printf("What is the capital of Spain? \n");
scanf(" %s", &capital);

if(capital == "Madrid"){
printf("Correct, move on to question 2 \n");
}
if(capital != "Madrid"){
printf("Incorrect, start over");
}


return 0;
}
I am having a problem here, whenever i run the program it always gives me the answer "Incorrect, start over", how do i fix it so it displays correctly?
 

MeesterYellow

Commendable
Jan 12, 2017
25
0
1,610
8
Yeah, like what rgd1101 said, the char data type can only hold one character. You're going to have to use a char array like what ksham said.

Also, in addition to what ksham was talking about. If there are only two outcomes, use if and else. If you got a few outcomes, you can use if with else if statements, and else at the end for the last resort.
 

ksham

Honorable
Mar 29, 2013
175
0
10,760
25
1. Change capital to capital[50] (or whatever length).
2. You can't compare strings with ==. Use strcmp().
3. There's no need for the second if. Use else.
 

MeesterYellow

Commendable
Jan 12, 2017
25
0
1,610
8
Yeah, like what rgd1101 said, the char data type can only hold one character. You're going to have to use a char array like what ksham said.

Also, in addition to what ksham was talking about. If there are only two outcomes, use if and else. If you got a few outcomes, you can use if with else if statements, and else at the end for the last resort.
 
Thread starter Similar threads Forum Replies Date
aymen9309 Apps General Discussion 1
SyndiShanX Apps General Discussion 2
D Apps General Discussion 1
BlueMoon1890 Apps General Discussion 0
Neon The (Anthro) Umbreon Apps General Discussion 1
K Apps General Discussion 2
T Apps General Discussion 10
M Apps General Discussion 2
R Apps General Discussion 3
S Apps General Discussion 2
N Apps General Discussion 1
P Apps General Discussion 4
A Apps General Discussion 8
M Apps General Discussion 6
S Apps General Discussion 1
C Apps General Discussion 5
H Apps General Discussion 3
A Apps General Discussion 1
U Apps General Discussion 1
F Apps General Discussion 3

ASK THE COMMUNITY