C - Can't get my second question in my game to work

0 Robert Summers · November 28, 2014

#include <stdlib.h>
#include <stdio.h>

int main()
{
    char calc = 0;
    char pi1 = 0;
    char pi2 = 0;
    printf("Let's Learn Mathematics!\nAnswer the following questions correctly...\nWhat is 2 + 2?\n");
    scanf("%s", &calc);
    if(calc == '4'){
        printf("That is correct.\n");
    }else{
        printf("Incorrect answer!\n");
        return 0;
    }
    printf("What is pi equal when rounded to 5 decimal places?\n");
    scanf("%s%s", &pi1, &pi2);
    if(pi1 == '3.' && pi2 == '14159'){
        printf("Correct.\n");
    }else{
        printf("Pi is not %s %s\n!!", &pi1, &pi2);
    }
    return 0;
}

Thanks for the help in return

Post a Reply

Replies

Oldest  Newest  Rating
0 Franz Schmidt · November 28, 2014
I don't understand why you want to use two strings to input pi, but even then you would need char ARRAYS

I made a solution with only one string where you save the pi input

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

int main()
{
char calc = 0;
char pi[10];
printf("Let's Learn Mathematics!\nAnswer the following questions correctly...\nWhat is 2 + 2?\n");
scanf("%c", &calc); //if you only enter one character always use %c instead of %s
if (calc == '4'){ //if you only check for only ONE character this is alowed, but don't compare strings like this
printf("That is correct.\n");
}
else{
printf("Incorrect answer!\n");
return 0;
}
printf("What is pi equal when rounded to 5 decimal places?\n");
scanf("%10s", &pi); //this %10s can only get 10 chars, cause the char array has only space for 10 chars.
if (!strcmp(pi, "3.14159")){ //to compare strings you need the strcmp() function; this function returns 0 if EVERYTHING is equal (don't forget the !);
printf("Correct.\n");
}
else{
printf("Pi is not %s!!", &pi);
}
return 0;
}
0 c student · December 5, 2014
your program seems to fail to compile:

/images/forum/upload/2014-12-05/d843383cbc44166353f8c666412cb061.jpg
on line 19, your declared variable "pi1" is invalid as it was declared as a char type, which only takes one character, but you have two "3."  with pi2, also declared as a char, i believe the error with this is that it has exceeded its maximum size of 255.
what you can try is to declare one pi variable as a double type which allows for floating point numbers such as 3.14159. having declared it as a double type, you can use scanf("%lf", &pi); to scan it in properly and then compare it with the input as you would normally.

scanf is not really a recommended function to use in this situation where you only require a single character input.  instead, i would use the getchar() function and declare it as char c = getchar() and then compare in the if statement if (c == '4').

hope this helped!

p.s. returning value of 0 means a successful termination.  for an unsuccessful termination, the value would be 1.
0 Franz Schmidt · December 5, 2014
Please keep in mind that there is a bug in the code tags, that makes the i in square brackets [] disappears because of the italic BB-Code
0 c student · December 7, 2014
here is a much simpler solution without the need for arrays and string comparisons.  also fixed up some bad habits you should avoid:
#include <stdlib.h>
#include <stdio.h>

int main () {
    
double pi;   // double type for floating point numbers

// try to avoid lengthy lines!
printf("Let's Learn Mathematics!\n"
"Answer the following questions correctly...\n"
"What is 2 + 2?\n");

// using getchar() to scan character and test
if (getchar() == '4') {
printf("That is correct.\n");
} else {
printf("Incorrect answer!\n");
exit (1);   // 1 for failed termination! try to avoid multiple returns!
}

printf("What is pi equal when rounded to 5 decimal places?\n");
scanf("%lf", &pi); // %lf for double type
if (pi == 3.14159){ // comparing pi to 3.14159
printf("Correct.\n");
} else {
printf("Pi is not %lf!!", pi);
exit (1);
}

return 0;
}
  • 1

C

107,229 followers
About

One of the most popular languages of all time.

Links
Moderators
Bucky Roberts Administrator