Looping

I need help with the loop
Im trying to only read the numbers in a string.
By the brute-force way would like check character by character
for example a43, 435, 4mi4v, 2fs, 405
the only ones that would be consider numbers are 435 and 405.
Any help will be very much appreciated

#include <iostream> // libraries
#include <iomanip>
#include <string>

using namespace std;

int main()
{
char string[80];
int i;
cout << "enter a string "; // prompting user
cin.getline(string,80); // geting line
i=0;
while (isdigit(string[i])) i++;
if (string[i]<'0' || string[i]>'9')
{
cout << "not a number" << endl;
}

}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//code tags
#include <iostream> // libraries
#include <iomanip>
#include <string>//you are not using this

using namespace std;

int main()
{
char string[80];//this should not compile/is not a string
int i;
cout << "enter a string "; // prompting user
cin.getline(string,80); // geting line
i=0;
while (isdigit(string[i])) i++;//what are u doing here?
if (string[i]<'0' || string[i]>'9')//if you are doing the while loop this should not be needed
{
cout << "not a number" << endl;
}
return 0; //you NEED a return value
} 
Topic archived. No new replies allowed.