while (scan != 'y' or 'n) or if(scan != 'y' or 'n)

This is a discussion on while (scan != 'y' or 'n) or if(scan != 'y' or 'n) within the C++ Programming forums, part of the General Programming Boards category; Code: #include <iostream> #include <string> #include <cstdlib> #include <stdlib.h> #include <windows.h> #include <stdio.h> #include <time.h> using namespace std; int Wait ...

  1. #1
    Linux Forever
    Join Date
    Oct 2002
    Posts
    373

    while (scan != 'y' or 'n) or if(scan != 'y' or 'n)

    Code:
    #include <iostream>
    #include <string>
    #include <cstdlib>
    #include <stdlib.h>
    #include <windows.h>
    #include <stdio.h>
    #include <time.h>
    using namespace std;
    
    int Wait ( int Seconds );
    
    int main ( void )
    {
    char scan;
    while (scan != 'n')
    {
    string a("Norton anti virus scanning \n");
    string c("7 Hits\n");
    string d("Viruses found: 4\n");
    string e("Code red Worm\n");
    string f("Nimda Virus\n");
    string g("Love Bug Virus\n");
    string h("Kevlarsmart Worm\n");
    string j("Trojans: 3\n");
    system("cls");
    cout << "scan system? Y or N: ";
    cin >> scan;
    cout << a;
    Wait (5);
    cout << c;
    Wait(3);
    cout << d;
    cout << e;
    cout << f;
    cout << g;
    cout << h;
    cout << "No files fixed\n";
    Wait(2);
    cout << j;
    cout << "No files fixed\n";
    Wait(4);
    cout << "Scan again? Y or N: ";
    cin >> scan;
        {
        while (scan != 'y' or 'n')
        cout << "Invalid command";
        Wait(2);
        return 0;
        }
    }
    }
    int Wait ( int Seconds )
    {
        clock_t endtime = clock() + Seconds * CLOCKS_PER_SEC;
        while ( ( clock() < endtime ) );
        return 0;
    }
    I can type s at the start and it scans. at the end, however, i get:

    Invalid commandInvalid commandInvalid commandInvalid commandInvalid commandInvalid commandInvalid commandInvalid commandInvalid commandInvalid commandInvalid commandInvalid commandInvalid commandInvalid commandInvalid commandInvalid commandInvalid commandInvalid commandInvalid commandInvalid commandInvalid commandInvalid commandInvalid commandInvalid commandInvalid commandInvalid commandInvalid commandInvalid commandInvalid commandInvalid commandInvalid

    Forever and ever.
    No matter what i type.
    Should i use a goto loop, a while loop, or if, or what?
    How can i fix that?

    It looks like this at the end:

    _________________________________
    _________________________________
    Scan again? Y or N: n
    Invalid commandInvalid commandInvalid commandInvalid commandInvalid commandInvalid commandInvalid commandInvalid commandInvalid commandInvalid commandInvalid commandInvalid commandInvalid commandInvalid commandInvalid commandInvalid commandInvalid commandInvalid commandInvalid commandInvalid commandInvalid commandInvalid commandInvalid commandInvalid commandInvalid commandInvalid commandInvalid commandInvalid commandInvalid commandInvalid commandInvalid
    Invalid commandInvalid commandInvalid commandInvalid commandInvalid commandInvalid commandInvalid commandInvalid commandInvalid commandInvalid commandInvalid commandInvalid commandInvalid commandInvalid commandInvalid commandInvalid commandInvalid commandInvalid commandInvalid commandInvalid commandInvalid commandInvalid commandInvalid commandInvalid commandInvalid commandInvalid commandInvalid commandInvalid commandInvalid commandInvalid commandInvalid
    _____________________________
    _____________________________
    This war, like the next war, is a war to end war.

  2. #2
    Registered User
    Join Date
    Jun 2002
    Posts
    82
    Well, its repeating because of this line:
    Code:
    while (scan != 'y' or 'n')
    You're taking the "or" of two expressions, "scan != 'y'" and "'n'". 'n' will always evaluate as true because it is nonzero. The correct way to write this expression is
    Code:
    while ((scan != 'y') && (scan != 'n'))
    Also, it will loop because you said or instead of and, cause scan will always be either not equal to 'y' or not equal to 'n'.

    Actually I'm surprised that your compiler let you say "or" instead of "||"; I thought it was just Perl that would let you do that.

    Oh, and please use "int main" instead of "void main", this is C++ not C.
    Claus Hetzer
    Compiler: Borland 5.5 (on Windows)
    Solaris CC (on Unix)
    Known Languages: C++, MATLAB, Perl, Java

  3. #3
    Linux Forever
    Join Date
    Oct 2002
    Posts
    373
    thanks, the int main( void ) is for the timer, as i can't get it to work if i delete it. my include files are for the void. i don't what i can use in place of it.
    This war, like the next war, is a war to end war.

  4. #4
    Linux Forever
    Join Date
    Oct 2002
    Posts
    373
    and it still doesn't work right. i still get infinite Invalid command.
    This war, like the next war, is a war to end war.

  5. #5
    Linux Forever
    Join Date
    Oct 2002
    Posts
    373
    im using dev c++ 4, with a patch which allows c, c++, ascii, perl and java.
    This war, like the next war, is a war to end war.

  6. #6
    Registered User
    Join Date
    Jun 2002
    Posts
    82
    Hmm...are you inputting a capital Y or N? If you are, they won't be equal to lowercase y or n. If you want to catch this you'll need to account for it in your while statement.
    Claus Hetzer
    Compiler: Borland 5.5 (on Windows)
    Solaris CC (on Unix)
    Known Languages: C++, MATLAB, Perl, Java

  7. #7
    Linux Forever
    Join Date
    Oct 2002
    Posts
    373
    now it's working. download it to see the remaining error... i need to input it twice. feel free to fix any bugs.
    Attached Files Attached Files
    This war, like the next war, is a war to end war.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File scan not working
    By samus250 in forum C Programming
    Replies: 3
    Last Post: 09-27-2008, 01:09 PM
  2. scan multiple items in multidimensional array
    By requiem in forum C Programming
    Replies: 1
    Last Post: 04-17-2003, 03:02 PM
  3. about scan 7-segment display via parallel port
    By mobdawg in forum C Programming
    Replies: 4
    Last Post: 09-11-2002, 06:11 AM
  4. Scan data from file
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 05-12-2002, 07:52 AM
  5. Replies: 0
    Last Post: 02-21-2002, 05:05 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21