Hey, I was bored and had not made a program for a short while so decided to make one, Just have a few questions and some "How do you.." questions, well heres my code..

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    int registerID, ID;
    char name[20];
    char choice;
    
    ofstream file;
    
    file.open("identification.txt");
    
    cout << "Do you wish to create a new account?";
    cin >> choice;
    
    if( choice == 'y' || choice == 'Y' )
    {
        cout << "Please register a number I.D : ";
        cin >> registerID;
        
        cout << endl << "Thanks, now what is your name?";
        cin.getline( name, 20);
        
        file << registerID << name;
    }
    
    else if( choice != 'y' || choice != 'Y' )
    
    cout << "What is your I.D? : ";
    cin >> ID;
    
    cout << endl << "Welcome " << name;
    
    return 0;
    
    cin.get();
    cin.get();
}

Just i'm not sure how i would access the i.d's in the file and check if they actually exist, many thanks

Dani AI

Generated

A few focused points to bridge the gaps in the thread. The program currently writes only the numeric ID into the file and does not implement any lookup — already flagged the missing search step, and correctly hinted that input handling and file modes are causing trouble. For a small, text-based registry the cleanest approach is to store one record per line with a fixed delimiter (for example: ID,Name). That makes parsing trivial and avoids ambiguity when names contain spaces.

A safe, practical lookup workflow:

  • Write each account as a single text line (e.g., 101,John Doe). Do not use binary mode for simple text.
  • When checking an ID, open the file for reading and scan line-by-line. Split each line on the delimiter, convert the left token to an integer and compare.
  • Return the matching name when found, or report "not found" if the scan finishes.

Example search routine (illustrative — integrate into your program where needed):

#include <fstream>
#include <string>
#include <iostream>

bool find_by_id(const std::string& path, int target, std::string& out_name) {
    std::ifstream in(path);
    if (!in) return false;
    std::string line;
    while (std::getline(in, line)) {
        auto pos = line.find(',');
        if (pos == std::string::npos) continue;
        int id = 0;
        try { id = std::stoi(line.substr(0, pos)); } catch (...) { continue; }
        if (id == target) { out_name = line.substr(pos + 1); return true; }
    }
    return false;
}

Troubleshooting notes: always check file.is_open() and handle std::stoi exceptions or malformed lines. Be careful when mixing formatted extraction (>>) and line-oriented input — leftover newlines will give empty reads; consume them or use consistent APIs. If the dataset grows beyond a few thousand records, consider building an in-memory index (unordered_map) at startup or switching to a lightweight DB like SQLite to avoid repeated linear scans.

Recommended Answers

All 8 Replies

line 30: else if( choice != 'y' || choice != 'Y' ) . That line will always return ' true'. Name me one input that won't :)
Also you need to put line 31-36 in curlybrackets ( {} ).
You should change it to :

if( choice == 'y' || choice == 'Y' )
{
  //do stuff
}
else if( choice == 'n' || choice == 'N' )
{
   //do other stuff
}
else 
{
   //invalid input
}

To answer your question, we need to know what your inputfile looks like.

[edit]
lines 37-40:

return 0;

cin.get();
cin.get();

Those cin,get() 's won't do anything if you return 0 before them. Your compiler should have warned you that there was unreachable code...
Change it to:

cin.ignore();
cin.get()
return 0;

That way the console will stay open before exiting the program

Thanks, The file is just a normal notepad .txt file, I modded the code a bit, and searched google but nothing really about how to access the date i want from the file..

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    int registerID, ID;
    char name[20];
    char choice;
    
    ofstream file;
    
    file.open("identification.txt", ios::out | ios::app | ios::binary);
    
    cout << "Do you wish to create a new account?";
    cin >> choice;
    
    if( choice == 'y' || choice == 'Y' )
    {
        cout << "Please register a number I.D : ";
        cin >> registerID;
        
        cout << endl << "Thanks, now what is your name?";
        cin.getline( name, 20);
        
        if (file.is_open())
        {
                             file << registerID << name;
        }
    }
    
    else if( choice == 'n' || choice == 'N' )
    {
         cout << "What is your I.D? : ";
         cin >> ID;
         
         cout << endl << "Welcome " << name;
    }
    
    else
        cout << "Invalid selection";
    
    return 0;
    
    cin.get();
    cin.get();
}

- Change line 14 back the way it was.
- change the other things I mentioned in my previous post (I editted it in the time you reposted (esp line 33, I made a typo) )
- Please post one or two lines from the txt file so we can see have the data is stored in it

I've changed code again, and I've just found a problem with the file, i entered 101 as I.D and name as name and the file just has "10" any ideas?

EDIT: Actually it saves I.D just not name?

Yeah. You made it binary. So please read my previous posts and change all those things. And pretty please, with sugar on top, post a few lines from your textfile. (where you store the userid's and names) :)

The codes :

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    int ID;
    char name[20];
    char choice;
    
    ofstream file;
    
    file.open("identification.txt");
    
    cout << "Do you wish to create a new account?";
    cin >> choice;
    
    if( choice == 'y' || choice == 'Y' )
    {
        cout << "Please register a number I.D : ";
        cin >> ID;
        
        cout << endl << "Thanks, now what is your name?";
        cin.getline( name, 20);
        
        if (file.is_open())
        {
                             file << ID << " " << name;
        }
    }
    
    else if( choice == 'n' || choice == 'N' )
    {
         cout << "What is your I.D? : ";
         cin >> ID;
         
         cout << endl << "Welcome " << name;
    }
    
    else
        cout << "Invalid selection";
    
    cin.ignore();
    cin.get();
    
    return 0;
}

And the only data in the text file is "123"

I guess you entered '123' as ID? What did you enter as name?

Also when using c++, I would recommend using std::strings instead of char[].

#include <string>
[...]
string name;
[....]
getline(cin, name);
[....]
file << name;

Correct me if i am wrong . But after entering the ID isnt it necessary for the program to find the matching case for the user input?

I mean there isnt any function in the above program which is searching for a particular id.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.