Hi CPP Guys, I am using switch statement to perform some specific search in my simple program, but when I enter case in more than two character e.g. *case: 'November'* the error message says only two or less character constants are allowed in case.
My question is that can i increase limit of character constants in the case? If yes how?
void search()
{
clrscr();
char sr,rep;
cin>>sr;
switch(sr)
{
case'John': /*Says, this should be only of 2 characters long i.e. 'jo'*/
case'JOHN':
case'john':
cout<<"Your Search returned: Johny Depp"<<endl;
cout<<"Want to see full details? (Y/N)";
cin>>rep; /*It also forces to write this twice, Once here, other down*/
break;
default:
cout<<"Your search did not match any record";
}
/*To show record or to skip*/
cin>>rep; /*It also forces to write this twice, Once here, other up*/
switch(rep)
{
case'y':
case'Y':
void john();
john();
break;
default:
cout<<"OK";
}
/*To show record or to skip*/
}
/*Search Function*/
The problem is that 'John' is not a character, it's a syntax error. A character is denoted by the single quotes, and can only be one character long, such as 'a' or '3'.
A multi-character array of chars is called a "string":
ex: "hello", "12", "John", denoted by the double quotes.
switch statements are very limited: They can only work with ints and chars (integral types).
If you want to compare strings, you have to use plain if-else statements. Use std::string and use the == operator to compare my_string to "John".
ex:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
//at top of code:
#include <string>
//...
std::string str;
cin >> str;
if (str == "John" || str == "john" || str == "JOHN")
{
cout<<"Your Search returned: Johny Depp"<<endl;
cout<<"Want to see full details? (Y/N)";
cin>>rep;
}
else
{
cout<<"Your search did not match any record";
}
Your rep variable switch statement is OK because you are only using single letters 'y' and 'Y'.
@leryss, I got your point, Genado's reply helped too. I use if statement as you guys told me to, if(str == "John"), When i tested the program, i entered exactly same "John" but it did not go into the if statement and displayed 'else statement'. as I have no record.
Using Same as below:
Use strings, not char arrays. They're safer and easier:
1 2 3 4 5 6 7 8 9 10
string str; // string because you want it to contain a string
char rep; // char because it will only contain one char
//
if(str == "John") // <- now this will work (double quotes for string)
// ...
if(rep == 'y') // <- this will work with single quotes (single quotes for char)
#include <iostream>
#include <string>
usingnamespace std;
string keyword[] = //string array (no ',' to last element)
{ //string array initialised before main so you wont need to use pointers for functions
"John Dumb",
"Alex Bro",
"Annie Whore"
};
bool compare(string name, int i); //case sensitive compare function(i.e : input JOHN or JoHN it will still find John Dumb )
int main ()
{
bool found = false; //boolean value in case we dont find any match up for keyword
string name;
cout<<"Enter Keyword to Search: ";
cin >> name;
for(int i = 0; i < 3; i++)
{
if (compare(name, i)) //using the function to compare strings
{
found = true; //if we find a match up the not found message will not be displayed
cout<<"Your Search returned: "<< keyword[i] << endl;
cout<<"Want to see full details? (Y/N)\n";
//... do things
}
}
if(found == false) // if match up not found ...
cout << "Not found";
}
bool compare(string name, int i)
{
string nameAux = keyword[i]; // creating a new string so it will be easier to compare them
bool x ;
for(int j = 0; j < name.size(); j++)
{
if( name[j] == toupper(nameAux[j]) || name[j] == tolower(nameAux[j]))
//if element j from name is equal with upper case or lower case of element j from nameAux then x = true
x = true;
else
{
x = false; // else x = false
break; // stops the loop ,if you dont stop the loop next elements may be equal and x gets true value
}
}
return x; //return the value of x to if statement
}