-5

I am just trying to learn how to use c++ and one thing that I am trying to make is a yes or no choice "choose y to continue choose n to exit" kinda thing I know other ways of doing this other then this method but I am trying to learn the concept of creating functions outside of main() better this is the code I am trying to use

int yesorno(char yn[]);

int yesorno(char yn[])
{
   int a=0;
   do 
   {
       if (!strcmp( yn , "y" || yn , "Y"))
        {    
           a=1;
        }
       else if (!strcmp( yn , "n" || yn ,"N"))
        {
           a=2;
        }
       else 
        {
           cout<<"Please choose y or n \n";
           cin>> yn;
           a=3;
        }

   }while (a==0 || a>=3);
   yn = "0";
   return a;
}

the error I get is "no matching function for call to 'strcmp' " in the if and else if statement yet using strcmp in main() works just fine and I have #include just wondering what I am doing wrong with this. Any help is very very much appreciated.

  • 1
    You have the boolean condition inside the strcmp.. – Marco A. Sep 04 '14 at 06:49
  • 6
    And if you learn c++, learn how not to use `strcmp` – quantdev Sep 04 '14 at 06:49
  • 1
    1. Learning anything then read the manual page. 2. C++ using std::string. – Ed Heal Sep 04 '14 at 06:52
  • When I was starting out with programming I also had this misleading intuition that when you have a condition e.g. myvariable == something you could put a logical OR there, like myvariable == a || b. But that's obviously wrong. Look at it like it was math - this would not be a valid statement. myvariable == a || myvariable == b would be. So, think of it that way. The same applies to function calls - so in your case, you have to OR the function calls. Exactly what Marco A. says. – PawelP Sep 04 '14 at 07:02
  • okay thank you :) I am still very much in the infancy of learning c++ and programing anything beyond the basics of easy easy loops and such theres still much to learn – jesse ftichar Sep 04 '14 at 07:17

2 Answers2

2

You can't insert boolean conditions into the function this way:

if (!strcmp( yn , "y" || yn , "Y"))

change the conditions to first evaluate the boolean result of the functions and then perform your logical comparison

if (!strcmp(yn , "y") || !strcmp(yn , "Y"))

Also notice that you're missing #include <cstring> to use strcmp

Marco A.
  • 43,032
  • 26
  • 132
  • 246
0

You need to #include <cstring> near the top of your code, as that's the Standard library header file with a declaration to tell the compiler about strcmp. A good site to check these things is cppreference: see here and note it mentions <cstring> near the top.

Also, you must do each test separately:

if (!strcmp(yn, "y") || !strcmp(yn, "Y"))
   ...

Thsi won't compile either:

yn = "0";

You could use:

strcpy(yn, "0");

But overall, your code could be cleaned up considerably by using C++'s std::string type:

int yesorno(std::string& yn)
{
   int a=0;
   do 
   {
       if (yn == "y" || yn == "Y")
           a=1;
       else if (yn == "n" || yn == "N")
           a=2;
       else 
       {
           cout<<"Please choose y or n \n";
           cin>> yn;
           a=3;
       }

   } while (a == 0 || a >= 3);
   yn = "0";
   return a;
}
Tony Delroy
  • 102,968
  • 15
  • 177
  • 252