RUSES
Would you like to react to this message? Create an account in a few clicks or log in to continue.
May 2024
MonTueWedThuFriSatSun
  12345
6789101112
13141516171819
20212223242526
2728293031  

Calendar Calendar


comparing chars in a string. Help?

3 posters

Go down

comparing chars in a string. Help? Empty comparing chars in a string. Help?

Post  VicVic Wed Mar 07, 2012 1:50 am

Coding my maze game, and looking at inputting strings for items, like 'get key'
Need to compare the first letter of each input, so if someone types north/south/east/west/n/s/e/w it will check the move is valid and change coordinates. if the first letter of the string is 'g' it will do a full strcmp with 'get key' or 'get item' or something to determine the command.
Everything else works fine, apart from the first letter compare. I tried to implement it like this:

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

int main ()
{
string dir[20];
getline (cin, dir[20]);
if ((dir[0]) == 'N')
} </code>

But it tells me 'no operator "==" matches these operands, even though the compare worked fine when I was just working with chars.
Any ideas?

Thanks =)

VicVic

Posts : 17
Join date : 2012-02-28

Back to top Go down

comparing chars in a string. Help? Empty Re: comparing chars in a string. Help?

Post  23pointsNorth Wed Mar 07, 2012 2:22 am

From the quick preview -> you are comparing strings with chars.
The definition string dir[20] makes basically an array of strings(which is array of chars) => try either with changing to "string dir", or char dir[20].
Not to mention that you are reading the line on the 20th element of the string array(overflow?).
Anyway, i vote for removing the array brackets in the definition and in getline.

23pointsNorth

Posts : 2
Join date : 2012-02-23

Back to top Go down

comparing chars in a string. Help? Empty Re: comparing chars in a string. Help?

Post  DiJuMx Wed Mar 07, 2012 2:29 am

A good way of remembering is to treat string variables as though they are char* variables. They aren't exactly char* variables but still....

Anyway, in your code you are essentially creating an array of strings, e.g. {"String 1", "String 2"...."String 20"}

When you do your comparison, you are comparing a string ("String 1") to a char ('N')

Note the use of double- and single-quotes

The following code is a corrected version which should work.
Code:

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

int main ()
{
string dir;
getline (cin, dir);
if ((dir[0]) == 'N')
}

DiJuMx

Posts : 7
Join date : 2012-02-29

Back to top Go down

comparing chars in a string. Help? Empty Re: comparing chars in a string. Help?

Post  VicVic Wed Mar 07, 2012 2:39 am

Yay =D

Thanks

VicVic

Posts : 17
Join date : 2012-02-28

Back to top Go down

comparing chars in a string. Help? Empty Re: comparing chars in a string. Help?

Post  Sponsored content


Sponsored content


Back to top Go down

Back to top


 
Permissions in this forum:
You cannot reply to topics in this forum