String to an int conversion... gah

Ok so I have this function Getguess that needs to take a string and convert it into an integer so I can compare it to the computers random guess. I've read up extensively on string stream, atoi etc, but for some reason, I just cannot get it to work correctly. I need to return combo as an integer. I left out my failed attempts at conversion in hopes some one can put me in the right direction.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
 int GetGuess ( )
{
	int const CARDS=18;
	std::string deck [CARDS]={"red circle","red square","red triangle","blue circle","blue square",
	 "blue triangle","yellow circle","yellow square","yellow triangle","orange circle",
	 "orange square" ,"orange triangle","purple circle","purple square",
     "purple triangle","green circle","green square","green triangle"};
     
     
	int answer=0;
	int i;

	
	std::string color, shape,combo;

	cout<<"Please choose a card with a color first, and then a shape.";
	cin>>color>>shape;
	combo= color + " " +shape;
	
 for (int i=0;i<CARDS;i++)
    {
        if (combo==*(deck+i))
        {
            combo=i;
            break;
        }
    }
    return (combo);
}
What do you actually want to return? The index of the combo in the deck?
So..In other words you are trying to find the index of the card?

What you should do is instead of combo = i on line 24 is assign i to a new variable that you declare before the loop like.

1
2
3
4
5
6
7
int index = 0;
for(...)
{
     ...

     index = i;
}


or alternatively you could just use a map and map the cards to numbers.

Though if you wanted to do combo = i. You would need to do something along the lines of

1
2
3
std::stringstream ss;
ss << i;
ss >> combo;


or
1
2
3
std::ostringstream ss;
ss << i;
combo = ss.str();


If you do it that way don't forget to #include <sstream>

Edit: btw you are using pointer notation with your arrays (it works fine though if that's what you prefer), the standard array notation is array[index] instead of *(array + index) though you can do index[array] or *(index + array) too if you wish.
Last edited on
I basically need to convert the cards to numbers after the user has selected them, the program is a game where the user will select 5 cards, and then check them against the computers guesses (by seeding random numbers, 1 to 18). The pointer notation is actually a requirement for this assignment, I'm unable to use an array name inside the code other than to declare the array. I have to point to it, which has been a royal pain.


Do you guys think mapping would be the best way, rather than how I'm trying to do the conversion here? I think I'm alot more prone to errors doing it the way I am after actually thinking about it.

This is the function idea:

GetGuess prompts the player to guess a card. The player enters a shape and a color. This function converts this input combination to an integer that represents that card, and returns that integer.

Thanks for the help guess, its much appreciated.
Topic archived. No new replies allowed.