So, I'm making a simple program that can edit text files. I have a text input that says "Please enter the name of the file, followed by a '.txt'. ". Although, whenever I enter a name, it will only show you the first character of what you entered. So, how would I have my input be able to store more than one letter at a time?

Recommended Answers

All 3 Replies

post code. you need to store the filename in either a character array or std::string. Example:

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

int main()
{
     std::string filename;
     cout << "Enter a filename\n";
     getline(cin, filename);
     cout << filename << "\n";
}

Heres the main part:

#include <iostream>
#include <windows.h>
#include <fstream>

using namespace std;
char txtname;

int main(void)
{
blah blah blah blah....

if (menu == 3) {
cout << txtname;
cout << "\nPlease enter the name of the file, followed by a '.txt'. ";
cin >> txtname;
}
}

Heres the main part:

#include <iostream>
#include <windows.h>
#include <fstream>

using namespace std;
char txtname;

int main(void)
{
blah blah blah blah....

if (menu == 3) {
cout << txtname;
cout << "\nPlease enter the name of the file, followed by a '.txt'. ";
cin >> txtname;
}
}

You have txtname declared as a char, so it can only store one character. You need a char ARRAY or a string. I like strings so I would change txtname to type string (make sure to #include the string library). If you do that, it will store the full filename.

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.