comparing chars in a string. Help?
3 posters
RUSES :: Part 1 :: Programming :: Coursework
Page 1 of 1
comparing chars in a string. Help?
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 =)
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
Re: comparing chars in a string. Help?
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.
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
Re: comparing chars in a string. Help?
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.
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
RUSES :: Part 1 :: Programming :: Coursework
Page 1 of 1
Permissions in this forum:
You cannot reply to topics in this forum