Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7969325
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T07:13:10+00:00 2026-06-04T07:13:10+00:00

This week I started studing a text files in C++ and in my exercice

  • 0

This week I started studing a text files in C++ and in my exercice I have to do a program that the user enter the lines in the file, but… for each space that the user enters, the program asking the new to user.

Here is my code:

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main(void){
    ofstream myfile;
    string answer;

    do{
        cout << "Insert a line in the file[END to finalize]: ";
        cin >> answer;
        myfile.open("example.txt");
        myfile << answer;
        myfile.close();
    }while(answer != "END");
}

The result is:

Insert a line in the file[END to finalize]: Hello my friend

Insert a line in the file[END to finalize]: Insert a line in the
file[END to finalize]: Insert a line in the file[END to finalize]:

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T07:13:11+00:00Added an answer on June 4, 2026 at 7:13 am

    operator>>(istream&, string&) basically grabs the next word. If you want to grab a whole line, try std::getline(std::cin, answer);.

    getline won’t include the newline, though. Meaning you’ll have to do something like myfile << answer << '\n'; to output them as lines.

    BTW, in most cases you’d want to either open the file outside the loop, or open it for appending with something like myfile.open("example.txt", ios::app);. Opening the file in the loop each time like you’re doing, i’m pretty sure you position the file pointer at the beginning of the file, so each line you write will overwrite at least the first part of the previous line.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Started working on a new application this week that is running the latest rails
I have just started this week in exploring Android Open GL ES and related
I started using NHibernate this week (struggling). I have a small application with 3
I'm new to WinDbg and have started using it this week to diagnose memory
I just started learning Flex this week, and I cannot get an image path
Sorry if my question seems dumb. I've started using Eclipse Ganymede 3.4 this week
This week I have figured out how to modify form elements in the location
I've asked this question already this week but am rephrasing it with the hope
We have been having some debate this week at my company as to how
I just started working with jqGrid this week. I'm attempting to make a generic

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.