C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old June 1st, 2003, 11:44 PM
xdrunkcowx xdrunkcowx is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Posts: 5 xdrunkcowx User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Question 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.

Reply With Quote
  #2  
Old June 2nd, 2003, 02:13 AM
7stud 7stud is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Feb 2001
Posts: 1,365 7stud User rank is Private First Class (20 - 50 Reputation Level)7stud User rank is Private First Class (20 - 50 Reputation Level) 
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;
}

Reply With Quote
  #3  
Old June 2nd, 2003, 06:31 AM
xdrunkcowx xdrunkcowx is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Posts: 5 xdrunkcowx User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #4  
Old June 2nd, 2003, 01:11 PM
infamous41md's Avatar
infamous41md infamous41md is offline
not a fan of fascism (n00b)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Feb 2003
Location: ct
Posts: 2,756 infamous41md User rank is Second Lieutenant (5000 - 10000 Reputation Level)infamous41md User rank is Second Lieutenant (5000 - 10000 Reputation Level)infamous41md User rank is Second Lieutenant (5000 - 10000 Reputation Level)infamous41md User rank is Second Lieutenant (5000 - 10000 Reputation Level)infamous41md User rank is Second Lieutenant (5000 - 10000 Reputation Level)infamous41md User rank is Second Lieutenant (5000 - 10000 Reputation Level)infamous41md User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 2 Days 11 h 4 m 29 sec
Reputation Power: 94
you need to #include <string> and
using namespace std;

Reply With Quote
  #5  
Old June 2nd, 2003, 02:17 PM
xdrunkcowx xdrunkcowx is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Posts: 5 xdrunkcowx User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
  #6  
Old June 2nd, 2003, 02:19 PM
7stud 7stud is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Feb 2001
Posts: 1,365 7stud User rank is Private First Class (20 - 50 Reputation Level)7stud User rank is Private First Class (20 - 50 Reputation Level) 
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.

Reply With Quote
  #7  
Old June 2nd, 2003, 02:33 PM
7stud 7stud is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Feb 2001
Posts: 1,365 7stud User rank is Private First Class (20 - 50 Reputation Level)7stud User rank is Private First Class (20 - 50 Reputation Level) 
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > foo not writing whole var

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap