I am trying to write a program that allows the user to enter an ID number, and then the program opens a txt file with the ID information, but the ifstream part isnt working, I know its because I put a variable in, but I dont know how to make it work without a thousand if statements or switch-case statements. Any help?

#include <iostream>
#include <stdio.h>
#include <fstream>
using namespace std;

int main()
{

string id;
string ext=".txt";
string first;
string last;



cout<<"Enter ID Number:"<<endl;
cin>>id;
id+=ext;
ifstream file(id);
file>>first;
file>>last;
cout<<first<<endl<<last<<endl;
}

open() method takes a char* and you are passing a std::string class.

ifstream file(id.c_str());
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.