-3

Guys i made this program. It is working as expected but at the end of my program it is not showing my num variable input. please help. Here's code:

#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);
}
Adnan Khan
  • 43
  • 7

2 Answers2

0

you need to print it out at the end by doing something like this:

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

make sure you put the & sign and it should work... Make sure you are using the correct compiler and have a space in between #include <stdio.h> so that your code does not appear messy.

The real problem is where you put if(f==0) if you are looking for the user input of "f" then make it if(f == "f") so that you can take in "f" instead of "0". Use string f; or char f instead of int. Make sure the scanf should be formatted like this if you are using char: scanf("%s", &f);

Have a nice day!

Skyy Civil
  • 63
  • 8
0

when you input your character, there are two things that happen:

  1. scanf will try to read a number that you input for the first argument
  2. when the number is not in the right format, it will parse the second character to your second scanf. So when you type F, then hit enter, the first F will be parsed as a number to the first argument (f = 0), and 'new line' character will be parsed to the second scanf (num = 0). you can check the second character's value by using
    printf("Value of num: %d\n", (int)num); 

It will print out that the value of num is 10, which is the value of 'new line' character in ASCII table. The correct way to do it (as you intend to do, I guess) is:

#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);
}
ngan ngo
  • 61
  • 3