![]() |
| |||||||
![]() |
| | LinkBack | Thread Tools | Display Modes |
| | #1 |
| Registered User Join Date: Jul 2004
Posts: 5
| Problem with input choice... Somethings wrong... really wrong.... 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 | |
| | #2 |
| & the hat of GPL slaying Join Date: Sep 2001
Posts: 5,730
| |
| Thantos is offline | |
| | #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();
}
|
| greenferoz is offline | |
| | #4 |
| Super Moderator Join Date: Sep 2001
Posts: 4,680
| 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 | |
| | #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 | |
| | #6 |
| vae victus! 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
}
|
| skorman00 is offline | |
| | #7 |
| & the hat of GPL slaying Join Date: Sep 2001
Posts: 5,730
| Linux only uses \n. Only MS has \r\n for use in files and in window output. |
| Thantos is offline | |
| | #8 |
| Registered User Join Date: Jul 2004
Posts: 5
| thanxx people, appreciate the help. |
| greenferoz is offline | |
| | #9 |
| Registered User 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";
}
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 | |
| | #10 |
| Registered User Join Date: Jul 2004
Posts: 5
| ah, perfect! thanxx again! |
| greenferoz is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
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 |