-
True or false statements
I was wondering if someone could point me in the right direction here with my code.The objective of the program is to ask the users name and display their name before asking them a question. The program ask the user if "Europe is bigger than Asia?"
"The program must include 2 string answers(one if user is correct, and one if user is wrong)in a 2-D array, and should print out the proper answer from the array after getting the user‟s answer"
Code:
#include <stdio.h>
#include <string.h>
int main(void)
{
int i,j;
char choice[BUFSIZ], name[BUFSIZ];
char T[23] = {"Your answer is wrong."};
char F[24] = {"Your answer is correct!"};
printf("What is your name ?");
gets(name);
puts("Hello");
puts(name);
printf("Is Europe larger than Asia? (T) or (F):");
//scanf("%s", choice);
gets(choice);
gets(T);
gets(F);
for(i=0; i<24; i++)
{
if( strcmp( choice , F ) == 0 )
puts(T[i]);
// printf("%s", F);
else if (strcmp(choice , F) < 0)
putchar("\n");
puts(F[i]);
}
// printf("%s", T);
getchar();
return 0;
}
-
So, what is the problem that you are facing?
-
Ok if I use scanf for choice instead of gets and I use printf instead of puts I get no errors, but nothing outputs. If U use gets and puts it says T[i] and F[i] can't be used:( I looked at the examples from my lecture but it works just fine! I'm not sure why either method doesn't work.
-
Why are you trying to input into T or F?
You also don't have an array of strings. You have a string called T, and a string called F. Your requirements that you posted indicated you need an array of two strings, so you should probably make something like
Code:
char responses[2][24];
-
Correct the usage of puts.
Signature:
int puts(const char *s);
You are trying to use T[i] so pass the address of this.
Also it seems that you are going wrong logically when comparing inputs.
Read the signature and usage of functions before you use in code.
-
Here is what I have now. It outputs my name and lets me input T or F but it doesn't display the result.
Code:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main(void)
{
int i,j;
char choice[BUFSIZ], name[BUFSIZ];
char output [2][24] = {"Your answer is wrong.",
"Your answer is correct!"};
printf("What is your name ?");
gets(name);
puts("Hello");
puts(name);
printf("Is Europe larger than Asia? (T) or (F):");
scanf("%s", &choice);
//gets(choice);
putchar('\n');
for(i=0; i<24; i++)
if (toupper (choice) =="T")
puts (output[i]);
//if( strcmp( choice , F ) == 0 )
// printf("%s", F);
// else
//else if (strcmp(choice , F) < 0)
// putchar("\n");
//puts(F[i]);
// printf("%s", T);
return 0;
}
-
Code:
scanf("%s", &choice);
%s is for a string, you only want one character, so why are you reading more?
&choice in this case would be fine as just choice.
Code:
if (toupper (choice) =="T")
You can't compare string literals (or arrays) with ==. "" indicates multiple characters, ' ' indicates a single character.
Quzah.
-
Code:
#include <stdio.h>
#include <string.h>
int main(void)
{
char choice, name[BUFSIZ];
char output [2][24] = {"Your answer is wrong.",
"Your answer is correct!"};
printf("What is your name ?");
fgets(name,BUFSIZ,stdin);
printf("Hello %s\n\n", name);
printf("Is Europe larger than Asia? (T) or (F):");
scanf("%c", &choice);
if (toupper( choice ) == 'T')
printf("%s \n",output[0]);
else
printf("%s \n",output[1]);
return 0;
}