繁体   English   中英

我的输入在程序结束时没有显示在 c++ 中

[英]My input is not showing in c++ at end of program

伙计们,我制作了这个程序。 它按预期工作,但在我的程序结束时它没有显示我的 num 变量输入。 请帮忙。 这是代码:

#include<stdio.h>

int main(){
    int f;
    int num;
    printf("WELCOME TO THE GAME\n\n");
    
    printf("enter F for starting the game\n");
    scanf("%d\n\n",&f);
    
    if(f==0){
        printf("Ques 01: you are exhausted because your week was endless and less than great.\n");
        printf("How are you going to spend your weekend?");
    }
    printf("\n\n");
    
    printf("Option 01: I'll call my friend to ask about their plans. I heard that a new restaurant opened/ a nice comedy is playing in the cinemas / there are big discounts at the paintball club. ");
    printf("\n\n");
    printf("option 02: I'll switch on the Don't disturb mode on my phone and stay at home.\n");
    printf("I'll watch a new episode of my favorite TV show, do a puzzle, and take a long bath with a book.");
    printf("\n\n");

    scanf("%d", &num);
}

您需要通过执行以下操作在最后打印出来:

scanf("%s", &num);
printf("num is, ", num);

确保你放了&符号,它应该可以工作......确保你使用的是正确的编译器,并且在#include <stdio.h>之间有一个空格,这样你的代码就不会显得混乱。

真正的问题是如果您正在寻找“f”的用户输入,则将if(f==0)放在哪里,然后将其设置为if(f == "f")以便您可以使用“f”而不是“ 0”。 使用string f; char f而不是int 如果您使用char ,请确保scanf的格式应如下所示: scanf("%s", &f);

祝你今天过得愉快!

当你输入你的角色时,会发生两件事:

  1. scanf 将尝试读取您为第一个参数输入的数字
  2. 当数字格式不正确时,它会将第二个字符解析为您的第二个 scanf。 因此,当您键入 F,然后按 Enter 键时,第一个 F 将被解析为第一个参数 (f = 0) 的数字,而“换行”字符将被解析为第二个 scanf (num = 0)。 您可以使用检查第二个字符的值
    printf("Value of num: %d\n", (int)num); 

它将打印出num的值为10,这是ASCII表中'new line'字符的值。 正确的方法(我猜你打算这样做)是:

#include<stdio.h>

int main(){
    char f;
    int num;
    printf("WELCOME TO THE GAME\n\n");
    
    printf("enter F for starting the game\n");
    scanf("%c",&f);
    
    if(f=='F'){
        printf("Ques 01: you are exhausted because your week was endless and less than great.\n");
        printf("How are you going to spend your weekend?");
    }
    printf("\n\n");
    
    printf("Option 01: I'll call my friend to ask about their plans. I heard that a new restaurant opened/ a nice comedy is playing in the cinemas / there are big discounts at the paintball club. ");
    printf("\n\n");
    printf("option 02: I'll switch on the Don't disturb mode on my phone and stay at home.\n");
    printf("I'll watch a new episode of my favorite TV show, do a puzzle, and take a long bath with a book.");
    printf("\n\n");
    printf("Enter a number....:");
    scanf("%d", &num);
    printf("You just enter number %d\n", num);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM