Thread: Problem with Checking Value

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    2

    Problem with Checking Value

    Hey guys. I am trying to write a program which will read data from a text file and then check if the data contains only numerals or not.

    My text file is basically in this format...

    Code:
    12
    34
    red
    Basically, the expected output of my program is that it will check the data and then tell me if the data is in numbers only or not. For this example, the output should be

    Code:
    yes!
    yes!
    no
    Here is my coding

    cpp file
    Code:
    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
    Code:
    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
    Code:
    using namespace std ;
    
    class CheckType
    {
    public :
        int CheckNumeral (string& ) ;
    private :
    
    };
    Using the prog i wrote , the output i get when i run the example text will be...

    Code:
    no
    no
    no
    Can sombody help me to identify the problem and tell me what might be the problem? hope for some help. Thanks

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I would suggest doing something like
    Code:
    std::cout << "|" << templine << "|";
    in your code to see if you have (say) spaces at the end of your lines that are getting picked up by readline.

  3. #3
    Registered User
    Join Date
    Mar 2008
    Posts
    36
    I've pieced together your code (didn't bother using a header file) and it seems to work fine for me?

    Code:
    #include<iostream>
    #include <fstream>
    #include <string>
    using namespace std;
    
    class CheckType
    {
    public :
        int CheckNumeral (string& ) ;
    private :
    
    };
    
    int CheckType:: CheckNumeral(string& num)
     {
       for (int i = 0; i < num.length(); i++) {
           if (!isdigit(num[i]))
               return 1;
       }
       return 3;
    }
    
    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;
    }
    
    }
    gives output :

    Code:
    yes!
    yes!
    no

  4. #4
    Registered User
    Join Date
    Apr 2010
    Posts
    2
    Really? you managed to run it and get the correct output? i tried running it on another computer and used the codes u pasted. yet i still get the same error. anybody know other ways to check for numerals?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  2. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  3. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  4. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  5. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM