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