Thread: Fstream Problem

  1. #1
    Xterria
    Guest

    Question Fstream Problem

    Ok...I have a problem with fstream. When ever you type in a space when you type what you want in the fstream generated file, the first word only comes out. For example, my sample code, compiles but it only shows the first word:

    #include <iostream.h>
    #include <fstream.h>
    int main(void)
    {
    char paragraph[20];
    cout << "Enter a sentence:";
    cin >> paragraph;
    ofstream a_file (test.txt);
    a_file << "Your Paragraph is:" << endl
    << paragraph;
    return 0;
    }

    Why is this? Someone Please Help. Thanks in advance.

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    you are using cin>> into a char array... this will only read up until the first whitespace character......

    use cin.getline(paragraph,20) instead.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    Xterria
    Guest

    Talking

    You've got no idea how helpful you've been these past few months..thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  2. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  3. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM
  4. Replies: 5
    Last Post: 11-07-2005, 11:34 PM
  5. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM