Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 如何使if语句仅在输入字符时运行_C_Cs50 - Fatal编程技术网

C 如何使if语句仅在输入字符时运行

C 如何使if语句仅在输入字符时运行,c,cs50,C,Cs50,如何使用if语句仅在输入字符时执行括号内的操作。但是当输入整数/无时,将它们发送到else语句 #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

如何使用if语句仅在输入字符时执行括号内的操作。但是当输入整数/无时,将它们发送到else语句

#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");
}

}
假设:

char answer1;
然后

尝试读取字符串并将其放入answer1中。这将始终覆盖answer1之后的内存,因为字符串将以null结尾,即使您只输入一个字母。因此,您的程序变得不可靠。请参阅P.P.的评论,使用:

scanf(" %c", &answer1);

首先检查用户输入的内容是否有效

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");
}

answer1是如何声明的?我在上面声明了它,我没有包括我的完整代码,你想让我添加它吗?我只是想看看你是如何声明answer1的。根据这一点,您在某个地方使用了错误的答案。答案1的类型对于了解测试答案1=='b'是否为有效代码非常重要。char answer1;扫描%c,&回答1;应该工作注意%c和%s之前的空格已替换为%c。当我进行if应答时!='',我得到一个错误:试图编译时字符常量为空。@Karsenhettinger try ifanswer!='\0'仍然不起作用
scanf(" %c", &answer1);
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");
}