Checking how many numbers in string - Help

Hi,

I need to check how many numbers are in my string. I know there is already a header that has a function to check this. But I wanted to learn and make my own function.

I am having trouble doing so, can anyone tell me if I am on the right track? And how to get past this error?



Link to my project: https://repl.it/@codecaine1/AffectionateUsedPyramid
The if statement needs some adjustment. You're not comparing s[i] with a single integer. You're comparing it with the whole string including the commas.
I forked your repl and made some changes... I've never used that platform, so I hope I did it correctly c':

https://repl.it/@quiyetbrul/StarryWretchedAddition
Last edited on
Hello codecaine1,

Your code is short. Just post it here. I had some trouble with your link, but it could have been just my computer running slow this morning.

Your if statement if (s[i] == "1,2,3,4,5,6,7,8,9") is comparing a char to a string and it needs to compare a "char" to a "char". The use if "isdigit()" is a better choice and if you do not want to use that you would need:
1
2
3
4
if (s[i] == '0')
	count++;
else if (s[i] == '1')
	count++;

And continue on with the "else if" statements for the rest of the numbers. Notice that the number is in single quotes because it is a "char" and not a number.

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
31
32
33
34
#include <iostream>
#include<string>

//using namespace std;  // <--- Best not to use.
// A recent post that is worth reading. http://www.cplusplus.com/forum/beginner/258335/

// find how many numbers in string

int numOfInt(std::string s)
{
	int count{};

	for (int i = 0; i < s.size(); i++)
	{
		//if (s[i] == "1,2,3,4,5,6,7,8,9")
		if(isdigit(s[i]))
		{
			count++;
		}

	}

	return count;
}

int main()
{
	//std::string s = "W3 Are Cool";
	std::string s{ "1 how 2 many 3 numbers 4 here?" };

	std::cout << "\n There are " << numOfInt(s) << " numbers in the string \"" << s << "\".\n" << std::endl;

	return 0;
}

I changed the output in "main" to show more of what you are doing. You can adjust that any way that you like.

Hope that helps,

Andy
Thanks for the comments. I am trying to avoid isDigit().

@Andy

I get a weird number like 36274 when I do this:

if (s[i] == '1')
count++;
@codecaine1

Here's the program without using isdigit() and using only one if statement.

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
#include <iostream>
#include <string>

using std::string;
using std::cout;
using std::endl;

int numOfInt(string s)
{
	int count=0;

	for (int i = 0; i < s.size(); i++)
	{
		if(s[i] >= '1' && s[i] <= '9')
			count++;
	}

	return count;
}

int main()
{
	string s= "1 how 2 many 3 numbers 4 here?";

	cout << "\n There are " << numOfInt(s) << " numbers in the string \"" << s << "\"." << endl << endl;

	return 0;
}


EDIT:

Whoops. Forgot to include checking for zero's.
The i loop should look like this, instead.
1
2
3
4
5
for (int i = 0; i < s.size(); i++)
	{
		if(s[i] >= '0' && s[i] <= '9')
			count++;
	}


Sorry for any confusion.
Last edited on
codecaine1’s code:
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
include <iostream>
#include<string>
using namespace std;

// find how many numbers in string

int numOfInt(string s)
{
 int count=0;
  
  for(int i=0; i<s.length(); i++)
  {
    if(s[i] == '0'|| s[i] == '1' || s[i] == '2' || s[i] == '3')
    {
      count++;
    }
  }
  
  return count;
}



int main()
{
  string s = "W311 Are Cool";
  
  cout << numOfInt(s);
}

I am concerned about your vocabulary. Is your assignment really just to count the number of digits in the string, or are you trying to count the number of integer numbers in the string?

Because those are two different things.

Also, remember, you can use the inequality operators to identify digits:

1
2
  if (('0' <= s[i]) && (s[i] <= '9'))
    // then I have found a digit 

Hope this helps.
Hello codecaine1,

PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.

Along with the proper indenting it makes it easier to read your code and also easier to respond to your post.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.

I found the second link to be the most help.


Post the program here and do not make people follow a link. That is for your benefit. Some people do not always have the time to follow your link and you could be missing out on some good comments.

You wrote:

I get a weird number like 36274 when I do this:

1
2
if (s[i] == '1')
count++; 


So what gets the weird number and how do you know this?

With this code:
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
#include <iostream>
#include<string>
using namespace std;

// find how many numbers in string

int numOfInt(string s)
{
 int count=0;
  
  for(int i=0; i<s.length(); i++)
  {
    if(s[i] == '0'|| s[i] == '1' || s[i] == '2' || s[i] == '3')
    {
      count++;
    }
  }
  
  return count;
}



int main()
{
  string s = "W311 Are Cool";
  
  cout << numOfInt(s);
}

I ran the program at the link in your OP and again with the shell program here with no problems that I could see. Tested the program in VS2017 compiling to the C++14 standards and again no problem.

So I need a little more information than just the if statement to know what is happening.

The new code works. I would suggest using whitenite1 if statement as it has the ability to catch all numbers. Duthomhas's if statement also works, but you need to finish it for the rest of the numbers.

A couple of tips that may help:

In your first post it is best to include the whole program. This includes any header you wrote and any input file that the program may use. Should the input file be large a fair sample will work, say five to ten lines or records.

After your first post DO NOT make changes to this code. It just confuses people when they can not find anything wrong with the program and it makes the replies hard to understand. Put any changes in a new message to show your progress.

The top part of this message is just in case you are not familiar using the tags especially the code tags.

Hope that helps,

Andy
Dúthomhas's if statement works for all Arabic (Western) digits.
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
31
32
33
34
#include <iostream>
#include <string>
using namespace std;


int numDigits( const string &str )
{
   int result = 0;
   for ( char c : str ) if ( '0' <= c && c <= '9' ) result++;
   return result;
}


int numNumbers( const string &str )
{
   bool state = false;
   int result = 0;
   for ( char c : str )
   {
      bool last = state;
      state = ( '0' <= c && c <= '9' );
      if ( (!last) && state ) result++;
   }
   return result;
}


int main()
{
   const string str = "101 dalmations, 49 red balloons, 747 jumbo jet";
   cout << "String: "  << str               << '\n';
   cout << "Digits: "  << numDigits( str )  << '\n';
   cout << "Numbers: " << numNumbers( str ) << '\n';
}
String: 101 dalmations, 49 red balloons, 747 jumbo jet
Digits: 8
Numbers: 3


Topic archived. No new replies allowed.