0

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;
}
Ardent Coder
  • 3,777
  • 9
  • 27
  • 53
  • 3
    Hint, `userChoice[50] != "yes"` **and** `userChoice[50] != "no"`. You need [`strcmp`](http://www.cplusplus.com/reference/cstring/strcmp/). – Elliott Frisch Apr 02 '20 at 17:14
  • Please take a step back, and refresh your text-books, class-notes or tutorials on how to compare strings. And use arrays in general. I also suggest you read more about [`fgets`](https://en.cppreference.com/w/c/io/fgets) and what it adds to the buffer. – Some programmer dude Apr 02 '20 at 17:14
  • @ElliottFrisch wow that worked, why did it not work with ==? and why did you add the exclamation mark symbol –  Apr 02 '20 at 17:17
  • 1
    `userChoice[50]` is invalid in the first place, past the end of the array. – aschepler Apr 02 '20 at 17:17
  • @Someprogrammerdude im a beginner at C what books or tutorials or books do you reccomend –  Apr 02 '20 at 17:18
  • Note the newline that was part of `"yes\n"` above (that comment is now removed). Please see [Removing trailing newline character from fgets() input](https://stackoverflow.com/questions/2693776/removing-trailing-newline-character-from-fgets-input/28462221#28462221) – Weather Vane Apr 02 '20 at 17:20
  • @aschepler What do you mean? i thought it would only go past the array if it goes past the 50 character word limit –  Apr 02 '20 at 17:21
  • The 50 characters are stored in elements [0]..[49]. Element [50] would be the 51st element of the array which does not exist. – Gerhardh Apr 02 '20 at 17:31
  • @Gerhardh oh I thought the 50 meant how many characters you wanted the variable to be in size so what does it actually mean?- –  Apr 02 '20 at 17:38
  • 1
    You are partially right. When defining the variable, the number in brackets defines the size of the array. But when you access the elements, it is the index of a single element. An `array[50]` holds elements `array[0]..array[49]` – Gerhardh Apr 02 '20 at 17:39
  • @Gerhardh oh so when I accessed the array in the if statement through if (userChoice[50] == "yes") that was incorrect I was actually supposed to do if(userChoice[0] == "yes")? –  Apr 02 '20 at 18:19
  • No. String comparison does not work like this. See the provided answer. – Gerhardh Apr 02 '20 at 18:24
  • @Gerhardh do you know any good C coding tutorials or books I could learn from I'm a beginner in C and dont get this. –  Apr 02 '20 at 18:25
  • @Qasim Check the link in my answer – Ardent Coder Apr 02 '20 at 18:35
  • @ArdentCoder okay thanks –  Apr 02 '20 at 19:05
  • This should be handled in any C beginner text book. Just look for the chapters adressing strings and arrays. – Gerhardh Apr 03 '20 at 06:34
  • @Gerhardh im following a youtube tutorial not a book what book do you reccomend? –  Apr 03 '20 at 09:56
  • I don't have a specific recommendation. You might take a look at the list here: https://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list – Gerhardh Apr 03 '20 at 10:03
  • Maybe there are also youtube or other online tutorials dealing with string and arrays in C – Gerhardh Apr 03 '20 at 10:04

1 Answers1

1

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.

Ardent Coder
  • 3,777
  • 9
  • 27
  • 53
  • what do you mean by the accessing of the array being out of bounds? –  Apr 02 '20 at 18:28
  • @Qasim This is your array: `char dataOfUser[50];`. How many characters can it store? 50. How do you access its elements? `dataOfUser[0]` (1st element), `dataOfUser[1]` (2nd element), and so on. So how do you access the 50th element? `dataOfUser[49]` (50th element). Now think about `dataOfUser[50]`, you are trying to access an element outside of your array! – Ardent Coder Apr 02 '20 at 18:31