How do i make a code so that i can open a file if the person that is using it types 1 for yes or 2 for no

Heres my code,

#include <iostream>
#include <fstream>
using namespace std;

int main(){
    // for logging in
    int Pass, Name;
    Pass = 159876;
    Name = 159874;
    int PassIn;
    int NameIn;
    // for opening the password and username files
    int a, b;
    a = 1;
    b = 1;
    int d;
    int e;

    
    cout << "Please Enter your Username:";
    cin >> NameIn;
    
    cout << "Please Enter a Password:";
    cin >> PassIn;
    
    if (NameIn != Name ||  PassIn != Pass){
          cout << "Incorrect Username/Password Combintation \n";
          }
    if (NameIn == Name && PassIn == Pass){
          cout << "What would you like to do: (1 for Yes/ 2 for No) \n";
          cout << "a) Access the password list \n";
          cin >> d;
            if (d == a){
                fstream passw;
                passw.open ("password.txt");
                }
          cout << "b) Access the username list \n";
          cin >> e;
                  if (e == b){
                fstream usern;
                usern.open ("username.txt");
                }    
          cout << "c) Exit \n";
                  
      
              
          }
          system("pause");
          return 0;
}

move lines 34 and 40 up near he top of main() so that the rest of your program can access those two stream objects. The two open statements are correct.

line 33: wrong comparison. If the user must enter either 1 or 2 then line 33 comparison should be if( d == '1' )

line 33: wrong comparison. If the user must enter either 1 or 2 then line 33 comparison should be if( d == '1' )

I have a variable "a" at the top that has the value of "1", i thought if the user inputs "1" the value of a would be used. but ok your the expert

Its still not working,

Oh yes, I see that variable. But it is initialized with the wrong value. Should be int a = '1'; meaning that '1' is not the same thing as 1.

its still not working

#include <iostream>
#include <fstream>
using namespace std;

fstream usern;
fstream passw;

int main(){

    // for logging in
    int Pass, Name;
    Pass = 159876;
    Name = 159874;
    int PassIn;
    int NameIn;
    // for opening the password and username files
    int d;
    int e;

    
    cout << "Please Enter your Username:";
    cin >> NameIn;
    
    cout << "Please Enter a Password:";
    cin >> PassIn;
    
    if (NameIn != Name ||  PassIn != Pass){
          cout << "Incorrect Username/Password Combintation \n";
          }
    if (NameIn == Name && PassIn == Pass){
          cout << "What would you like to do: (1 for Yes/ 2 for No) \n";
          cout << "a) Access the password list \n";
          cin >> d;
            if (d == '1'){
                passw.open ("password.txt");
                }
          cout << "b) Access the username list \n";
          cin >> e;
                  if (e == '1'){
                usern.open ("username.txt");
                }    
          cout << "c) Exit \n";
                  
      
              
          }
          system("pause");
          return 0;
}

what do you mean by "still not working"? What doesn't work ? All your program does is open a file if the user enters 1 to the prompt. So how do you know the program isn't doing that?

line 42: What's the purpose of that line? There is no cin so the user can not enter either 1 or 2 for that line.

What i mean by that is i want it to open the file so that I can read it, the same as if you clicked on a link to another webpage.

Well, it does open the file, it just doesn't do anything with it. If you want the file to be displayed on the screen then you have to add more code to read and print it.

ok, what do i have to read up on to display it on the screen?

By the way thanks for helping

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.