Link to home
Start Free TrialLog in
Avatar of nouellette
nouelletteFlag for United States of America

asked on

Confused about C++ 'char' values

I have another fairly simple C++ question.  I'm not very good at programming and I posted a similar question last week.  

I understand basic char values in terms of integers and one character inputs and letters...but I think I'm having trouble with text based variables.

I'm attmepting to write a simple program that asks the user to input text...if the user inputs the right text, the program performs a function or it ends, if the user doesn't enter the right text, it keeps asking.  Pretty simple "while" or "if" statement.  But...I need help with the value of the text input.  I thought this code should do it but it's simply not ending and keeps asking, even when I enter the right text:  Here is an example:

#include <iostream>
#include <string>
using namespace std;

int main()
{
     char b1[10];
     
     while(b1 != "blue")
     {
          cout << "Enter the word 'blue' please: ";
          cin >> b1;
         
          cout << endl;
     }

     
     cout << endl << "thank you for entering blue" << endl;

     return 0;
}

So here I defined b1 with 10 character spaces.  Is that the correct way?  I simply want to say if b1 does not equal the word BLUE, to keep asking.  If they type BLUE, nove on to "thank you" and end.  Or this could be an IF statement but I need assistance with text values for CHAR.  Is that an array??  Am I correct in including the <STRING> library or is that not necessary?  I've tried single quotes, double quotes, but I'm not sure of the syntax for defining a variable for text and then saying "if this equals "BLUE" or in this case "does not equal BLUE".  

THanks in advance.

(Im using Microsoft Visual C++, many times I see C code suggested here instead of MS C++, so please suggest C++ code!)
Avatar of Exceter
Exceter
Flag of United States of America image

Try this,

#include <iostream>
#include <string>
using namespace std;

int main()
{
    char b1[10];
   
    while( strcmp( b1, "blue" ) != 0 )
    {
         cout << "Enter the word 'blue' please: ";
         cin >> b1;
         
         cout << endl;
    }

   
    cout << endl << "thank you for entering blue" << endl;

    return 0;
}

Exceter
>> So here I defined b1 with 10 character spaces.  Is that the correct way?

Yes it is. C and C++ use null terminated strings. That is, you declare the char array with enough room to store the desired value, perhaps with a little extra room. When the user enters a word say "blue," for example, the word blue is stored in the first four bytes of b1 and then a fifth byte is used to tell the program that this is the end of the string.

>> I simply want to say if b1 does not equal the word BLUE, to keep asking.  If they type BLUE, nove on to "thank you" and end.

My code does this.

>> Is that an array??  

Both b1 and the text "blue" are and array because a char can only contain one character. Thus to hold more than one character you need an array of chars.

>> I've tried single quotes, double quotes, but I'm not sure of the syntax for defining a variable for text and then saying
>> "if this equals "BLUE" or in this case "does not equal BLUE".  

You need to use the strcmp() function to compare char arrays, as I did in my code.

Exceter
ASKER CERTIFIED SOLUTION
Avatar of Salte
Salte

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial