Problem with input choice... Somethings wrong... really wrong....

This is a discussion on Problem with input choice... Somethings wrong... really wrong.... within the C++ Programming forums, part of the General Programming Boards category; My assignment is to program a room booking system that has different restrictions for different users. So far i'm done ...

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    5

    Problem with input choice... Somethings wrong... really wrong....

    My assignment is to program a room booking system that has different restrictions for different users. So far i'm done with the login pages when i noticed something wrong. The program works fine when i enter numbers but gets stuck when i enter a character. There's also supposed to be 3 text files containing the names of the users which i can't include here. I think you'll notice those . By the way, i'm very much a newbie to c++. I think you'll notice that too... Here's a sample of my source code.

    #include <iostream>
    #include <fstream>
    #include <stdlib.h>
    using namespace std;

    //-------------------------------------------------------------------------------------------------------------------------------
    // Function Prototypes and declarations
    //-------------------------------------------------------------------------------------------------------------------------------
    ifstream inputFromFile;
    ofstream outputToFile;

    void loginChoice();
    void administratorLogin();
    void memberChoice();
    void lecturerLogin();
    void studentLogin();
    void administratorMenu();
    void lecturerMenu();
    void studentMenu();

    //-------------------------------------------------------------------------------------------------------------------------------
    // Function to open and write a file
    //-------------------------------------------------------------------------------------------------------------------------------
    void openWriteFile(const string filename)
    {
    outputToFile.clear();
    outputToFile.open(filename.c_str(),ios::in);
    if (!outputToFile)
    cout << "There was an error opening the file" << endl;
    }

    //-------------------------------------------------------------------------------------------------------------------------------
    // Function to close a written file
    //-------------------------------------------------------------------------------------------------------------------------------
    void closeWriteFile()
    {
    outputToFile.close();
    }

    //-------------------------------------------------------------------------------------------------------------------------------
    // Function to open and read a file
    //-------------------------------------------------------------------------------------------------------------------------------
    int openReadFile(const string filename)
    {
    inputFromFile.clear();
    inputFromFile.open(filename.c_str(),ios::in);
    if (!inputFromFile)
    cout << "There was an error opening the file" << endl;
    }

    //-------------------------------------------------------------------------------------------------------------------------------
    // Function to close a read file
    //-------------------------------------------------------------------------------------------------------------------------------
    int closeReadFile()
    {
    inputFromFile.close();
    }

    //-------------------------------------------------------------------------------------------------------------------------------
    // The Main Function
    //-------------------------------------------------------------------------------------------------------------------------------
    int main(int argc, char *argv[])
    {
    loginChoice();
    return 0;
    }

    //-------------------------------------------------------------------------------------------------------------------------------
    // Login Choice
    //-------------------------------------------------------------------------------------------------------------------------------
    void loginChoice()
    {
    int choice;

    cout <<" ||------------------------------||"<<endl;
    cout <<" ||Multimedia Room Booking System||"<<endl;
    cout <<" ||------------------------------||"<<endl;
    cout <<" || Please choose: ||"<<endl;
    cout <<" || ||"<<endl;
    cout <<" || '1' for administrators ||"<<endl;
    cout <<" || '2' for members ||"<<endl;
    cout <<" || '3' to exit program ||"<<endl;
    cout <<" ||------------------------------||"<<endl<<endl<<endl;

    cout << "Your choice is: ";
    cin >> choice ;

    if (choice == 1)
    {
    system ("cls");
    administratorLogin();
    }
    else if (choice == 2)
    {
    system ("cls");
    memberChoice();
    }
    else if (choice == 3)
    {
    // will exit
    }
    else
    {
    cout <<endl<<"Please choose from the list above"<<endl;
    system ("PAUSE");
    system ("cls");
    loginChoice();
    }
    }

    //-------------------------------------------------------------------------------------------------------------------------------
    // Administrator Login
    //-------------------------------------------------------------------------------------------------------------------------------
    void administratorLogin()
    {
    int a, ID_number = 0;
    string password, b;

    cout<<" ID number : ";
    cin>>ID_number;
    cout<<" | "<<endl;
    cout<<" Password : ";
    cin>>password;
    system("cls");

    openReadFile("AdministratorPassword.txt");
    while(!inputFromFile.eof())
    {
    inputFromFile>>a>>b;
    if(!inputFromFile.fail())
    {
    if((a==ID_number)&&(b==password))
    {
    closeReadFile();
    administratorMenu();
    }
    }
    }
    if((a!=ID_number)||(b!=password))
    {
    cout<<"Your ID number or password is invalid "<<endl;
    closeReadFile();
    system ("PAUSE");
    system ("cls");
    loginChoice();
    }
    }

    //-------------------------------------------------------------------------------------------------------------------------------
    // Administrator Menu
    //-------------------------------------------------------------------------------------------------------------------------------
    void administratorMenu()
    {
    cout <<"there will be a menu here";
    system ("PAUSE");
    system ("cls");
    loginChoice();
    }


    //-------------------------------------------------------------------------------------------------------------------------------
    // Member Type Choice
    //-------------------------------------------------------------------------------------------------------------------------------
    void memberChoice()
    {
    int choice;

    cout <<" ||------------------------------||"<<endl;
    cout <<" || Members Login ||"<<endl;
    cout <<" ||------------------------------||"<<endl;
    cout <<" || Please press: ||"<<endl;
    cout <<" || ||"<<endl;
    cout <<" || '1' for lecturers ||"<<endl;
    cout <<" || '2' for students ||"<<endl;
    cout <<" || '3' back to first menu ||"<<endl;
    cout <<" || '4' to exit program ||"<<endl;
    cout <<" ||------------------------------||"<<endl<<endl<<endl;

    cout << "Your choice is: ";
    cin >> choice ;

    if (choice == 1)
    {
    system("cls");
    lecturerLogin();
    }
    else if (choice == 2)
    {
    system("cls");
    studentLogin();
    }
    else if (choice == 3)
    {
    system("cls");
    loginChoice();
    }
    else if (choice == 4)
    {
    //will exit
    }
    else
    {
    cout <<endl<<"Please choose from the list above"<<endl;
    system ("PAUSE");
    system ("cls");
    memberChoice();
    }
    }

    //-------------------------------------------------------------------------------------------------------------------------------
    // Lecturer Login
    //-------------------------------------------------------------------------------------------------------------------------------
    void lecturerLogin()
    {
    int a, ID_number = 0;
    string password, b;

    cout<<" ID number : ";
    cin>>ID_number;
    cout<<" | "<<endl;
    cout<<" Password : ";
    cin>>password;
    system("cls");

    openReadFile("LecturerPassword.txt");
    while(!inputFromFile.eof())
    {
    inputFromFile>>a>>b;
    if(!inputFromFile.fail())
    {
    if((a==ID_number)&&(b==password))
    {
    closeReadFile();
    lecturerMenu();
    }
    }
    }
    if((a!=ID_number)||(b!=password))
    {
    cout<<"Your ID number or password is invalid "<<endl;
    closeReadFile();
    system ("PAUSE");
    system ("cls");
    memberChoice();
    }
    }

    //-------------------------------------------------------------------------------------------------------------------------------
    // Lecturer Menu
    //-------------------------------------------------------------------------------------------------------------------------------
    void lecturerMenu()
    {
    cout <<"there will be a menu here";
    system ("PAUSE");
    system ("cls");
    memberChoice();
    }

    //-------------------------------------------------------------------------------------------------------------------------------
    // Student Login
    //-------------------------------------------------------------------------------------------------------------------------------
    void studentLogin()
    {
    int a, ID_number = 0;
    string password, b;

    cout<<" ID number : ";
    cin>>ID_number;
    cout<<" | "<<endl;
    cout<<" Password : ";
    cin>>password;
    system("cls");

    openReadFile("StudentPassword.txt");
    while(!inputFromFile.eof())
    {
    inputFromFile>>a>>b;
    if(!inputFromFile.fail())
    {
    if((a==ID_number)&&(b==password))
    {
    closeReadFile();
    studentMenu();
    }
    }
    }
    if((a!=ID_number)||(b!=password))
    {
    cout<<"Your ID number or password is invalid "<<endl;
    closeReadFile();
    system ("PAUSE");
    system ("cls");
    memberChoice();
    }
    }

    //-------------------------------------------------------------------------------------------------------------------------------
    // Student Menu
    //-------------------------------------------------------------------------------------------------------------------------------
    void studentMenu()
    {
    cout <<"there will be a menu here";
    system ("PAUSE");
    system ("cls");
    memberChoice();
    }











    Man, i dont even know how to use this message board properly.

  2. #2

  3. #3
    Registered User
    Join Date
    Jul 2004
    Posts
    5
    Sorry, here's the code with tags...

    Code:
    #include <iostream>
    #include <fstream>
    #include <stdlib.h>
    using namespace std;
    
    //-------------------------------------------------------------------------------------------------------------------------------
    // Function Prototypes and declarations
    //-------------------------------------------------------------------------------------------------------------------------------
    ifstream inputFromFile;
    ofstream outputToFile;
    
    void loginChoice();
    void administratorLogin();
    void memberChoice();
    void lecturerLogin();
    void studentLogin();
    void administratorMenu();
    void lecturerMenu();
    void studentMenu();
    
    //-------------------------------------------------------------------------------------------------------------------------------
    // Function to open and write a file
    //-------------------------------------------------------------------------------------------------------------------------------
    void openWriteFile(const string filename)
    {
      outputToFile.clear();  
      outputToFile.open(filename.c_str(),ios::in);
      if (!outputToFile) 
          cout << "There was an error opening the file" << endl;      
    }    
    
    //-------------------------------------------------------------------------------------------------------------------------------
    // Function to close a written file
    //-------------------------------------------------------------------------------------------------------------------------------
    void closeWriteFile()
    {
       outputToFile.close();
    }    
    
    //-------------------------------------------------------------------------------------------------------------------------------
    // Function to open and read a file
    //-------------------------------------------------------------------------------------------------------------------------------
    int openReadFile(const string filename)
    {
      inputFromFile.clear();  
      inputFromFile.open(filename.c_str(),ios::in);
      if (!inputFromFile) 
          cout << "There was an error opening the file" << endl;      
    }    
    
    //-------------------------------------------------------------------------------------------------------------------------------
    // Function to close a read file
    //-------------------------------------------------------------------------------------------------------------------------------
    int closeReadFile()
    {
       inputFromFile.close();
    }    
    
    //-------------------------------------------------------------------------------------------------------------------------------
    // The Main Function
    //-------------------------------------------------------------------------------------------------------------------------------
    int main(int argc, char *argv[])
    {
      loginChoice();
      return 0;
    }
    
    //-------------------------------------------------------------------------------------------------------------------------------
    // Login Choice
    //-------------------------------------------------------------------------------------------------------------------------------
    void loginChoice()
    {
       int choice; 
        
        cout <<" ||------------------------------||"<<endl;             
        cout <<" ||Multimedia Room Booking System||"<<endl;
        cout <<" ||------------------------------||"<<endl;
        cout <<" || Please choose:               ||"<<endl;
        cout <<" ||                              ||"<<endl;
        cout <<" || '1' for administrators       ||"<<endl;
        cout <<" || '2' for members              ||"<<endl;
        cout <<" || '3' to exit program          ||"<<endl;
        cout <<" ||------------------------------||"<<endl<<endl<<endl;
        
        cout <<  "Your choice is: ";
        cin >> choice ;
    
        if (choice == 1)
        {
            system ("cls");
            administratorLogin();
        }        
        else if (choice == 2)
        {
            system ("cls");
            memberChoice();        
        }        
        else if (choice == 3)
        {
            // will exit
        }        
        else 
        {
            cout <<endl<<"Please choose from the list above"<<endl;
            system ("PAUSE");
            system ("cls");
            loginChoice();
        }
    }
    
    //-------------------------------------------------------------------------------------------------------------------------------
    // Administrator Login
    //-------------------------------------------------------------------------------------------------------------------------------
    void administratorLogin()
    {
     int a, ID_number = 0;
     string password, b;
          
     cout<<"                ID number :  "; 
     cin>>ID_number;
     cout<<"                   |    "<<endl;
     cout<<"                Password  :   ";
     cin>>password;
     system("cls");
     
     openReadFile("AdministratorPassword.txt");
     while(!inputFromFile.eof())
     {
      inputFromFile>>a>>b;
      if(!inputFromFile.fail())
      {
       if((a==ID_number)&&(b==password))
       {
        closeReadFile();
        administratorMenu();
       }
      }
     }
     if((a!=ID_number)||(b!=password))
      {
      cout<<"Your ID number or password is invalid "<<endl;
      closeReadFile();
      system ("PAUSE");
      system ("cls");
      loginChoice();
      }
    }
    
    //-------------------------------------------------------------------------------------------------------------------------------
    // Administrator Menu
    //-------------------------------------------------------------------------------------------------------------------------------
    void administratorMenu()
    {
        cout <<"there will be a menu here";
        system ("PAUSE");
        system ("cls");
        loginChoice();
    }
    
    
    //-------------------------------------------------------------------------------------------------------------------------------
    // Member Type Choice
    //-------------------------------------------------------------------------------------------------------------------------------
    void memberChoice()
    {
       int choice;
         
        cout <<" ||------------------------------||"<<endl;
        cout <<" || Members Login                ||"<<endl;
        cout <<" ||------------------------------||"<<endl;
        cout <<" || Please press:                ||"<<endl;
        cout <<" ||                              ||"<<endl;
        cout <<" || '1' for lecturers            ||"<<endl;
        cout <<" || '2' for students             ||"<<endl;
        cout <<" || '3' back to first menu       ||"<<endl;
        cout <<" || '4' to exit program          ||"<<endl;
        cout <<" ||------------------------------||"<<endl<<endl<<endl;
        
        cout <<  "Your choice is: ";
        cin >> choice ;
    
        if (choice == 1)
        {
            system("cls");
            lecturerLogin();
        }
        else if (choice == 2)
        {
            system("cls");
            studentLogin();
        }
        else if (choice == 3)
        {
            system("cls");
            loginChoice();
        }
        else if (choice == 4)
        {
            //will exit
        }
        else
        {
            cout <<endl<<"Please choose from the list above"<<endl;
            system ("PAUSE");
            system ("cls");
            memberChoice();
        }
    }
    
    //-------------------------------------------------------------------------------------------------------------------------------
    // Lecturer Login
    //-------------------------------------------------------------------------------------------------------------------------------
    void lecturerLogin()
    {
     int a, ID_number = 0;
     string password, b;
          
     cout<<"                ID number :  "; 
     cin>>ID_number;
     cout<<"                   |    "<<endl;
     cout<<"                Password  :   ";
     cin>>password;
     system("cls");
     
     openReadFile("LecturerPassword.txt");
     while(!inputFromFile.eof())
     {
      inputFromFile>>a>>b;
      if(!inputFromFile.fail())
      {
       if((a==ID_number)&&(b==password))
       {
        closeReadFile();
        lecturerMenu();
       }
      }
     }
     if((a!=ID_number)||(b!=password))
      {
      cout<<"Your ID number or password is invalid "<<endl;
      closeReadFile();
      system ("PAUSE");
      system ("cls");
      memberChoice();
      }
    }
    
    //-------------------------------------------------------------------------------------------------------------------------------
    // Lecturer Menu
    //-------------------------------------------------------------------------------------------------------------------------------
    void lecturerMenu()
    {
        cout <<"there will be a menu here";
        system ("PAUSE");
        system ("cls");
        memberChoice();
    }
    
    //-------------------------------------------------------------------------------------------------------------------------------
    // Student Login
    //-------------------------------------------------------------------------------------------------------------------------------
    void studentLogin()
    {
     int a, ID_number = 0;
     string password, b;
          
     cout<<"                ID number :  "; 
     cin>>ID_number;
     cout<<"                   |    "<<endl;
     cout<<"                Password  :   ";
     cin>>password;
     system("cls");
     
     openReadFile("StudentPassword.txt");
     while(!inputFromFile.eof())
     {
      inputFromFile>>a>>b;
      if(!inputFromFile.fail())
      {
       if((a==ID_number)&&(b==password))
       {
        closeReadFile();
        studentMenu();
       }
      }
     }
     if((a!=ID_number)||(b!=password))
      {
      cout<<"Your ID number or password is invalid "<<endl;
      closeReadFile();
      system ("PAUSE");
      system ("cls");
      memberChoice();
      }
    }
    
    //-------------------------------------------------------------------------------------------------------------------------------
    // Student Menu
    //-------------------------------------------------------------------------------------------------------------------------------
    void studentMenu()
    {
        cout <<"there will be a menu here";
        system ("PAUSE");
        system ("cls");
        memberChoice();
    }
    ah, looks better...

  4. #4
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    You're unlikely to find anyone willing to read that whole length of code. This does however, sound like a firaly simple problem, and could propably be tracked down pretty easily if you gave us some more detail as to what is happening. When you enter a character, what exactly happens?

  5. #5
    Registered User
    Join Date
    Jul 2004
    Posts
    5

    program gets stuck

    Ok, if i enter numbers, as in integers, the program will work fine but for some reason when i enter alphabets or characters at the login screen for example, the program will just get stuck in some kind of loop. It says press any key to continue and just keeps displaying the main menu.. I just dont understand how to fix this.

  6. #6
    vae victus! skorman00's Avatar
    Join Date
    Nov 2003
    Posts
    594
    that will always cause bad things to happen, and it will also mess up if you were to input something like "4abc." There are several ways you can fix it, but here's a quick one:

    Code:
    int a;
    
    while(true)
    {
        cout << "enter the value of a:";
     
        if(cin >> a)    //this test will fail if you recieved input other than the type of a
        {
             while(cin.get() != '\n');    //after a was filled, this will eat up the rest of the line
                                          //until it finds \n, which is when the user pressed 'enter'
             break; //stop getting input
        }
        cin.clear();   //cin failed...this will clear that fail bit
        while(cin.get() != '\n');    // same reason as above
    }
    if you're on a linux system, I think pressing enter not only puts in a \n character, but also a \r, but don't quote me on that.

  7. #7
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Linux only uses \n. Only MS has \r\n for use in files and in window output.

  8. #8
    Registered User
    Join Date
    Jul 2004
    Posts
    5
    thanxx people, appreciate the help.

  9. #9
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    your problem is that you've set up your program to accept an int, and when it gets a char it keeps checking the stream for an int... problem is, it won't remove the char becuse it's not an integer, so everytime it checks the stream, it finds the same character... therefore you have an infinite loop... another way to solve it is to change what it's looking for to an char, and then tell the user you're looking for a int... then put in a check so that it only accepts 'integers' in the range you want... example:

    brokt
    Code:
    ...
    int main()
    {
         int choice;
         cout<<"Enter a Number between 1 and 5: ";     //'a' is entered
         cin>>choice;     //infinite loop
         if(choice<static_cast<char>(1+'0')  ||  choice>static_cast<char>(5+'0'))
              cout<<"That is not a Valid Choice";
    }
    fixt:
    Code:
    ...
    int main()
    {
         char choice;
         cout<<"Enter a Number between 1 and 5: ";     //'a' is entered
         cin>>choice;     //accepts ANY keyboard input
         if(choice<static_cast<char>(1+'0')  ||  choice>static_cast<char>(5+'0'))  //not what it wanted
              cout<<"That is not a Valid Choice";     //no infinite loop here - just a message and the program is under control
    }
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  10. #10
    Registered User
    Join Date
    Jul 2004
    Posts
    5
    ah, perfect! thanxx again!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can someone help me with these errors please code included
    By geekrockergal in forum C Programming
    Replies: 7
    Last Post: 02-10-2009, 01:20 PM
  2. Need some help with C program writing
    By The_PC_Gamer in forum C Programming
    Replies: 9
    Last Post: 02-12-2008, 08:12 PM
  3. problem : input and calculation working together
    By itay390 in forum C Programming
    Replies: 13
    Last Post: 07-30-2005, 12:32 PM
  4. Confusing problem with file input
    By Vorok in forum C++ Programming
    Replies: 3
    Last Post: 01-05-2003, 02:49 AM
  5. Help with my code...somethings Wrong..
    By Learning C. in forum C Programming
    Replies: 2
    Last Post: 03-19-2002, 08:29 AM

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