Another C++ question.

FrogDog

Diamond Member
Jan 12, 2000
4,761
0
0
I've looked over and manipulated this code for an hour and I can't get it to run. I also can't find an answer in my book as to why it won't work. Here's my code:

#include <iostream.h>

int main()
{

const int MAX = 60;
char element[MAX];

cout << &quot;Type the element you would like to look up, or its symbol, and press enter:\n&quot;
<< endl << endl;
cin.getline(element, MAX, '\n');


if(element[MAX] == 'H' || 'h' || 'Hydrogen') <---------error
{
cout << &quot;You picked Hydrogen.&quot;;
}


return 0;
}

The error it gives me is this: Too many characters in constant.

If I limit the 'Hydrogen' in the if statement to 3 or less characters it runs fine....
:confused:
 
Mar 5, 2001
66
0
0
umm....you have 'hydrogen' in signle quotations.

you have to put it in double quotes.

how are you comparing a character to a string, anyway? rather...why?

if you need any help, feel free to ask (or e-mail me).

cheers.
 

juiio

Golden Member
Feb 28, 2000
1,433
4
81
First of all, your if statement will always evaluate to true.

Second of all, a character is 8 bytes. The value in it can only represent one ascii character.

Lastly, you gather in a whole ine of input, but you only compare one character.
 

sojin

Member
Sep 19, 2000
145
0
0
if i understand the functionality of ur code correctly, you may replace the if condition with:

if (!strcmp(element, &quot;H&quot;) || !strcmp(element, &quot;h&quot;) ||!strcmp(element, &quot;Hydrogen&quot;) )

that will check if user input is either a 'H'/'h' or a word &quot;Hydrogen&quot;...
 

indd

Senior member
Oct 16, 1999
313
0
0

It seems like what you're trying to do is display &quot;You picked hydrogen.&quot; if the user enters enter characters 'H' or 'h' or if they enter the string &quot;Hydrogen&quot;. There's a couple of problems here..

Right now, the if(element[MAX] == ... part refers to the 61st element (0 is the 1st, 59 is the 60th) of the element string array. That's going to cause some problems right there. What you probably want to do is refer to the first character in the element array. So of the user types:

H

element will be equal to &quot;H\0&quot;

So you'd want to change the code to if(element[0] == .....)

The next problem is with the or statements. When you do if statements you test a series of boolean expressions. So let's say we have this code:

int a = 5;

if( a == 5 ) { do this... }
// the &quot;a == 5&quot; is the boolean expression here
else { do something else }

A boolean expression is false if the value is zero or NULL.
It's true if the value is anything besides zero or NULL.

So:
if( a ) { cout << &quot;not zero\n&quot;; }
else { cout << &quot;is zero\n&quot;; }

This means that the code (I changed it some here) has a problem:

if(element[0] == 'H' || 'h' || &quot;Hydrogen&quot;)

The last two parts (|| 'h' || &quot;Hydrogen&quot;) will always evaluate to true (they are nonzero), so this expression will always be true (ie, x || 1 || 1 ). The code is actually not comparing the value of element[0] with the 'h' and &quot;Hydrogen&quot;..

What you really want to do is something like:

if( (element[0] == 'H') || (element[0] == 'h') ||
|| (element[0] == &quot;Hydrogen&quot;) )
//this last part needs some changes described below

The last problem has to do with the 'Hydrogen' part of the or statement. You refer to single characters with single quotation marks. You refer to strings with double quotation marks. That's why the words Hydrogen were changed to be surrounded with double-quotes. That's not all though.

The expression (element[0] == &quot;Hydrogen&quot;) doesn't make sense. The code is comparing a character to a character array. What you're trying to do is compare a string to a string. C has this function called strcmp (in the string library, so make sure to #include <string.h>) which compares two strings. You call it with int temp = strcmp( string1, string2 );.
It will return 0 if string 1 is equal to string2 (or, their lengths are the same and the characters inside are the same).. or a nonzero value otherwise.

So what you want is: (strcmp( element, &quot;Hydrogen&quot; ) == 0)

If you put it all together then,

-make sure to include <string.h> in your file
-change the if statement to:
if( ( element[0] == 'H' ) || ( element[0] == 'h' ) || ( strcmp( element, &quot;Hydrogen&quot; ) == 0 ) )

hope this helps..
indd