Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,110 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,728 people online right now. Registration is fast and FREE... Join Now!




String comparison with .txt file

 
Reply to this topicStart new topic

String comparison with .txt file

cosette_hinds
12 May, 2007 - 12:36 AM
Post #1

New D.I.C Head
*

Joined: 30 Dec, 2006
Posts: 22


My Contributions
Hi. I am having a bit of trouble understanding an assignment and was looking for some ideas either on where to look for information or how best to be thinking about the following problem.

At the beginning of the assignment we are to ask for a security code which is to be four numeric characters.
We need to complete error checking on the input to ensure that the number of characters is correct, and also that they are numeric. Once this is complete we need to compare the string with the "id" string in a text file (that has been provided for us).

The text file is in the format:
1234 a Fred
2345 c James
and so forth - where the first four characters are the id, the char is a status level (a for administrator) and the final part is the name of the person.

The code that I have worked on for this part of the assignment is below:

CODE


#include <iostream>
#include <fstream>
#include <string>
#include <iomanip> [color=#006600]// for a different part of the assignment[/color]

using namespace std;

struct userInfo
{
    string id;
    char adminLevel;
    string name;
};

const int MAX_CODE_LENGTH = 4;

[color=#006600]// int displayMenu(string name); - for later in assignment[/color]
int main()
{
    string securityCode;
    string fileCode;
    ifstream fin;
    bool errorFlag = false;
    bool codeFound = true;
    userInfo user;
    int menuChoice; [color=#006600]// later in assignment[/color]

    [color=#006600]// assignment specified welcome goes here[/color]
    do
    {
        cout << "Please enter your security code: ";
        cin >> securityCode;

        if (securityCode.length() != MAX_CODE_LENGTH)
        {
            cout << "Invalid length security code." << endl;
        }
        else
        {
            for (int i = 0; i < MAX_CODE_LENGTH && errorFlag == false; i++)
            {
                if (isdigit(securityCode[i]) == 0)
                {
                    cout << "Invalid character in security code." << endl;
                    errorFlag = true;
                }
            }
[color=#CC33CC]// this is where I'm not really sure. . .[/color]

            if (!errorFlag)
            {
                //open security.txt
                fin.open("security.txt");

                if (!fin.is_open())
                {
                    cout << "Error opening file!";
                    system("pause");
                    exit(1);
                }
    
                do
                {
                    fin >> fileCode;

                    if (fileCode != securityCode)
                    {
                        codeFound = false;
                    }
                } while (!fin.eof() && codeFound == false);
                    
                // clear error-state flags for the file stream (eg. eof()
                fin.clear();
                
                // move file pointer to beginning of file
                fin.seekg(0, ios::beg);

                if (!codeFound)
                {
                    cout << "Security code not found!" << endl;
                }
                else
                {
                    fin >> user.id;
                    fin.ignore(1);
                    fin >> user.adminLevel;
                    fin.ignore(1);
                    fin >> user.name;

                    cout << endl << endl;
                }
            }
        }
        
        // clear error-state flags for the file stream (eg. eof())
        fin.clear();
                
        // move file pointer to beginning of file
        fin.seekg(0, ios::beg);

        fin.close();

    } while (codeFound == false || errorFlag);


The problem is in trying to read only the first four characters of a line in the .txt file.

I also have to include error-checking in the above but don't want to try anything with that until I have managed to make the first part work.

Thanks to anyone who can offer guidance.
User is offlineProfile CardPM
+Quote Post

Amadeus
RE: String Comparison With .txt File
12 May, 2007 - 11:00 AM
Post #2

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,226



Thanked: 37 times
Dream Kudos: 25
My Contributions
Since the file itself is comprised of lines of information separated by spaces, you have a few options. First and foremost, you can read a, entire line from the file using the getline() function, then simply separate that line into components using the space as a delimiter. Even easier would be to simp,y read the first item of the line into a variable using the cin (or fin) >>operator. As it will stop taking input when it encounters whitespace, something like
CODE

fin>>myvar;

will take the first item on the line only.
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 09:37PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month