|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
hello,
Im working on a little test program where the user can input reminder notes in a simple little program then load them later. This is just a part of the code ![]() PHP Code:
Now the output comes out right exept for the note part is only writing the first word or group of text??? Second what kinda file should i be saving to and how would i load the text? thanks for yor time, jordan Last edited by xdrunkcowx : June 1st, 2003 at 11:49 PM. |
|
#2
|
|||
|
|||
|
"Now the output comes out right exept for the note part is only writing the first word or group of text???"
Actually it's cin>>note that is only reading the first word, and that's because the >> operator is defined to stop reading as soon as it encounters whitespace(spaces, tabs, or newlines). For string types: string text; getline(cin, text); For char arrays: char text[100]; cin.getline(text, 100); "Second what kinda file should i be saving to and how would i load the text?" Writing to a text file is fine. If by "load" you mean "read from the file" Code:
ifstream inFile("database.txt");
string text;
while(!inFile.eof())
{
getline(inFile, text);
cout<<text<<endl;
}
|
|
#3
|
|||
|
|||
|
okay thanks a lot !!
Im still not sure on how to use For string types: string text; getline(cin, text); string isnt a valid var type and i dont know how to use the first chunk of code eather thanks again, jordan Last edited by xdrunkcowx : June 2nd, 2003 at 06:35 AM. |
|
#4
|
||||
|
||||
|
you need to #include <string> and
using namespace std; |
|
#5
|
|||
|
|||
|
Thanks! but guys or girls you need to remember im a newb please be descriptive in your answers
![]() |
|
#6
|
|||
|
|||
|
Code:
Im still not sure on how to use For string types: string text; getline(cin, text); Code:
#include<iostream> //cout, cin defined in there
#include<string> //string types defined in there
#include <fstream> //ofstream, ifstream defined in there
using namespace std;
//need this otherwise you would have to refer to cout with
//its full name std::cout, same with everything else in the
//standard library. Read about "namespaces" for more
//information
int main()
{
ofstream outFile("C:\\TestData\\output.txt");
string text;
for(int i=0; i<3; i++)
{
cout<<"Enter a message:"<<endl;
getline(cin, text);
//Reads from the keyboard until a \n is encountered
//which occurs when the user hits return.
outFile<<text<<endl;
}
ifstream inFile("C:\\TestData\\output.txt");
cout<<endl<<"Here are the messages:\n";
while(!inFile.eof()) // while not at the end-of-file
{
getline(inFile, text);
cout<<text<<endl;
}
return 0;
}
Last edited by 7stud : June 2nd, 2003 at 02:32 PM. |
|
#7
|
|||
|
|||
|
Get the book: "Ivor Horton's Beginning C++" and you can look up stuff you don't know how to do, or don't understand.
|
![]() |
| Viewing: Dev Shed Forums > Programming Languages > C Programming > foo not writing whole var |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|