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

Dani AI

Generated

Short recap and a practical fix. wanted the program to "open" a file when the user types 1 or 2 and show its contents. The current code opens the streams but never reads from them. Also be consistent about input types: if you read into an int, compare to 1 (no quotes); if you read into a char, compare to '1'. was on the right track about the comparison, but a clearer approach is to open, check, read, and close the file where you need it (prefer local ifstream objects so RAII closes them automatically).

Example helper and menu (reads and prints the file to the console):

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

void print_file(const std::string& path) {
    std::ifstream in(path);
    if (!in) {
        std::cerr << "Cannot open file: " << path << '\n';
        return;
    }
    std::string line;
    while (std::getline(in, line)) {
        std::cout << line << '\n';
    }
}

int main() {
    int choice = 0;
    std::cout << "1) Show passwords  2) Show usernames  3) Exit\n";
    if (!(std::cin >> choice)) return 1;
    if (choice == 1) print_file("password.txt");
    else if (choice == 2) print_file("username.txt");
    return 0;
}

Quick troubleshooting and tips:

  • If the program says it cannot open the file, check the working directory. Place the text files next to the executable or use an absolute path while testing.
  • Always check the stream after opening (if (!in)), and report errors so you know why nothing appears.
  • If you plan to mix operator>> and getline, flush the leftover newline from cin (for example with std::string tmp; std::getline(std::cin, tmp);) before using getline.
  • Avoid global fstream objects unless needed; use local ifstream for clarity and safety.
  • Security note: do not store real passwords in plain text for real programs—use proper hashing and secure input methods.

This gives a clean, portable way to both open and actually display a file after a menu choice.

Recommended Answers

All 9 Replies

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.