简体   繁体   中英

Simple C program returns no output

Ive written a simple C program which takes user input and stores it in the dataofUser variable, then it allows the user to pick if they want to display the data they entered by saying yes or no, this is done through fgets and a if statement which checks if the userChoice variable has yes or no stored inside it depending on if its yes or no it will display the data or display "no output!", when i run the program i get no output of the data i entered below you can see what shows on the console:

Please enter your input: this is a random input
Your data has been entered please enter Yes or No to display it: yes

Process returned 0 (0x0)    execution time : 4.829 s
Press ENTER to continue.

This is the build messages log and the various error messages

||=== Build: Debug in randopoint2 (compiler: GNU GCC Compiler) ===|
/home/Documents/randopoint2/randopoint2/main.c||In function ‘main’:|
/home/Documents/randopoint2/randopoint2/main.c|17|warning: comparison between pointer and integer|
/home/Documents/randopoint2/randopoint2/main.c|17|warning: comparison with string literal results in unspecified behavior [-Waddress]|
/home/Documents/randopoint2/randopoint2/main.c|21|warning: comparison between pointer and integer|
/home/Documents/randopoint2/randopoint2/main.c|21|warning: comparison with string literal results in unspecified behavior [-Waddress]|
||=== Build finished: 0 error(s), 4 warning(s) (0 minute(s), 0 second(s)) ===|
||=== Run: Debug in randopoint2 (compiler: GNU GCC Compiler) ===|

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

int main()
{
    char dataOfUser[50];
   char userChoice[50];

  printf("Please enter your input: ");
  fgets(dataOfUser, 50, stdin);

  printf("Your data has been entered please enter Yes or No to dipslay it: ");
  fgets(userChoice, 50, stdin);


  if(userChoice[50] == "yes")
  {
  printf("Your notes are: %s", dataOfUser);
  }
  else if(userChoice[50] == "no")
  {
  printf("no output!");
  }

    return 0;
}

Problem:

if(userChoice[50] == "yes")

That is not how strings are compared in C.

Moreover, accessing userChoice[50] is undefined behaviour because you are trying to access the array out of its bounds.

The same goes for else if(userChoice[50] == "no")

Also note that when you press enter to end your input, the newline \\n is stored in userChoice .

Solution:

  1. Add #include <string.h> to get the strcmp function.

  2. Change if(userChoice[50] == "yes") to if (strcmp(userChoice, "yes\\n") == 0)

  3. Change else if(userChoice[50] == "no") to else if (strcmp(userChoice, "no\\n") == 0)

Aside:

printf("Your data has been entered please enter Yes or No to dipslay it: ");

You are asking the user to enter either "Yes" or "No" but compare it with "yes" and "no" in your code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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