• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 296
  • Last Modified:

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

0
reesee324
Asked:
reesee324
1 Solution
 
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
 
Kent OlsenData Warehouse Architect / DBACommented:
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
 
phoffricCommented:
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
 
phoffricCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now