Thread: My program works but only one problem...

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    11

    My program works but only one problem...

    http://rapidshare.de/files/17827852/awesome.rar.html

    1. scroll down and click FREE

    2. download it

    3. extract anywhere

    4. run it

    5. specify the file type and contents

    6. come back here.



    As you have noticed, it does not allow spaces. I don't know what could be wrong...


    Code:
    #include <fstream>
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    string fileName;
    string fileCont;
    
    using namespace std;
    
    int main()
    {
    cout<<"What filename should we use? ";
    cin>>fileName;
    cin.ignore();
    cin.get();
    cout<<"What content should we statch into the file? ";
    cin>>fileCont;
    cin.ignore();
    ofstream file (fileName.c_str());
    file<<fileCont;
    cin.get();
    return 0;
    }

    Any help would be greatly appreciated.


    Never mind the double using namespace std; 's, it was an error.
    Last edited by Rusty_chainsaw; 04-12-2006 at 09:59 AM.

  2. #2
    C / C++
    Join Date
    Jan 2006
    Location
    The Netherlands
    Posts
    312
    cin >> reads until the next space.

    use
    Code:
    getline(method, targetvariable, delimiter);
    
    // method = cin, by example
    // targetvariable = variable to store input
    // delimiter(optional) = '\n' or another cstring character.
    Operating Systems:
    - Ubuntu 9.04
    - XP

    Compiler: gcc

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    11
    When I use that it works perfectly, but when I remove cin.ignore() and cin.get() it doesn't show anything in the file.


    And when I don't I need to hit enter three times.
    Last edited by Rusty_chainsaw; 04-12-2006 at 10:18 AM.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Leave cin.ignore() after each use of cin>> (e.g. the filename), but don't use cin.ignore() after a call to getline (e.g. the file content). The ignore() ignores the trailing newline from when the user types an answer and hits enter, but getline automatically ignores that newline for you, so you only want to call ignore() explicitly after cin >>.

    Then, add a cin.get() to the end of your program to keep it open.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Mastermind program problem with iterator
    By Truckomobil in forum C++ Programming
    Replies: 3
    Last Post: 06-08-2009, 01:16 PM
  2. Replies: 14
    Last Post: 03-02-2008, 01:27 PM
  3. Some Problem With My Program
    By Americano in forum C Programming
    Replies: 5
    Last Post: 10-18-2003, 01:58 AM
  4. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM
  5. Program abort due to memory problem
    By cazil in forum C++ Programming
    Replies: 5
    Last Post: 01-21-2002, 12:55 PM