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

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.