0

I'm new to this, and I'm assuming there is an easy solution to my issue. My first formula works exactly how I'd like it to. If the user input matches dogage99, then it prints "Correct". I want to do something similar, but using words instead of numbers. I've switched double for char, and adjusted the formula accordingly.

The problem is, the second formula doesn't work as I expected. When the user input matches dogname1, it doesn't print "Correct", it just continuously asks to "enter dog name".

What can I do to fix my issue?

int main() {

double guess99;
double dogage99 = 3;

while (guess99 != dogage99) {
    printf ("enter dog age:");
    scanf ("%lf", &guess99);
}
printf ("Correct\n");



char guess1;
char dogname1= "spot";

while (guess1 != dogname1) {
    printf ("enter dog name:");
    scanf ("%s", &dogname1);
}
printf ("Correct\n");
Ken White
  • 123,280
  • 14
  • 225
  • 444
hatch
  • 3
  • 1
  • 1
    You can, but you really want to use `char[]` or `char*` here with `strcpy`. But `guess99` and `guess1` are uninitialized in what you've shown. And be careful doing exact comparisons on floating points. And is a floating point type for a dog's age really necessary? Is someone expected to enter something like `4.73` for the age of their dog? – Fred Larson Jan 14 '22 at 22:28
  • 2
    You should turn on your compiler warnings. `"spot"` is a `char[5]`, not a `char`, so that whole bit of code is going to do the wrong thing. – 0x5453 Jan 14 '22 at 22:28
  • Also, trying to test [in]equality with `double` variables, as you do with `while (guess99 != dogage99)` has a really good chance of not doing what you want - see [**Is floating point math broken?**](https://stackoverflow.com/questions/588004/is-floating-point-math-broken) – Andrew Henle Jan 14 '22 at 23:10

2 Answers2

1

First of all the line char dogname1= "spot"; should be corrected to char *dogname1= "spot"; This way the char array dogname1 will be correctly initialized as a character array and will contain the null-terminator "\0" at the end of the array.

You must also ensure, that guess has enough memory secured, so you must either create a array of sufficiently enough bytes (for e.g. 256), or dynamically allocate memory. In this example I would do the first changing char guess1; to char guess1[256];

Knowing that guess1 has sufficiently enough memory and a null-terminator we can next rewrite the while loop to

while(strcmp(guess1, dogname1)) { ... }

The strcmp() standard library function returns 0 if and only if both character arrays match lexicographical and that is also the time we want to go out of the loop. It is also important to know, that you must ensure both arrays have null-terminators when using this exact function. If you cannot ensure it, then use strncmp().

For reference about all the different compare functions and their implications: https://www.ibm.com/docs/en/aix/7.1?topic=s-strcmp-strncmp-strcasecmp-strcasecmp-l-strncasecmp-strncasecmp-l-strcoll-strcoll-l-subroutine

rdabek
  • 70
  • 8
-2

I am sure the compiler is complaining big time at you about

char dogname = "spot";

in c the type char represent a single character not a string. Strings in C are a sequnce of characters followed by a char set to \0. The compiler will set that up for you if you do

char *dogname="spot";

It will allocate 5 bytes , load s,p,o,t,\0 into those bytes and set the dogname variable to point at the first character.

If you want to compare string you have to use the c library function called strcmp - https://man7.org/linux/man-pages/man3/strcmp.3.html.

Alos you need a char array to receive the input. We just say we want 50 characters. Must also tell scanf to not allow more than 50 charaters. Note that I asked for 51 character array to allow for the trailing 0 that must always be present.

so your loop becomes

char  guess1[51];
char *dogname1= "spot";

while (guess1 != dogname1) {
    printf ("enter dog name:");
    scanf ("%50s", guess1);
}
printf ("Correct\n");

note you must #include string.h

check this out https://www.tutorialspoint.com/cprogramming/c_strings.htm

pm100
  • 48,078
  • 23
  • 82
  • 145
  • 2
    `scanf ("%s", &dogname1);` should be `scanf ("%s", guess1);` and `guess1` needs to be a char array as well – codyne Jan 14 '22 at 22:35
  • @codyne - was hoping OP would work the rest out for themselves – pm100 Jan 14 '22 at 22:45
  • This piece of code just won't even compile. If you would like the OP to finish the code with the help of your tips and hints, you should show explicitly that this is ```pseudo-code``` instead of effective ```C code```. – Wu Xiliang Jan 15 '22 at 00:23
  • @WuXiliang it was OK before I corrected for codynes comment. I repasted OPs code and forgot to correct the ="spot" again. Fixed – pm100 Jan 15 '22 at 00:47