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.