reesee324
asked on
Passing in as a string and then testing to see if it is a character
This is an assignment for school but I have to pass the variable in as a string and then test if it is A, B, or C. I've attached my code because I can't exactly figure out what I am doing wrong.
/*******************************
*Function to get discount code *
*******************************/
void get_code (char prompt[], char code[])
{
printf("\n %s", prompt);
gets(code);
if (code == "A")
{
printf ("It works");
system("pause");
//store to structure
}
else if (code == "B")
{
printf ("It works");
system("pause");
//store to structure
}
else if (code == "C")
{
printf("It works");
system("pause");
//store to structure
}
else
{
printf("Please enter a discount code: ", code);
system("pause");
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Since you are writing a C++ program, I would advise using the C++ string type rather than the char [] type. The latter can easily overflow.
I've modified your function to show how to use the C++ string type. In your OP (lines 26-30) your function is trying to validate the input, which is incomplete. Below shows how to validate using a string type for the proper size and value.
I also use the getline function to read in the string (without having to worry about buffer overflow)
http://www.cplusplus.com/reference/string/getline/
The prototype has & in it which means you are passing by reference.
I've modified your function to show how to use the C++ string type. In your OP (lines 26-30) your function is trying to validate the input, which is incomplete. Below shows how to validate using a string type for the proper size and value.
I also use the getline function to read in the string (without having to worry about buffer overflow)
http://www.cplusplus.com/reference/string/getline/
The prototype has & in it which means you are passing by reference.
/*******************************
*Function to get discount code *
*******************************/
void get_code (string & prompt, string & code)
{
cout << prompt << endl;
bool validInput = false;
while( !validInput ) {
getline(cin, code); // read in user entry
if( code.size() != 1 ) {
cout << "Enter only one char: 'A', 'B', or 'C'" << endl;
continue;
}
validInput = true; // Got 1 char - assume input is valid
if (code == "A")
{
printf ("It works");
//store to structure
}
else if (code == "B")
{
printf ("It works");
//store to structure
}
else if (code == "C")
{
printf("It works");
//store to structure
}
else
{ // INVALID ENTRY
cout << "Enter a discount code: only one char: 'A', 'B', or 'C'" << endl;
validInput = false;
}
}
}
If the code does not compile, then add
#include <string>
using namespace std;
Notice that now the if statements correspond to your OP code. In fact you do not have to test for the size of the string being exactly 1, since if more chars are entered, then the == tests will fail anyway. But the size test is an extra nicety for providing the user a more refined explanation of what is wrong.
ASKER
Okay thanks! I tried the strcmp but I guess I was doing it wrong because I kept getting a break in my code. Thanks for the help!
Don't confuse a C++ string type with the C character array type. The style that you're using for comparison works with C++ strings, not with character arrays.
To test strings in C, you need to test all of the characters in the string. The comparison that you've coded tests the addresses of the strings. That won't be a lot of help. :)
Try using strcmp() to test that one string is equal to another.
if (strcmp (code, "A") == 0)
{
printf ("It works");
system("pause");
//store to structure
}