Problem with checking input

gregarion's Avatar, Join Date: Feb 2010
Light Poster
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.
virxen's Avatar, Join Date: Nov 2009
Pro contributor
change
if (check=1) ----> if (check==1)
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
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).
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
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?