0

I am a beginner in C++ and I was trying to make a decision function which I could use in future programs.

The idea is that the program would ask a yes/no question, the user would type in yes or no and they would receive an output according to their answer. But when I try running the code, the program shows the output for the "yes" case no matter what the answer. Here's the code:

#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <conio.h>

class dec
{
    public:
        int flag;
        char yn[3];

        void decision()
        {
            std::cout<<"Enter yes or no:\n";
            std::cin>>yn;
            flag=0;
            while(flag==0)
            {
                if(strcpy(yn,"yes"))
                    flag=1;
                else if(strcpy(yn,"no"))
                    flag=-1;
                else
                {
                    std::cout<<"Invalid input.\n";
                    flag=0;
                }
            }
        }
        void reset()
        {
            flag=0;
        }
};

main()
{
    dec d;
    std::cout<<"Test?\n";
    d.decision();
    if(d.flag==1)
        std::cout<<"Correct.";
    else
        std::cout<<"Wrong.";
}

No matter what I type, the output is always "Correct". Please help.

PS: I'm using flag so that I can reuse the function again when needed.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • 7
    Use `std::string` instead of `char[]`, then you can use `yn == "yes"` for example. In any case, [`strcmp`](https://en.cppreference.com/w/cpp/string/byte/strcmp) is what you use for string comparison, not [`strcpy`](https://en.cppreference.com/w/cpp/string/byte/strcpy) which does a copy. – Cory Kramer Oct 08 '19 at 16:07
  • 2
    @CoryKramer: That's pretty much the answer - can you make it a real answer so we can mark this question answered? – MSalters Oct 08 '19 at 16:09
  • 2
    `std::cin>>yn;` -- Your code now has one of the most exploited vulnerabilities -- buffer overrun. What happens if I type in 100 characters instead of 2? Classes such as `std::string` prevent these things from happening. – PaulMcKenzie Oct 08 '19 at 16:10
  • 3
    Unrelated: Thumbs up for not `using namespace std;`! – Ted Lyngmo Oct 08 '19 at 16:42
  • You should only need to compare once, if you convert to all lower case or all upper case before the compare. Search the internet for "c++ transform string toupper". – Thomas Matthews Oct 08 '19 at 17:49
  • @CoryKramer where do I use ```std::string``` in the code? ```std::cin>>std::string yn```? – Arun Ravindranath Oct 08 '19 at 18:00

1 Answers1

4

strcpy() copies a string into a character array. Instead you need to use strcmp() to compare two arrays. You do it like this:

if (strcmp(yn, "yes") == 0)

Also beware that you need to declare char yn[4] to allow for a terminating NULL character.

Alternatively, you can use std::string instead of an array and then compare strings with ==:

if (yn == "yes")
Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
  • Don't know why the strcmp() suggestion did not work. Stil shows the answer as "Correct." The ```std::string``` suggestion worked though. Anyway, thanks for the help. Much appreciated. – Arun Ravindranath Oct 08 '19 at 18:06
  • @ArunRavindranath Make sure you declare `char yn[4];`. Notice that you have declare an array with **four** elements, not three. Also use the syntax I show above when using `strcmp()`. For details, you should read the documentation for this function. If your code still isn't quite right, check out [this article](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) for some tips on debugging your code to see what's going on. – Code-Apprentice Oct 08 '19 at 19:35
  • @ArunRavindranath On a side note: what happens when the user types in "abcd" or something else other than "yes" or "no"? – Code-Apprentice Oct 08 '19 at 19:37
  • if user types in something other than "yes" or "no" ```flag=0``` and the while loop repeats again until the answer is yes or no. BTW, thanks for the advice. I didn't use "==0" in strcmp() before, which is why it showed "Correct." Thanks for the help again. – Arun Ravindranath Oct 09 '19 at 08:45
  • @ArunRavindranath That is only what you think the code does. You should actually try it. Hint: how will the answer change after the first time the user enters one? – Code-Apprentice Oct 09 '19 at 15:38