May 3, 2012 at 9:45pm UTC
I am trying to read a string and tell whether or not it is a number or not. The program below work but i believe my problem is in my range. the loop is only reading 3 digits in a number. how do i make it read all the characters up into a space ?
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 35 36 37 38 39 40 41 42 43 44 45 46 47 48
#include <iostream> // libraries
#include <iomanip>
#include <string>
using namespace std;
int main()
{
char str[80];
int i;
int counter=0;
cout << "enter a string " ; // prompting user
cin.getline(str,80); // geting line
cout<<endl;
i =0;
while (str[i]!='\0' )
{
if (str[i] == ' ' )
{
for (int x=i-3; x<i; ++x)
{
if (str[x]<'0' || str[x] >'9' )
{
++counter;
}
}
if (counter>=1)
{
for (int x=i-3; x<i; ++x)
{
cout<<str[x];
}
cout<<" is not a number" << endl;
}
else
{
for (int x=i-3; x<i; ++x)
{
cout<<str[x];
}
cout<<" is a number" << endl;
}
counter=0;
}
i++;
}
}
enter a string 435 dsaf 23 sd 3
435 is a number
saf is not a number
23 is not a number
sd is not a number
Press any key to continue . . .
Last edited on May 3, 2012 at 9:50pm UTC
May 4, 2012 at 5:17pm UTC
If you want to know a string's length, use the string.length object.