Comparing numbers to a list of numbers held in a text file

This is a discussion on Comparing numbers to a list of numbers held in a text file within the C++ Programming forums, part of the General Programming Boards category; I don't think I need to go into detail about the program I'm trying to make, but just ask if ...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    31

    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.

  2. #2
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,581
    Since used_codes is an std::string, you only need to convert usrCode to a string and use the find() string member function...

    The conversion happens with string streams:

    Code:
    #include <sstream>
    
    stringstream str_usrCode;
    str_usrCode << usrCode;
    Next you simply see if the code exists in the buffer...
    Code:
    if( used_codes.find(str_usrCode.str(), 0) != std::string::npos ) {
       // found!
    } else {
       // not found.
    }
    However, the whole convertion business is not needed if you simply declare usrCode as a string. After all, you are not using it as an int. Then it's just a matter of:

    Code:
    if( used_codes.find(usrCode, 0) != std::string::npos ) {
     // same as before...
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    31
    Thanks alot! But now I have another question: how would I go about retrieving data from a file as an int or a double variable? Is it similar to converting int to string?

  4. #4
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,581
    There are other ways, but I find stringstreams to be the most flexible... and easier to type.

    Imagine you have a file "test.txt" with the following contents:
    "jmajeremy 34 6754 7.3 user"

    You want the contents to map to "string int long double string".

    Code:
    #include <sstream>
    #include <fstream>
    #include <string>
    #include <iostream>
    
    int main()
    {
    
        std::ifstream ifile("test.txt");
        if (!ifile) {
            std::cerr << "Couldn't open file!";
        } else {
            std::string buff;
            getline(ifile, buff); // read first line an place it in string
            ifile.close();  // close file
    
            //create storage
            std::string name;
            int age;
            long money;
            double value;
            std::string type;
    
            std::istringstream iss(buff); // create input sstream from string
    
            iss >> name >> age >> money >> value >> type; // store into variables
    
            // and to prove it...
            std::cout << name << "\n";
            std::cout << age << "\n";
            std::cout << money << "\n";
            std::cout << value << "\n";
            std::cout << type;
        }
    
        return 0;
    }
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 11:10 AM
  2. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 01:36 PM
  3. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 02:27 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 11:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21