-2

I was seeking some help on an issue. I have to read certain "passwords" from a .txt file, like "abE13#" and do some simple error checking to make sure they fit certain requirements. But at the current moment, it's printing out the passwords (which is meant to be done), but it's ignoring the checking and gets stuck in a loop where new lines are being printed out. I'm sure it has to do something with while(ch!='\n') but I'm not all that sure what is needed there in place of that to check.

ch = inFile.get();
while(!inFile.eof())
{
    while(ch != '\n')
    {
    cout << ch;
    if(isalpha(ch))
        {
            charReq++;
            if(isupper(ch))
                uppercaseReq++;
            else
                lowercaseReq++;
        }
    else if(isdigit(ch))
        {
            charReq++;
            digitReq++;
        }
    else if(isSpecial(ch))
        {
            charReq++;
            specialCharReq++;
        }
     if(uppercaseReq < 1)
           cout << "\n missing uppercase" << endl;
     ch = inFile.get();
    }
}

It's supposed to kind of follow this format,

Read a character from the password.txt file

while( there are characters in the file )
 {
 while( the character from the file is not a newline character )
{
Display the character from the file

Code a cascading decision statement to test for the various required characters

Increment a count of the number of characters in the password

Read another character from the password.txt file
}

Determine if the password was valid or not. If the password was invalid,
display the things that were wrong. If the password was valid, display that it
was valid.

Read another character from the file (this will get the character after the
newline character -- ie. the start of a new password or the end of file)
}

Display the total number of passwords, the number of valid passwords, and the
number of invalid passwords

kenny10009
  • 107
  • 1
  • 8
  • 1
    Welcome to Stack Overflow! It sounds like you may need to learn how to use a debugger to step through your code. With a good debugger, you can execute your program line by line and see where it is deviating from what you expect. This is an essential tool if you are going to do any programming. Further reading: [How to debug small programs](http://ericlippert.com/2014/03/05/how-to-debug-small-programs/). – Paul R Mar 24 '16 at 19:58
  • You have _`while(inFile)`_. I think [Why is iostream::eof inside a loop condition considered wrong?](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) is heavily related. – πάντα ῥεῖ Mar 24 '16 at 20:00
  • `cout << ch;` is outputting all of the characters as they are read. This is why the passwords are being printed – Thomas Zwaagstra Mar 24 '16 at 20:09
  • The passwords are meant to printed out, but it loops through an infinite cycle of printing out new lines. – kenny10009 Mar 24 '16 at 20:13
  • @kenny10009 Check my answer – DimChtz Mar 24 '16 at 20:14

3 Answers3

0

It keeps prints because of this while(inFile). This is always true. Change it to an if statement just to check if file is open:

if ( inFile )

EDIT: It will stop at the first password because of this while(ch != '\n'). When he gets to the end of the first password ch will be '\n', while fails and stop reading. Change it to:

while( !inFile.eof() )
DimChtz
  • 4,043
  • 2
  • 21
  • 39
  • I tried this, but it only seems to print out the first password, and not the rest. – kenny10009 Mar 24 '16 at 20:18
  • @kenny10009 With these changes it whould be printing correctly – DimChtz Mar 24 '16 at 20:25
  • Outer while loop is `while( !inFile.eof())` and inner is still `while(ch != '\n')`, it still loops through printing blank new lines – kenny10009 Mar 24 '16 at 20:26
  • Didn't you read my answer? Change the first while to `if ( inFile )` and the second while to `while( !inFile.eof() )` – DimChtz Mar 24 '16 at 20:27
  • I don't have the option to change them from `while` to `if`, well I can, it was just stated to me that it has to be done with two `whiles` – kenny10009 Mar 24 '16 at 20:31
-1
while( the character from the file is not a newline character )

You have converted this line of pseudocode into this line of c++ code:

while (ch != '\t')

'\t' is the tab character, not the newline character. This could definitely cause problems as to why you are never ending and instead just printing out new lines (Really EOF, but you don't see that).

'\n' is the newline character.

Give that a try.

EDIT:

Also, your only checking for an entire ifstream to be false. I don't quite know when that would happen, but I would recommend checking for the EOF flag. Your code should turn into something along the lines of this:

while( !inFile.eof() )
{
    while(ch != '\n' && !inFile.eof() )
    {
        // ...
    }
}
Cmoraski
  • 78
  • 4
-1

If you don't check for infile twice, you may end up in an infinite loop.

while(infile.good())
{
    while (inFile.good() && ch != '\n')
    {
    ...
    }
    if (ch == '\n')
    {...}
    else
    {...}
}
Robert Jacobs
  • 3,266
  • 1
  • 20
  • 30