Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   Problem with checking input (http://www.go4expert.com/forums/checking-input-t21697/)

gregarion 8Apr2010 02:54

Problem with checking input
 
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 8Apr2010 04:54

Re: Problem with checking input
 
change
if (check=1) ----> if (check==1)

xpi0t0s 8Apr2010 11:48

Re: Problem with checking input
 
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 8Apr2010 11:52

Re: Problem with checking input
 
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?


All times are GMT +5.5. The time now is 09:51.