#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    5
    Rep 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 :p
    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.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Feb 2001
    Posts
    1,472
    Rep Power
    15
    "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;
    }
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    5
    Rep 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.
  6. #4
  7. not a fan of fascism (n00b)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Feb 2003
    Location
    ct
    Posts
    2,756
    Rep Power
    95
    you need to #include <string> and
    using namespace std;
  8. #5
  9. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    5
    Rep Power
    0
    Thanks! but guys or girls you need to remember im a newb please be descriptive in your answers :)
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Feb 2001
    Posts
    1,472
    Rep Power
    15
    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.
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Feb 2001
    Posts
    1,472
    Rep Power
    15
    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.

IMN logo majestic logo threadwatch logo seochat tools logo