I just need a simple program fragment that saves sentences/phrases and can load as exactly as the input. Do not rely on this code as I had made several failed attempted solutions. This is only what I remember:

#include<fstream.h>
#include<iostream.h>
#include<string.h>
#include<conio.h>
main()
{int x=0;char strings[5][50];
ofstream save("data.txt",ios::out)
while(x<5)
{gets(strings[x]);
save<<strings[x]<<endl;
x++;}
save.close();
x=0;
/* ifstream load("data.txt",ios::in);
while(!load.eof())
{load>>strings[x];
puts(strings[x]);
x++;
}
load.close(); */
getche();
return 0;
}

I just want these strings to retain the space between words. my problem is that these spaces are vanishing and strings tend to contract instead or just gave the first word.
Input:
the food is good
going high
delicious pie

Expected output:
the food is good
going high
delicious pie

Not this (my problem):
the
going
delicious
--OR--
thefoodisgood
goinghigh
deliciouspie

I will need it to store sentences in a simple manner so I'm focused on saving sentences and loading it exactly as what the input gives. Don't give tips because my mind burnt out on studying functions, just give the code directly as I only need a very simple program. Thank you.

Recommended Answers

All 3 Replies

Don't give tips because my mind burnt out on studying functions, just give the code directly as I only need a very simple program. Thank you.

You will find that most people here wont go for that. I will also take it from the code you posted that you are using turbo c++. Unless this is what you have to use for school I would seriously look for a different IDE/Compiler. If you search there are plenty of references for good free up to date compilers.

Your spaces are disapperaring because you aren't using an input method that grabs a whole line and stores it (like getline(cin, <string>), for instance). Maybe you could also just use the string class instead of C-strings because they make so much more sense and are so much more useful in actual programming--unless of course using a C-string is what you have to do, in which case you just need to adjust your input methods and possibly append a " " to the end of each C-string except for the last one. Just a thought.

And in response to the above poster, I totally agree and think you might want to try Visual Studio because it is a great (and free to use for students) all-in-one IDE for multiple languages.

(and free to use for students)

the Express version is free for everyone. Code::Blocks with MinGW compiler is also an excellent free compiler, it's also portable between MS-Windows and Linux. There might be a version for MAC, but I don't know about that.

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.