0

How do i use an if statement to only do what's inside the brackets when a character is entered. But when an integer/nothing is entered send them to the else statement.

#include <stdio.h>
#include <ctype.h>
#include <cs50.h>
#include <stdbool.h>

int main(void)
{
// use boolean for printing if user fails(true) or passes(false) test
int score = 0; //score starts at 0, and 10 points are added if correct.
char answer1;
char answer2;
char answer3;
char answer4;
char answer5;
char answer6;
char answer7;
char answer8;
char answer9;
char answer10;

system("clear");
printf("\n1. What is the capital of Russia?\n");
printf("a)Washington DC \nb)Moscow\nc)Copenhagen\nd)Stockholm\n");
scanf("%s", &answer1);
system("clear");


   if (answer1 == 'b') {
    printf("Correct!\n");
    (score += 10); //if answer is correct, score adds 10
    printf("Your score is: %i\n",score);
    }

   if (answer1 != 'b') {
    printf("Wrong!\n");
    printf("Your score is: %i\n",score);
    }

   else {
    printf("\nPlease enter in a valid response\n");
   }


printf("\n2. Who was the first president of the United States?\n");
printf("a)Washington\nb)Lincoln\nc)Obama\nd)Adams\n");
scanf("%s", &answer2 &&);
system("clear");
    if (answer2 == 'a') {
    printf("Correct!\n");
    (score += 10); //if answer is correct, score adds 10
    printf("Your score is: %i\n",score);
    }

    if (answer2 != 'a') {
    printf("Wrong!\n");
    printf("Your score is: %i\n",score);
    }



printf("\n3. How many states are there in the United States?\n");
printf("a)25\nb)87\nc)42\nd)50\n");
scanf("%s", &answer3);
system("clear");
    if (answer3 == 'd') {
    printf("Correct!\n");
    (score += 10); //if answer is correct, score adds 10
    printf("Your score is: %i\n",score);
    }

    if (answer3 != 'd') {

    printf("Wrong!\n");

    printf("Your score is: %i\n",score);
    }



printf("\n4. What is the square root of 144?\n");
printf("a)12\nb)14\nc)7\nd)24\n");
scanf("%s", &answer4);
system("clear");
    if (answer4 == 'a') {
    printf("Correct!\n");
    (score += 10); //if answer is correct, score adds 10
    printf("Your score is: %i\n",score);
    }

    if (answer4 != 'a') {

    printf("Wrong!\n");

    printf("Your score is: %i\n",score);
    }



printf("\n5. What is the most basic unit of human life?\n");
printf("a)Skin\nb)Mitochondra\nc)Cell\nd)ATP\n");
scanf("%s", &answer5);
system("clear");
    if (answer5 == 'c') {
    printf("Correct!\n");
    (score += 10); //if answer is correct, score adds 10
    printf("Your score is: %i\n",score);
    }

    if (answer5 != 'c') {

    printf("Wrong!\n");

    printf("Your score is: %i\n",score);
    }



printf("\n6. What programming language is this written in?\n");
printf("a)Objective-C\nb)C++\nc)C#\nd)C\n");
scanf("%s", &answer6);
system("clear");
    if (answer6 == 'd') {
    printf("Correct!\n");
    (score += 10); //if answer is correct, score adds 10
    printf("Your score is: %i\n",score);
    }

    if (answer6 != 'd') {

    printf("Wrong!\n");

    printf("Your score is: %i\n",score);
    }



printf("\n7. What is apple's newest programming language?\n");
printf("a)Swift\nb)Java\nc)Python\nd)Objective-C\n");
scanf("%s", &answer7);
system("clear");
    if (answer7 == 'a') {
    printf("Correct!\n");
    (score += 10); //if answer is correct, score adds 10
    printf("Your score is: %i\n",score);
    }

    if (answer7 != 'a') {

    printf("Wrong!\n");

    printf("Your score is: %i\n",score);
    }



printf("\n8. What is the most portable way to store data?\n");
printf("a)Ram\nb)Flash Drive\nc)Solid-state drive\nd)Hard Drive\n");
scanf("%s", &answer8);
system("clear");
    if (answer8 == 'b') {
    printf("Correct!\n");
    (score += 10); //if answer is correct, score adds 10
    printf("Your score is: %i\n",score);
    }

    if (answer8 != 'b') {

    printf("Wrong!\n");

    printf("Your score is: %i\n",score);
    }



printf("\n9. What is the name of IBM's AI?\n");
printf("a)Arnold\nb)Jonathan\nc)Watson\nd)Pablo\n");
scanf("%s", &answer9);
system("clear");
    if (answer9 == 'c') {
    printf("Correct!\n");
    (score += 10); //if answer is correct, score adds 10
    printf("Your score is: %i\n",score);
    }

    if (answer9 != 'c') {

    printf("Wrong!\n");

    printf("Your score is: %i\n",score);
    }



printf("\n10. What is the universal sign for peace?\n");
printf("a)Index finger and thumb raised\nb)Index finger raised\nc)Middle finger raised\nd)Index and middle finger raised\n");
scanf("%s", &answer10);
system("clear");
    if (answer10 == 'd') {
    printf("Correct!\n");
    (score += 10); //if answer is correct, score adds 10
    printf("Your score is: %i\n",score);
    }

    if (answer10 != 'd') {

    printf("Wrong!\n");

    printf("Your score is: %i\n",score);
    }

if (score > 70) {
printf("\nCongratulations! You have passed!\n");
}

else {
printf("\nUnfortunately, you have failed. Please try again\n");
}

}

This is my code, I tried putting an else statement below the if statements saying to enter in a valid response, but it doesn't work. Please help.

Edit: I fixed it

q1:
printf("\n1. What is the capital of Russia?\n");
printf("a)Washington DC \nb)Moscow\nc)Copenhagen\nd)Stockholm\n");
scanf(" %c", &answer1);
system("clear");


if (answer1 == 'a' || answer1 == 'b' || answer1 == 'c' || answer1 == 'd') {

    if (answer1 == 'b') {
    printf("Correct!\n");
    (score += 10); //if answer is correct, score adds 10
    printf("Your score is: %i\n",score);
    goto q2;
    }

    if (answer1 != 'b') {
    printf("Wrong!\n");
    printf("Your score is: %i\n",score);
    }
 }
else {
printf("Please enter a letter a-b!");
goto q1;
}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278

2 Answers2

1

First check to see if the user entered something valid...

if (answer1 != '') {

  if (answer1 == 'b') {
     printf("Correct!\n");
     (score += 10); //if answer is correct, score adds 10
     printf("Your score is: %i\n",score);
   }
   if (answer1 != 'b') {
     printf("Wrong!\n");
     printf("Your score is: %i\n",score);
   }
}
else {
  printf("please enter valid answer!\n");
}
tomsmithweb
  • 371
  • 2
  • 6
  • 20
0

Assuming:

char answer1;

then

scanf("%s", &answer1);

tries to read a string and places that in answer1. This will always overwrite memory following answer1 because a string will be null-terminated, even if you would enter only one letter. As a result, your program has become unreliable. Instead (see comment of P.P.), use:

scanf(" %c", &answer1);
Paul Ogilvie
  • 25,048
  • 4
  • 23
  • 41