The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
foo not writing whole var
Discuss foo not writing whole var in the C Programming forum on Dev Shed. foo not writing whole var C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

June 1st, 2003, 11:44 PM
|
|
Junior Member
|
|
Join Date: Jun 2003
Posts: 5
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
foo not writing whole var
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:
cout << "Date:";
cin >> date;
cout << "Note: \n";
cin >> note;
ofstream foo("datebase.txt");
foo << "Date:" << date << "\n";
foo << "Note:" << note;
}
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.
|

June 2nd, 2003, 02:13 AM
|
|
Contributing User
|
|
Join Date: Feb 2001
Posts: 1,365

Time spent in forums: 18 h 9 m 25 sec
Reputation Power: 14
|
|
"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;
}
|

June 2nd, 2003, 06:31 AM
|
|
Junior Member
|
|
Join Date: Jun 2003
Posts: 5
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
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.
|

June 2nd, 2003, 01:11 PM
|
 |
not a fan of fascism (n00b)
|
|
Join Date: Feb 2003
Location: ct
|
|
|
you need to #include <string> and
using namespace std;
|

June 2nd, 2003, 02:17 PM
|
|
Junior Member
|
|
Join Date: Jun 2003
Posts: 5
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
Thanks! but guys or girls you need to remember im a newb please be descriptive in your answers 
|

June 2nd, 2003, 02:19 PM
|
|
Contributing User
|
|
Join Date: Feb 2001
Posts: 1,365

Time spent in forums: 18 h 9 m 25 sec
Reputation Power: 14
|
|
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.
|

June 2nd, 2003, 02:33 PM
|
|
Contributing User
|
|
Join Date: Feb 2001
Posts: 1,365

Time spent in forums: 18 h 9 m 25 sec
Reputation Power: 14
|
|
|
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.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|