Finding int in string

Is there a way to check what int a string has? For example, a string named "Choice" holds the value of "A1". I can find the 'A' with Choice[0] == "A'. When I try if Choice[1] == 1, it returns nothing.

Here's my code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <string>

int main()
{
        string Choice;
        getline(cin, Choice);

	if(Choice[1] == 1)
	{
		cout << Choice[1]; //This doesn't print anything.  
	}

	for(char a = 'A'; a <= 'Z'; ++a)
	{
		cout << Choice[0]; //This works.
	}

        return 0;
}


Any help would be appreciated.
Last edited on
You sure can.
try this
1
2
3
4
string str = "A1B3Apple35C8";
    for(int i = 0; i<str.size(); i++)
    if(isalpha(str[i])) cout << "character " << i << " in string \"str\" is: " << str[i] << " which is a letter." << endl;
    else cout << "character " << i << " in string \"str\" is: " << str[i] << " which is a number." << endl;
Last edited on
oh and by the way it would have to be "1" not 1 if you are doing the == because it still is a cosnt char (string) not an int. if you wish to convert it to an int you would have to do int num = atoi(choice[positionof1]);
Thank you for the help, but I have a couple of questions.

Can you explain isalpha to me? I looked it up in the reference section but still didn't get it. In your code, why does it even print letters? I thought it was only for checking numbers?

Also, I can't put the 1 in quotes or I get this error:

operand types are incompatible ("char" and "const char *")

Finally, what's the difference between isdigit, isalpha, stoi, and atoi? I looked them up in the reference section and they seem to do exactly the same thing.
Last edited on
@giblit Your code is flawed. Your if statement if(isalpha(str[i])) evaluates to true if the input is a letter. In your code anything that isn't a letter is considered a number. So the '!', '?', '.' and '@' characters are all considered numbers. Why not use the function designed specifically for the purpose of finding digits?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <string>
using namespace std;

int main()
{
	string foo;

	cout << "Enter a string: ";
	cin >> foo;
	cin.ignore();
	cout << endl;

	for(int i = 0; i < foo.length(); i++)
	{
		if(isdigit(foo[i]))
			cout << "Character " << foo[i] << " at element " << i << " is a digit " << endl;
	}
	
	cin.ignore();
	return 0;
}
Last edited on
@yayu This if statement will work if(Choice[1] == 1) if you put the 1 in single quotes. if(Choice[1] == '1')

isdigit checks to see of a character is a digit
isalpha checks to see if a character is a letter
stoi converts a string to an integer (class string)
atoi converts a cstring to an integer (char array)
Last edited on
Thank you very much! Everything works now and I understand better.
@yanson sorry it was late at night and made the code example fast with out trying it i'm not sure why I checked if it was alpha and not digit lol
Topic archived. No new replies allowed.