how to close the program after 3 tries
Apr 23, 2014 at 7:13pm UTC
I am new to programming with c++ and for this current project I need help
I need to create a loop that will close the project after 3 tries
so basically if it doesn't find a match after three tries it needs to shut down but the problem is i don't even know where to start
your help is greatly appreciated
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
// the struct keyword defines a structure type
// it is a way of storing many different values
// in variables of potentially different types
// under the same name
struct RCRD
{
string userID;
string Password;
int pin;
}; // remember ';' here
const int MX = 50;
typedef RCRD ARR[MX];
void Greeting();
void PrintInfo(const ARR ListIN, const int SIZE);
int GetInfo(ARR ListIN);
bool FindInfo(const RCRD Target, const ARR ListIN, const int SIZE);
bool TryAgain(void );
int main()
{
Greeting();
RCRD OneUser;
ARR List;// array of 50 'Record's
int NumRecords;
bool search;
PrintInfo(&OneUser, 1);
NumRecords = GetInfo(List);
if (NumRecords != -1)
{
cout << "Number of Records: "
<< NumRecords << endl << endl;
PrintInfo(List, NumRecords);
cout << "Enter the userId:" << endl;
cin >> OneUser.userID ;
cout << "Enter the Password:" << endl;
cin >> OneUser.Password ;
cout << "Enter the pin" << endl;
cin >> OneUser.pin ;
search = FindInfo(OneUser, List, NumRecords);
if (search)
cout << "Record Found" << endl;
else
cout << "No Record Found " << endl;
cout << "Try Again? " << endl;
}
return 0;
}
void Greeting()
{
// Displays the login Menu Welcome Message
cout << "\t\tWelcome to MyGreatWebSite.com\n\n"
<< "*=====================================*\n"
<< " Please Enter the Following\n"
<< endl
<< " userID: NO BLANKS PERMITTED\n"
<< endl
<< " password: NO BLANKS PERMITTED\n"
<< endl
<< " PIN: 4 DIGITS\n"
<< "*=====================================*\n" ;
}
void PrintInfo(const ARR ListIN, const int SIZE)
{
cout << left;
cout << setw(15) << "userID" ;
cout << setw(15) << "password" ;
cout << setw(5) << "pin" << endl;
for (int i = 0; i < SIZE; i++)
{
cout << setw(15) << ListIN[i].userID
<< setw(15) << ListIN[i].Password
<< setw(5) << ListIN[i].pin << endl;
}
cout << endl;
cout << right;
}
int GetInfo(ARR ListIN)
{
int counter;
int NumOfItems;
ifstream inFile;
string FileName;
FileName = "users.txt." ;
inFile.open(FileName.c_str());
counter = 0;
if (inFile)
{
inFile >> NumOfItems;
while (!inFile.eof() && counter < MX)
{
inFile >> ListIN[counter].userID
>> ListIN[counter].Password
>> ListIN[counter].pin;
counter++;
}
inFile.clear();
inFile.close();
}
else
{
cout << "Cannot Open the file" << endl;
counter = -1;
}
return counter;
}
bool FindInfo(const RCRD Target, const ARR ListIN, const int SIZE)
{
int i;
bool Match;
Match = false ;
i = 0;
while (i < SIZE && !Match)
{
if (ListIN[i].userID == Target.userID &&
ListIN[i].Password == Target.Password &&
ListIN[i].pin == Target.pin)
Match = true ;
else
i++;
}
return Match;
}
Apr 23, 2014 at 7:21pm UTC
You already know how to use while loop, initialize a variable say tries to number_of_tries (in this case 3) then run a while loop while tries is greater than zero. Put the lines from 50 to 64 in this while.
Then modify the else block (at lines 62-64) add code to reduce the number of tries here.
Apr 23, 2014 at 7:22pm UTC
Lines 50-64 you need a loop so that the user can be prompted multiple times. If you find the record you need to exit the loop. If you don't find the record, bump a counter and exit the loop when the counter gets to 3.
Topic archived. No new replies allowed.