>Read up on the std::string data type.
Okay...to add a little more substance, using the std::string type, you can concatenate ".txt" onto the end of a string after reading the name:
Code:
#include <fstream>
#include <iostream>
#include <string>
int main()
{
std::string name;
std::cout<<"Enter a name: ";
std::getline ( std::cin, name );
name += ".txt";
std::ifstream in ( name.c_str() );
if ( !in )
std::cerr<<"File cannot be opened\n";
else {
//...
}
}
Alternatively, because many students are generally not allowed to use the standard C++ classes to make life easier, a C based solution is also possible, though more error prone:
Code:
#include <cstring>
#include <fstream>
#include <iostream>
int main()
{
char name[100] = {0};
std::cout<<"Enter a name: ";
std::cin.getline ( name, sizeof name );
std::strcat ( name, ".txt" );
std::ifstream in ( name );
if ( !in )
std::cerr<<"File cannot be opened\n";
else {
//...
}
}