Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7782017
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T19:24:30+00:00 2026-06-01T19:24:30+00:00

I am creating a somewhat weak/vague database (My experience is very little, and please

  • 0

I am creating a somewhat weak/vague database (My experience is very little, and please forgive the mess of my code). For this, I create a check everytime my console program starts that checks whether a database (copied to userlist.txt) is created already, if not a new will be created, if the database exists, however, it should all be copied to a ‘vector users’ (Which is a struct) I have within the class ‘userbase’ that will then contain all user information.

My userstats struct looks like this,

enum securityLevel {user, moderator, admin};
struct userstats
{
    string ID;

    string name;
    string password;
    securityLevel secLev;                         
};

I will contain all this information from a textfile in this code,

    int main()
    {
        Userbase userbase;  // Class to contain userinformation during runtime.

        ifstream inFile;
        inFile.open("userlist.txt");


        if(inFile.good())
        {
            // ADD DATE OF MODIFICATION
            cout << "USERLIST FOUND, READING USERS.\n";
            userstats tempBuffer;
            int userCount = -1;
            int overCount = 0;

            while(!inFile.eof())
            {
                string buffer;

                getline(inFile, buffer);
                if (buffer == "ID:")
                {
                    userCount++;

                    if (userCount > overCount)
                    {
                        userbase.users.push_back(tempBuffer);
                        overCount++;

                    }
                    tempBuffer.ID = buffer;
                    cout << "ID";           // Just to see if works

                }

                else if (buffer == "name:")
                {
                    cout << "name";         // Just to see if works
                    tempBuffer.name = buffer;

                }
                else if (buffer == "password:")
                {
                    cout << "password";     // Just to see if works
                    tempBuffer.password = buffer;
                }



            }
            if (userCount == 0)
            {
                userbase.users.push_back(tempBuffer);
            }

            inFile.close();
        }
...

What I try to do is to read and analyze every line of the text file. An example of the userlist.txt could be,

created: Sun Apr 15 22:19:44 2012

mod_date: Sun Apr 15 22:19:44 2012


ID:1d
name:admin
password:Admin1
security level:2

(I am aware I do not read “security level” into the program yet)

EDIT: There could also be more users simply following the “security level:x”-line of the preceding user in the list.

Now, if the program reads the line “ID:1d” it should then copy this into the struct and finally I will put it all into the vector userbase.users[i]. This does not seem to work, however. It does not seem to catch on to any of the if-statements. I’ve gotten this sort of program to work before, so I am very confused what I am doing wrong. I could really use some help with this. Any other kind of criticism of the code is very welcome.

Regards,
Mikkel

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T19:24:31+00:00Added an answer on June 1, 2026 at 7:24 pm

    None of the if (buffer == ...) will ever be true as the lines always contain the value of the attribute contained on each line as well as the type of the attribute. For example:

    ID:1d

    when getline() reads this buffer will contain ID:1d so:

    if (buffer == "ID:")
    

    will be false. Use string.find() instead:

    if (0 == buffer.find("ID:")) // Comparing to zero ensures that the line
    {                            // starts with "ID:".
        // Avoid including the attribute type
        // in the value.
        tempBuffer.ID.assign(buffer.begin() + 3, buffer.end());
    }
    

    As commented by jrok, the while for reading the file is incorrect as no check is made immediately after getline(). Change to:

    string buffer;
    while(getline(inFile, buffer))
    {
        ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This relates somewhat to: InvalidOperationException while creating wcf web service instance I have a
This is somewhat a continuation of this post I've taken the following code and
This is somewhat a two-part question (please let me know if they should be
I'm somewhat new to MVC3, so I have a few questions... I am creating
(creating a separate question after comments on this: Javascript redeclared global variable overrides old
My problem involves creating a control somewhat analogous to the apple magnifying glass in
I know that when creating buttons, like next and previous, that the code can
Sorry for the somewhat confusing title. Not sure really how to title this. My
So I've run into a somewhat maddening bug (perhaps) with Visual Studio database projects,
I'm creating a game in which I have a somewhat complex method for creating

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.