Problem with checking input

Discussion in 'C' started by gregarion, Apr 7, 2010.

  1. gregarion

    gregarion New Member

    Joined:
    Feb 4, 2010
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    Hey guys. i am trying to write a program which will read input from a text file, check if each line contains any alphabets and then display a message imforming me if there is an alphabet in each line. My text file is formatted in this way...

    Code:
    123
    562
    red
    21e
     
    Based on this example, the expected output of my program would be..

    Code:
    No alphabets!
    No alphabets!
    Aplhabets!
    Alphabets!

    Here the coding which i have done...
    Code:
     class CheckType
    {
    public :
        int CheckNumeral (string&) ;
    private :
    };
    #include <stdlib.h>
    #include "header.h"
     
     int CheckType::CheckNumeral(string&num)
     {
         int check = 0 ;
       for (int i = 0; i<num.size(); i++) {
           if (!isdigit(num[i])){
     
               check = 1;
           if (check = 1)
               cout << "Alphabets!" << endl;
              num = "";
               check = 0;       
           }   
           else {
              cout << "No alphabets!"<< endl;
       num = "";
       check = 0;
           }
           num = "";
       }
     }
     
    int main()
    {
        fstream readFile("Testing.txt");
        if (readFile.is_open())
        {
        string templine ;
        CheckType ct ;
      //  ofstream myfile;
    // myfile.open ("New.txt");
    while (getline(readFile,templine))
    {
     
    ct.CheckNumeral(templine);
        }
        }
    }

    Based on my coding now , when i the text file has data like this...

    Code:
    111
    222
    red

    I will get the correct output which is...

    Code:
    No Alphabets!
    No Alphabets!
    Alphabets!

    but when my text file has data like this...
    Code:
    111
    222
    red
    1r
    I would get an output which says...

    Code:
    No Alphabets!
    No Alphabets!
    Alphabets!
    No Alphabets!
    which is wrong as the last input does have an alphabet in it.

    I been trying out different ways to figure it out. Hope for some help in pointing out where the mistake is please.
     
  2. virxen

    virxen Active Member

    Joined:
    Nov 24, 2009
    Messages:
    387
    Likes Received:
    90
    Trophy Points:
    28
    change
    if (check=1) ----> if (check==1)
     
  3. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    You need to get into the habit of indenting your code correctly. This makes the problem obvious (you're only checking the first character in the line)
    Code:
    int CheckType::CheckNumeral(string&num)
    {
        int check = 0 ;
        for (int i = 0; i<num.size(); i++) {
            if (!isdigit(num[i])){
    
                check = 1;
                if (check = 1) // = is assignment, == is comparison.
                    cout << "Alphabets!" << endl;
                num = "";
                check = 0;       
            }   
            else {
                cout << "No alphabets!"<< endl;
                num = "";
                check = 0;
            }
            num = ""; // what's this line for???
        }
    }
    
    Proper indenting is an essential debugging skill consistently used by experts, not just some unnecessary retarded thing that teachers insist on for no good reason (they save that for scanf).
     
  4. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    But even if you change = to == that block doesn't make sense. Assign 1 to check. If check is 1 (which it will be, cos you've just assigned 1 to it) then display "Alphabets!". Bizarre. Why not just display "Alphabets!" without bothering with the check variable?

    Then you do this again: num = "";
    I can't help wondering what you think that line does; could you explain please?
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice