0

i'm trying to get 2 words to be compared whether they're the same or not, but scanf() on line 14 isn't working and the code goes straight to printing "False"

#include <string.h>
#include <stdio.h>

int main()
{
int w1, w2;                     //word 1, word 2

{

printf("What is the first word?");
scanf("%%", &w1);

printf("What is the second word?");
scanf("%%", &w2);                        <--------line 14

{
    if
    (w1 == w2) {
    printf("True");
    }
    else {
        printf("False");
    }
}

}
}
Marek R
  • 32,568
  • 6
  • 55
  • 140
  • 3
    What do you think the `%%` format does? What are you trying to do with it? – Adrian Mole Oct 26 '21 at 12:05
  • 1
    You can't store a *word* in an `int`, and `%%` is not a valid format specification. Get [a good book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) and don't learn C when you should learn C++. – molbdnilo Oct 26 '21 at 12:06
  • i'm trying input a word. should it be changed to %c? – JAKE Cho Oct 26 '21 at 12:07
  • You are not scanning anything – if you had checked the return value of `scanf` you would have noted. That one should be equal to the number of scanned values (one in your case), if not, something went wrong. – Aconcagua Oct 26 '21 at 12:07
  • As mentioned, `int` cannot store words – if you want words, you need to provide a character buffer for `scanf` and read with `%s` format specifier. Safer, though, is reading with C++ streams: `std::string s1, s2; if(std::cin >> s1 >> s2) { /* both words successfully read */ } else { /* some error occurred – handle appropriately */ }`. – Aconcagua Oct 26 '21 at 12:09
  • Side note: you have really strange formatting, usually one doesn't break after `if` – and why these surplus braces? – Aconcagua Oct 26 '21 at 12:11
  • 1
    You may want to get a [C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). Trying to learn C++ from random website advice is often counterproductive. – n. m. could be an AI Oct 26 '21 at 12:19

0 Answers0