Hey guys , i would like to ask some question regarding checking if a string is all integer. First off, i wrote a program to check if the value i enter contains strings.

string str;
    int n = 0 ;
    cout << "Enter the letter please "<< endl;
    cin >> str;
     const char* fd = str.c_str ();

   //  for(int i=0;i<str.length();i++)
    
         if (isdigit(fd[n]))
     {
             n++;    }
     else
     {
         cout <<"Not all are numbers"<< endl;
         
     }

}

My expected output is if the string only hold integers (0-9) , it will then print the string out. if not , it will print another message telling me it does not contain numbers only.

I tried running my coding. but it does not seem to be able to work when i input in information such as "12d".

Hope for some help here and if you all know a better way to check , please guide me. Thanks!

Recommended Answers

All 8 Replies

Its pretty close. You had the right idea to use a for loop and use isdigit
function. You just didn't do it correctly.

Here are basic steps you can try :

1) Get input into a string
2) Create a for loop from i = 0 to string.length
3) use isdigit inside an if condition inside the for loop
4) if "3" fails then exit the loop and show and error message
5) If the loop does not fail then you have an string with all digits.

Hey , i tried out your way and my program is working fine if i just input in 1 value. What i tried to do is to read the input from a text file which contains information in this way.

12
34r

Here is my program...

here the cpp file

int CheckType:: CheckNumeral(string& num)

 {

     int a;
     int b;
   for (int i = 0; i < num.length(); i++) {
       if (!isdigit(num[i]))
           return 1;
   }
    
   return 3;
   
}

main file.

int main()
{
    fstream readFile("Note.txt");
string templine ;
string line ;
    CheckType ct ;

while (getline(readFile,templine))
{
    if (templine != " "){
ct.CheckNumeral(templine);
 if (ct.CheckNumeral(templine)== 3){
       cout<<"yes!"<<endl;
  }else if(ct.CheckNumeral(templine)== 1){
       cout<<"no"<<endl;
    }
  else{
      cout << "do not know"<< endl;
  }
    }
     else
    {
        cout << "i am done" << endl;
    }

Header file.

using namespace std ;

class CheckType
{
public :
    int CheckNumeral (string& ) ;
private :

};

the problem i am facing is, when i input only one variable into the text file, it is able to detect if there is is only numerals in each line.
But when i input two or more values, the output all comes out wrong.

Anybody knows what is wrong with my coding?

What does "comes out wrong" mean? Maybe details would help.

Read this
I think you've forgot that isdigit checks 1 char at a time . Not several chars

What comes out wrong? well for example, if my text file just contains one input such as "12", when i run it in the program , i get the output of "yes!" which is the expected output.

But when my text file contain multiple information such as ...

12
34
re

the expected output should be...

yes!
yes!
no

but the result i get is..

no
no
no

First thing I'd do is format your code so it can be followed easier.

Then, in CheckNumeral() display the important values as the code runs. See if there is a bad character being read, like a space or something.

okay. i managed to solve part of the problem . I managed to display the correct input out when my text file has values such as..

12
12
red

The output i get now is correct which is....

Yes!
Yes!
no!

But i realize that one problem which i did not realize is that it only checks char , so for example, if my text file has data such as ,

1e
12
red

The expected output should be ..

no
yes
no

But what i am getting is...

yes
yes
no

I figured that its because of the first value of the first input , which is '1' , thus the program read it as a digit and does not check the rest of the string.

Does anybody know how i can modify my current coding in order for the prog to check the entire string and not just part of it?

My current coding...

int CheckType:: CheckNumeral(string& num)


 {

   for (int i = 0; i < num.length(); i++) {
       if (!isdigit(num[i]))
         return 2;
       num = "";

class CheckType
{
public :
    int CheckNumeral (string& ) ;
private :

};

int main()
{
    fstream readFile("Testing.txt");

    if (readFile.is_open())
    {

    string templine ;
    CheckType ct ;


while (getline(readFile,templine))
{
    string okay;
    okay = templine ;
    
ct.CheckNumeral(okay);
 if (ct.CheckNumeral(okay)== 1){
       cout<<"yes!"<<endl;

  }else if(ct.CheckNumeral(okay)== 2){
       cout<<"no"<<endl;
    
    }
    }
    }
    else
    {
        cout << "File do not exist" << endl;
    }
}

   }
   return 1;
   num = "";

}

I just told you .. do you want someone to do it for you?
How to check if digit ?
Just split the number for ex you've got the number 1250405 you're going to split it multiple times like this 1 2 5 0 4 0 5 then check if all digits ; if all of them where digits then the text you've entered is a valid number .
If you check this 12345E6789 you're going to split it in 1 2 3 4 5 E 6 7 8 9 when you're going to check if 'E' is a digit it will return as false ==> you're going to give the answer "no"
I just said that in a previous reply , but i think you just want some1 to make your code + you're idea is bad you're complicating a lot .

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.