Comparing numbers to a list of numbers held in a text file
I don't think I need to go into detail about the program I'm trying to make, but just ask if you need to know more about.
-In a certain part of my program, the user is asked to enter a three-digit number.
-The number that that user enters is then stored in a file called "used_codes.log".
-The three-digit number that the user enters is given to them in advance and corresponds to an algorithm with another three-digit number.
-I created another program to generate these two corresponding numbers so I don't have to do the math every time.
-The generator saves the numbers to a file called "issued_codes.log"
-What I want to be able to do, is make the generator, before printing a number, to check "issued_codes.log" and never give the same number twice.
-I also want my program to compare the number submitted by the user to the codes in "used_codes.log", and not accept any codes that have been used before
I was thinking of a code somewhere along the lines of this:
Code:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int maint()
{
promptA:
int usrCode;
string usedCodes;
cout << "Please enter your code: " << endl;
cin >> usrCode;
ifstream used_codes ("used_codes.log");
while (! used_codes.eof())
{
getline (used_codes, usedCodes);
}
used_codes.close();
if (usrCode != "???") // This is the problem, I don't know how to tell it that I don't want it to equal anything in the file.
//Also, I don't know how to make C++ recognize the individual numbers;
//I've only ever retrieved information from a file as a single string variable.
//I want to be able to retrieve the individual numbers as int */ )
{
...
}
else
{
cerr << "You have entered an incorrect code. Please try again." << endl;
goto promptA;
}
............................
............................
............................
system("PAUSE");
return 0;
}
BTW, I've tried to convert string data types to int, float, double, and long data types before, but my program shows me a ☺, instead of what I had intended.