C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 07-14-2004, 08:49 PM   #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.
greenferoz is offline   Reply With Quote
Old 07-14-2004, 08:51 PM   #2
& the hat of GPL slaying
 
Thantos's Avatar
 
Join Date: Sep 2001
Posts: 5,732
http://cboard.cprogramming.com/annou...ouncementid=51
<< !! Posting Code? Read this First !! >>
Thantos is offline   Reply With Quote
Old 07-14-2004, 09:00 PM   #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...
greenferoz is offline   Reply With Quote
Old 07-14-2004, 09:42 PM   #4
Super Moderator
 
Join Date: Sep 2001
Posts: 4,746
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?
sean is offline   Reply With Quote
Old 07-14-2004, 10:10 PM   #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.
greenferoz is offline   Reply With Quote
Old 07-14-2004, 11:04 PM   #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.
skorman00 is offline   Reply With Quote
Old 07-14-2004, 11:08 PM   #7
& the hat of GPL slaying
 
Thantos's Avatar
 
Join Date: Sep 2001
Posts: 5,732
Linux only uses \n. Only MS has \r\n for use in files and in window output.
Thantos is offline   Reply With Quote
Old 07-14-2004, 11:38 PM   #8
Registered User
 
Join Date: Jul 2004
Posts: 5
thanxx people, appreciate the help.
greenferoz is offline   Reply With Quote
Old 07-15-2004, 08:45 AM   #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
major_small is offline   Reply With Quote
Old 07-15-2004, 03:30 PM   #10
Registered User
 
Join Date: Jul 2004
Posts: 5
ah, perfect! thanxx again!
greenferoz is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 06:43 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

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