Hello,
I coded a project for class on my openSuse machine. It worked flawless. When I turned it in to my teacher, she compiled it on Visual Studio and it gave her this error:
Run-time Check Failure #3 - The variable userinput is being used without being initialized.
Needless to say I got a zero on a project that worked flawlessly on my box. Is anyone able to spot this said problem with 'userinput' or is there some sort of difference between the linux compiler and the windows compiler that would make this happen? I would really appreciate any feedback. I will paste the entire project in.
Code:
# include <iostream>
# include <limits> // INCLUDED FOR THE GETINT FUNCTION
using namespace std;
typedef int* IntArPtr;
int labamount = 4; // Defines the amount of labs, globally set to 4.
int pressenter() //I PUT THIS BABY IN THERE SO I DON'T HAVE TO TYPE THIS EVERYTIME I WANT THE USER TO PRESS ENTER.
{
cin.ignore(256, '\n');
cout << endl << "Press enter to continue...";
cin.get();
return 0;
}
int getInt() // THIS IS NOT MY FUNCTION BUT I INCORPORATED IT TO SANITIZE INTEGER INPUT. OTHERWISE, IF THE USER
//ENTERS CHARACTERS TO AN INT CIN IT CAUSES SERIOUS PROBLEMS.
// It isn't necessary for the program but the fact that entering a letter caused the program to start
//looping annoyed me enough to find a solution to it.
{
int x = 0;
while(!(cin >> x))
{
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(),'\n');
cout << "Invalid input..." << endl
<< "It must be in this format: <ID> <LAB> <STATION>." << endl
<< "(e.g. 12345 3 4 logs user 12345 input Lab 3, Station 4.)" << endl << ": ";;
}
return x;
}
int login (IntArPtr db_input[],int labsize_input[]) //Login function!
{
int countcheck = 0,idcount;
int id,lab,station;
cout << "Enter your User id, lab, and station to login to." << endl
<< "It must be in this format: <ID> <LAB> <STATION>." << endl
<< "(e.g. 12345 3 4 logs user 12345 input Lab 3, Station 4.)" << endl << ": ";
id = getInt();
lab = getInt();
station = getInt();
idcount = id;
while( idcount > 0) //This device divides the user id by 10 until there is no number divisible by 10 left.
// It then makes sure it was able to count 5 times so that the user id is 5 digits.
{
countcheck++;
idcount = idcount/10;
}
if ((countcheck != 5) || (id < 0))
{
cout << "Your ID must be five numbers long!!!" << endl;
pressenter();
}
else if ((lab > labamount) || (lab < 1))
{
cout << "No such lab!" << endl;
pressenter();
}
else if ((station > labsize_input[lab - 1]) || (station < 1))
{
cout << "Lab " << lab << " does not have a station " << station << "!" << endl;
pressenter();
}
else if (db_input[lab - 1][station - 1] != 0)
{
cout << "That station is currently in use!" << endl;
pressenter();
}
else
{
db_input[lab - 1][station - 1] = id;
cout <<"You have successfully logged in to Lab " << lab << ", station "
<< station << " as user " << id << "!";
pressenter();
}
return 0;
}
int logoff (IntArPtr db_input[],int labsize_input[]) // Logoff function!
{
int id,lab,station;
cout << "Enter your User id, lab, and station to logout of." << endl
<< "It must be in this format: <ID> <LAB> <STATION>." << endl
<< "(e.g. 12345 3 4 logs user 12345 input Lab 3, Station 4.)" << endl << ": ";
id = getInt();
lab = getInt();
station = getInt();
if ((lab > labamount) || (lab < 1))
{
cout << "No such lab!" << endl;
pressenter();
}
else if ((station > labsize_input[lab - 1]) || (station < 1))
{
cout << "Lab " << lab << " does not have a station " << station << "!" << endl;
pressenter();
}
else if (db_input[lab - 1][station - 1] != id)
{
cout << "There is no such user currently logged on to that workstation.";
pressenter();
}
else if (db_input[lab - 1][station - 1] == id)
{
cout << "User " << id << " successfully logged out of Lab " << lab << " Station " << station << ".";
db_input[lab - 1][station - 1] = 0;
pressenter();
}
return 0;
}
int displaylabs (IntArPtr db_input[],int labsize_input[]) // Display all workstations function!
{
cout << "Lab Number Computer Stations" << endl;
for (int index1 = 0;index1 < labamount;index1++)
{
cout << index1 + 1 << " ";
for (int index2 = 0;index2 < labsize_input[index1];index2++)
{
cout << index2 + 1 << ": ";
if (db_input[index1][index2] == 0)
{
cout << "empty ";
}
else
{
cout << db_input[index1][index2] << " ";
}
}
cout << endl;
}
pressenter();
return 0;
}
int search (IntArPtr db_input[],int labsize_input[]) //Function to search for a user!
{
int id;
bool searchresult = 0;
cout << "Please enter a user id:";
id = getInt();
for (int index1 = 0;index1 < labamount;index1++)
{
for (int index2 = 0;index2 < labsize_input[index1];index2++)
{
if (db_input[index1][index2] == id)
{
cout << "User: " << id <<" is currently logged into Lab " << index1 + 1
<< " Station " << index2 + 1 << "." << endl;
searchresult = 1;
}
}
}
if (searchresult == 0)
{
cout << "No such user id is presently logged in to any workstation.";
}
pressenter();
return 0;
}
int menu (IntArPtr db_input[],int labsize_input[]) // Main menu which calls the rest of the functions.
{
char userinput;
while ((userinput != 'q') && (userinput != 'Q'))
{
userinput = '0';
cout << "Welcome to Lab X network." << endl << "Please choose an option:" << endl;
cout << "L - Login" << endl;
cout << "X - Logoff" << endl;
cout << "D - Display status of all workstations" << endl;
cout << "S - Search for a user logged in" << endl;
cout << "Q - quit" << endl << "admin@labx: ";
cin >> userinput;
if ((userinput == 'l') || (userinput == 'L'))
{
login(db_input,labsize_input);
}
else if ((userinput == 'x') || (userinput == 'X'))
{
logoff(db_input,labsize_input);
}
else if ((userinput == 'd') || (userinput == 'D'))
{
displaylabs(db_input,labsize_input);
}
else if ((userinput == 's') || (userinput == 'S'))
{
search(db_input,labsize_input);
}
}
for (int index1 = 0;index1 < labamount;index1++)
{
delete[] db_input[index1]; //Kill each array within the db.
}
delete[] db_input; // Kill the db.
return 0;
}
int main ()
{
int labsize [] = {5,6,4,3}; //Defines the size of each lab here
IntArPtr *db = new IntArPtr[labamount]; //Create the lab array!
for (int index1 = 0;index1 < labamount;index1++)
{
db[index1] = new int[labsize[index1]]; //Create a dynamic array for each of the four labs!
for (int index2 = 0;index2 < labsize[index1];index2++)
{
db[index1][index2] = 0; //initializes each workstation to 0 which is understood by my program
// to mean empty.
}
}
menu(db, labsize); //starts the menu, which controls when to run the functions for each menu option.
return 0;
}
Thank you so much!
David Taylor