Question

C++ problem. hi heys, i am trying to remove beginning and the end whitespace in one...

C++ problem. hi heys, i am trying to remove beginning and the end whitespace in one file, and output the result to another file. But i am confused about how to remove whitespace. i need to make a function to do it. It should use string, anyone can help me ?

here is my code.

#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <fstream>

using namespace std;
void IsFileName(string filename);

int main(int argc, char const *argv[]) {
string inputfileName = argv[1];
ifstream inStream;
inStream.open("first.txt");
IsFileName(inputfileName);

string outputfileName = argv[2];
ofstream outStream;
outStream.open("second.txt");
IsFileName(outputfileName);

string line;
while (getline(inStream, line))
{
cout << line << endl;
outStream << line <<endl;
}

inStream.close();
outStream.close();

/*add code*/
return 0;
}// main

void IsFileName(string filename){
ifstream inStream;
inStream.open(filename.c_str());
if (inStream.fail()){
cout << "Error opening the file"<<endl;
exit(0);
}
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Question:

C++ problem. hi heys, i am trying to remove beginning and the end whitespace in one file, and output the result to another file. But i am confused about how to remove whitespace. i need to make a function to do it. It should use string, anyone can help me ?

here is my code.

#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <fstream>

using namespace std;
void IsFileName(string filename);

int main(int argc, char const *argv[]) {
string inputfileName = argv[1];
ifstream inStream;
inStream.open("first.txt");
IsFileName(inputfileName);

string outputfileName = argv[2];
ofstream outStream;
outStream.open("second.txt");
IsFileName(outputfileName);

string line;
while (getline(inStream, line))
{
cout << line << endl;
outStream << line <<endl;
}

inStream.close();
outStream.close();

/*add code*/
return 0;
}// main

void IsFileName(string filename){
ifstream inStream;
inStream.open(filename.c_str());
if (inStream.fail()){
cout << "Error opening the file"<<endl;
exit(0);
}
}

Answer:

Removing white-spaces from a string can be done by below logic,

** Define a 'index' variable to keep track of non-space character,

** In the given Input String 'line' traverse in the String.

** Check the given string index holds 'non-spaces' then increment the index value.and store the same character into string.

** Doing above operation will prevent adding 'white-space' character into the string. Output String Holds only non-space characters only.

As per above logic below is the Code New Code added in BOLD

#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <fstream>

using namespace std;
void IsFileName(string filename);

int main(int argc, char const *argv[]) {
string inputfileName = argv[1];
ifstream inStream;
inStream.open("first.txt");
IsFileName(inputfileName);

string outputfileName = argv[2];
ofstream outStream;
outStream.open("second.txt");
IsFileName(outputfileName);

string line;
while (getline(inStream, line))
{
cout << line << endl;
outStream << line <<endl;
}

inStream.close();
outStream.close();

/*add code*/

//start of the code

//remove spaces from the string 'line'

//define variable index to keep track of non-space character

int index = 0;

//do remove space logic as shown below

for(int i = 0; line[i]; i++){

//If character is not-a-space then

if (line[i] != ' ') {

line[index++] = line[i];

}

}

line[index] = '\0';

//end of remove spaces code
return 0;
}// main

void IsFileName(string filename){
ifstream inStream;
inStream.open(filename.c_str());
if (inStream.fail()){
cout << "Error opening the file"<<endl;
exit(0);
}
}

Compile and execute the above code gcc compiler compatible operating systems.

Sample Output For the Same Could be like this:

Input String: " PaulJames "

Output String: "PaulJames"

Add a comment
Know the answer?
Add Answer to:
C++ problem. hi heys, i am trying to remove beginning and the end whitespace in one...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C++ Programming question For this exercise, you will receive two string inputs, which are the names...

    C++ Programming question For this exercise, you will receive two string inputs, which are the names of the text files. You need to check if the input file exists. If input file doesn't exist, print out "Input file does not exist." You are required to create 4 functions: void removeSpace (ifstream &inStream, ofstream &outStream): removes white space from a text file and write to a new file. If write is successful, print out "Text with no white space is successfully...

  • In c++ please How do I get my printVector function to actually print the vector out?...

    In c++ please How do I get my printVector function to actually print the vector out? I was able to fill the vector with the text file of names, but I can't get it to print it out. Please help #include <iostream> #include <string> #include <fstream> #include <algorithm> #include <vector> using namespace std; void openifile(string filename, ifstream &ifile){    ifile.open(filename.c_str(), ios::in);    if (!ifile){ cerr<<"Error opening input file " << filename << "... Exiting Program."<<endl; exit(1); }    } void...

  • I am having trouble trying to output my file Lab13.txt. It will say that everything is...

    I am having trouble trying to output my file Lab13.txt. It will say that everything is correct but won't output what is in the file. Please Help Write a program that will input data from the file Lab13.txt(downloadable file); a name, telephone number, and email address. Store the data in a simple local array to the main module, then sort the array by the names. You should have several functions that pass data by reference. Hint: make your array large...

  • I need to add something to this C++ program.Additionally I want it to remove 10 words...

    I need to add something to this C++ program.Additionally I want it to remove 10 words from the printing list (Ancient,Europe,Asia,America,North,South,West ,East,Arctica,Greenland) #include <iostream> #include <map> #include <string> #include <cctype> #include <fstream> #include <iomanip> using namespace std; void addWord(map<std::string,int> &words,string s); void readFile(string infile,map<std::string,int> &words); void display(map<std::string,int> words);    int main() { // Declaring variables std::map<std::string,int> words;       //defines an input stream for the data file ifstream dataIn;    string infile; cout<<"Please enter a File Name :"; cin>>infile; readFile(infile,words);...

  • Can anyone help me with my C++ assignment on structs, arrays and bubblesort? I can't seem...

    Can anyone help me with my C++ assignment on structs, arrays and bubblesort? I can't seem to get my code to work. The output should have the AVEPPG from highest to lowest (sorted by bubbesort). The output of my code is messed up. Please help me, thanks. Here's the input.txt: Mary 15 10.5 Joseph 32 6.2 Jack 72 8.1 Vince 83 4.2 Elizabeth 41 7.5 The output should be: NAME             UNIFORM#    AVEPPG Mary                   15     10.50 Jack                   72      8.10 Elizabeth              41      7.50 Joseph                 32      6.20 Vince                  83      4.20 ​ My Code: #include <iostream>...

  • I have 2 issues with the C++ code below. The biggest concern is that the wrong...

    I have 2 issues with the C++ code below. The biggest concern is that the wrong file name input does not give out the "file cannot be opened!!" message that it is coded to do. What am I missing for this to happen correctly? The second is that the range outputs are right except the last one is bigger than the rest so it will not output 175-200, it outputs 175-199. What can be done about it? #include <iostream> #include...

  • The following program is in c++ ; I am trying to read in games from a...

    The following program is in c++ ; I am trying to read in games from a file and when doing so I am only reading in parts of the file. It is giving me a segmentation fault error I am going to post my code below if anyone is able to help me debug the program I would greatly appreciate it. The program is too large to be submitted as a single question on here :( --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- void Business::addGamesFromFile() {...

  • Hi, I want to creat a file by the name osa_list that i can type 3...

    Hi, I want to creat a file by the name osa_list that i can type 3 things and store there ( first name, last name, email). then i want to know how to recal them by modifying the code below. the names and contact information i want to store are 200. #include <iostream> #include <fstream> #include <cstdlib> #include <iomanip> using namespace std; class Student { private: string fname; string lname; string email;       public: Student(); Student(string fname, string lname,...

  • Hi there, I am working on a binary search tree code in c++. The program must...

    Hi there, I am working on a binary search tree code in c++. The program must store and update students' academic records, each node includes the student name, credits attempted, credits earned and GPA. I have made some progress with the code and written most of the functions in the .cpp file (already did the .h file) but i am struggling with what to put in the main and how to put an update part in the insert function. I...

  • /* BEGIN - DO NOT EDIT CODE */ // File: main.cpp #include "LoginAccount.h" #include "RegisteredLoginAccount.h" #include...

    /* BEGIN - DO NOT EDIT CODE */ // File: main.cpp #include "LoginAccount.h" #include "RegisteredLoginAccount.h" #include "GuestLoginAccount.h" #include <iostream> #include <fstream> #include <string> #include <sstream> using namespace std; //prototypes and constants const int ARRAY_SIZE = 20; using number_of_records_t = int; //Function Declarations. void readAccountsFromFile(ifstream& fin, RegisteredLoginAccount registeredAccounts[], number_of_records_t& registeredLength, GuestLoginAccount guestAccounts[], number_of_records_t& guestLength); bool openFileForInput(ifstream& ifs, const string& filename); bool openFileForOutput(ofstream& ofs, const string& filename); void writeAccountsToFile(ofstream& ofs, RegisteredLoginAccount registeredAccounts[], const number_of_records_t registeredLength, GuestLoginAccount guestAccounts[], const number_of_records_t guestLength); void writeAccountToFile(ofstream&...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT