0

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

3
Contributors
8
Replies
9
Views
8 Years
Discussion Span
Last Post by Sky Diploma
0

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

0

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();
}
0

- 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

0

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?

0

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) :)

0

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"

0

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;
0

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.

This article has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.