int write()
{
    system("cls");
    
    char text[2000];
    char thing[100];
    
    cout << "which file would you like to write to?";
    cin.getline (thing, 100);
    
    cout << "what would you like to write? (up to two thousand characters)";
    cin.getline (text, 20000);
    
    ofstream file;
    file.open(thing);
    file<<text;
    file.close();
    
    system("pause");
    
    return haha();
}

ok so I'm writing a program that can write to a file and read a file from cmd prompt, not anything i can sell but im a bit of a beginner and i thought this would be a good program to write to use fstream which i just learned and iostream which i learned first. haha() is a function i wrote that is basically a crossroads function, it directs the user to wherever they want to go. now my problem is that whenever i run the file it just charges through the cin.getline's, it comes up with both of them at the same time and whatever the user types in goes to both of them which isnt very useful. please help, what did i do wrong? if you need more of my program tell me

Recommended Answers

All 4 Replies

For me, this problem only occurred when I entered more than 100 characters. Try adding this:

if(cin.fail())
  cin.ignore(256, '\n');

just after you read the filename.

Other things wrong with your code:
-Don't use system commands. Just don't.
-You are letting the user input 20,000 characters into a 2,000 character buffer. Could be a typo, but fix it anyway.

For me, this problem only occurred when I entered more than 100 characters. Try adding this:

if(cin.fail())
  cin.ignore(256, '\n');

just after you read the filename.

Other things wrong with your code:
-Don't use system commands. Just don't.
-You are letting the user input 20,000 characters into a 2,000 character buffer. Could be a typo, but fix it anyway.

i tried that and it still just charges through, what happens is when i run this function the output is "which file would you like to write to?what would you like to write? (up to two thousand characters)"

then you can type

For me, this problem only occurred when I entered more than 100 characters. Try adding this:

if(cin.fail())
  cin.ignore(256, '\n');

just after you read the filename.

Other things wrong with your code:
-Don't use system commands. Just don't.
-You are letting the user input 20,000 characters into a 2,000 character buffer. Could be a typo, but fix it anyway.

nevermind, there was input in the stream from my other functions, i cleared it with cin.ignore() and it was fixed, thanks death though. you gave me the idea :)

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.