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");
	}
}

Open in new window

reesee324Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Infinity08Commented:
>> if (code == "A")

You can't compare strings like that in C.

Either use strcmp (http://www.cplusplus.com/reference/clibrary/cstring/strcmp/) to compare entire strings :

        if (!strcmp(code, "A"))

or just compare the first character of the string :

        if (code[0] == 'A')
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Kent OlsenDBACommented:
Hi reesee,

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
      }
0
phoffric\Commented:
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.
   
/*******************************  
*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;
        }  
    }
}

Open in new window

0
phoffric\Commented:
If the code does not compile, then add
#include <string>
using namespace std;

Open in new window

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.
0
reesee324Author Commented:
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!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.