Reputation: 57
Since I made the function void logoff_check()
the program crashes with the first (correct) input with no errors when compiling. I don't see anything wrong.
I know I'm not explaining well. If you need more details of the problem just comment and I will explain the best that I can. I'm new in programming and I'm studying computer science by the way.
This is the function:
void logoff_check(string username, string lab1[], string lab2[], string lab3[], string lab4[], bool& logoff)
{
for (int i = 0; i < 6; i++)
{
if (lab1[i] == (username + " ") && i < 5){
lab1[i] == "";
cout << "\n\n\t\t\tLog Off Successful.\n\n";
logoff = true;
break;
}
else if (lab2[i] == (username + " ")){
lab2[i] == "";
cout << "\n\n\t\t\tLog Off Successful.\n\n";
logoff = true;
break;
}
else if (lab3[i] == (username + " ") && i < 4){
lab3[i] == "";
logoff = true;
cout << "\n\n\t\t\tLog Off Successful.\n\n";
break;
}
else if (lab4[i] == (username + " ") && i < 3){
lab4[i] == "";
cout << "\n\n\t\t\tLog Off Successful.\n\n";
logoff = true;
break;
}
}
}
Here's the rest of the code:
#include <iostream>
#include <cctype>
#include <cstring>
#include <string>
#include <algorithm>
using namespace std;
void input(string& username, bool& userDigit);
void admin(string[], string[], string[], string[]);
void user(string username, string[], string[], string[], string[]);
void screen(string[], string[], string[], string[]);
void search(string ID, string [], string [], string [], string [], bool& notfound);
void logoff_check(string username, string [], string [], string [], string [], bool& logoff);
int main()
{
string username("");
const string Admin_key("ADMIN");
string lab1[5] = { "", "", "", "", "" };
string lab2[6] = { "", "", "", "", "", "" };
string lab3[4] = { "", "", "", "" };
string lab4[3] = { "", "", "" };
char ans;
bool digitID = true,
end = false,
inputbool = false,
logoff = false;
do
{
cout << endl;
input(username, digitID);
logoff_check(username, lab1, lab2, lab3, lab4, logoff);
if (Admin_key == username && !logoff)
admin(lab1, lab2, lab3, lab4);
else if (digitID && !logoff)
user(username, lab1, lab2, lab3, lab4);
else if (!logoff)
cout << "\nBad input data!\n";
cout << "\n\nTo continue hit Enter or press Q to exit\n";
cin.ignore();
cin.get(ans);
if (ans == 'q' || ans == 'Q') //alternate end because the program never ends with this condition
end = true; //in the do-while loop. I dont know why.
} while (!end);
return 0;
}
void input(string& username, bool& userDigit)
{
//Function Body
}
void admin(string lab1[], string lab2[], string lab3[], string lab4[])
{
//Function Body
}
void user(string username, string lab1[], string lab2[], string lab3[], string lab4[])
{
//Function Body
}
void screen(string lab1[], string lab2[], string lab3[], string lab4[])
{
//Function Body
}
void search(string ID, string lab1[], string lab2[], string lab3[], string lab4[],bool& notfound)
{
//Function Body
}
void logoff_check(string username, string [], string [], string [], string [], bool& logoff)
{
//Function is above
}
Upvotes: 0
Views: 62
Reputation: 17956
You're going past the end of your arrays, because you have your guards backward:
if (lab1[i] == (username + " ") && i < 5){
should be:
if (i < 5 && lab1[i] == (username + " ")){
The &&
operator performs short-circuit evaluation, and you do definitely need it here, because each of your arrays is a different length.
Putting the test after you've already tried to index the array isn't terribly useful.
Upvotes: 2