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

  • SEARCH
  • Home
  • 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 7690455
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T20:24:04+00:00 2026-05-31T20:24:04+00:00

if I have a text file like this : this is line one This

  • 0

if I have a text file like this :

this is line one
This is line two
this is line three

How would I use getline to read each into a stringstream, then print the stream into a new string while preserving the newline character? I am on a mac using Xcode 4. Here is my code: I am having trouble because the text it prints out only prints on one line.

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <sstream>
#include <cctype>
using namespace std;
string getInput ();
ifstream * openInFile ();
int getShiftValue ();
void cypherMenu ();
void menu ();
string shiftCharacters (int shiftNum, ifstream * inFile);
string getOutput ();
ofstream * openOutFile ();
void printSentence (string outData, ofstream * outFile);
void notOption (string optionString);
string capitalize (string choice);
string option ();
int main() {
ifstream * inFile;
ofstream * outFile;
string inFileName, outFileName, outData, optionString, capOptionString; 
int shiftNum = 0;
bool isOption = false; 
while (capOptionString.compare("2") != 0 || 
      capOptionString.compare("QUIT") != 0) {
   do {
   menu();

   optionString = option();
   capOptionString = capitalize(optionString);
   if (capOptionString.compare("1") == 0 || capOptionString.compare("CAESAR")
       == 0) {
       isOption = true;
   }
   else if (capOptionString.compare("2") == 0 || 
            capOptionString.compare("QUIT") == 0) {
       isOption = false;
       return 0;
   }
   else {
       notOption(optionString);
   }
   }
   while (!isOption);
   cypherMenu();




   inFile = openInFile(); 
   shiftNum = getShiftValue();
   outData = shiftCharacters(shiftNum, inFile);
   inFile->clear();
   inFile->close();
   outFile = openOutFile();
   printSentence(outData, outFile);
   outFile->clear();
   outFile->close();
}
   return 0;
}
// Input Functions
string getInput () {
cout << "Enter an input file name: "; 
string inFileName;
getline(cin, inFileName); 
return inFileName;
}
string getOutput () {
string outFileName;
cout << "Enter an output file name: ";
getline(cin, outFileName);
cout << endl;
return outFileName;
}
ifstream * openInFile () {
ifstream * inFile;   
bool isGood = false; 
string inFileName;    
inFile = new ifstream;
do {   
    inFileName = getInput();
    inFile->open(inFileName.c_str());
   if (inFile->fail()) { 
       cout << "Couldn't open file" << endl;
    }
   else {
       isGood = true;
   }
}
while (!isGood);
return inFile;
}
ofstream * openOutFile () {
ifstream testStream; 
ofstream * outFile;   
bool isUnique = false; 
string fileName;
do {   
   fileName = getOutput();
   testStream.clear(); 
   testStream.open(fileName.c_str(), ios_base::in);
   if (testStream.good()) {
            cout << "The file already exists, please choose another" 
            << endl;
            testStream.clear();
            testStream.close();
    }
    else {
            isUnique = true;
            testStream.clear();
            testStream.close();
    }
}
while (!isUnique);
outFile = new ofstream;
outFile->open(fileName.c_str());
return outFile;
}
int getShiftValue () {
int shiftNum;
string trash;
cout << "Please enter shift value: ";
cin >> shiftNum;
getline(cin, trash); 
return shiftNum;
}
string option () {
string optionString;
getline(cin, optionString);
cout << endl;
return optionString;
}
// Data manipulation functions 
 **string shiftCharacters (int shiftNum, ifstream * inFile){
 string inData, outData, trash; 
 char outChar;
int idx = 0, length = 0;
stringstream outSentence; 
 do { 
 while (getline(* inFile, inData, '\n')) {
     getline(* inFile, trash);
     for (idx = 0; idx <= inData.length() - 1; idx++) {
        if (inData[idx] >= 'a' && inData[idx] <= 'z') {
            outChar = (((inData[idx] - 'a') + shiftNum) % 26) +
            'a';
            outSentence << outChar;
            length += 1;
        }
        else if (inData[idx] >= 'A' && inData[idx] <= 'Z') {
            outChar = (((inData[idx] - 'A') + shiftNum) % 26) +
            'A';
            outSentence << outChar;
            length += 1;
        }


        else {
            outChar = inData[idx];
            outSentence << outChar;
            length += 1;
        }
    }
     outSentence << trash;

 }
 }
 while (!(inFile->eof()));


 outData.resize(length);

while (!(outSentence).eof()) {
    // outSentence >> noskipws >> outData;
     getline(outSentence, outData);

 }

 return outData;
 }**
string capitalize (string choice) {
string outString;
outString.resize(choice.length());
transform(choice.begin(), choice.end(), outString.begin(), ::toupper);
return outString;
}
// Output funcitons
void cypherMenu () {
cout << "C A E S A R  C Y P H E R  P R O G R A M" << endl
    << "========================================" << endl;


  return;
    }
    void printSentence (string outData, ofstream * outFile) {
    int idx = 0;
    char outChar;
    stringstream outString;
    outString << outData;
    for (idx = 0; idx <= outData.length() - 1; idx++) {  
        outChar = outString.get();
        outFile->put(outChar); 
    }
    }
    void menu () {
    cout <<  "Available Options: " << endl 
        << "1. CAESAR - encrypt a file using Caesar Cypher" << endl
        << "2. QUIT - exit the program" << endl << endl
        << "Enter a keyword or option index: ";
    return;
    }
    void notOption (string optionString) {
    cout << optionString << " is an unrecognized option, try again" << endl 
        << endl;
    return;
    }

The Problem lies within the function shiftCharacters. I am not sure how to get it to preserve the new line character please help?? The code is compilable.

  • 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-05-31T20:24:06+00:00Added an answer on May 31, 2026 at 8:24 pm
    getline( the_stream, the_string );
    the_string += '\n';
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a plain text file looking like this: some text containing line breaks
Let's say you have a text file like this one: http://www.gutenberg.org/files/17921/17921-8.txt Does anyone has
I have text file with something like first line line nr 2 line three
I have text file with some stuff that i would like to put into
I have a text file looks like this : 100 50 20 90 4.07498
I have text file with a row like this: SendersTimeSeriesIdentification: COMPANY_A/COMPANY_B/REF_7/20090505 I'd like to
I have a text file that looks like this: value1 value2 value3 There are
I have string like this: G:\Projects\TestApp\TestWeb\Files\Upload\file.jpg How can I remove all text before Files
Is there a faster way to search each line of one text file for
I have a text file with catalog names (one per line) and I need

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.