Question

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 openofile(string filename, ofstream &ofile){
  
ofile.open(filename.c_str(), ios::out);
  
if (!ofile){
cerr<<"Error opening output file " << filename << "... Exiting Program."<<endl;
exit(1);
}
  
}

void fillVector(vector<string> vecName,int &dsize, ifstream &fin){
cout << "Starting to fill array" << endl;
int i =0;
string names;
while(!fin.eof()){
fin >> names;
vecName.push_back(names);
cout << vecName[i] << endl;
i++;
}
dsize = i;
}

void printVector(vector<string> vecName,int &dsize, ostream &out){
for(int i=0; i< vecName.size(); i++){
out << vecName[i] << " ";
}
out << endl;
}


int main(){
  
vector<string> vecName;
int dsize=0;
string iFilename = "words.txt";
string oFilename1 = "wordsoutput.txt";
string oFilename2 = "sortedWordsOutput.txt";
string oFilename3 = "uppercasewords.txt";
ifstream fin;
ofstream fout;
openifile(iFilename, fin);
fillVector(vecName, dsize,fin);
openofile(oFilename1, fout);
printVector(vecName,dsize,cout);
printVector(vecName,dsize,fout);
fout.close();

  
return 0;
}

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

#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 openofile(string filename, ofstream &ofile){

ofile.open(filename.c_str(), ios::out);

if (!ofile){

cerr<<"Error opening output file " << filename << "... Exiting Program."<<endl;

exit(1);

}

}

// we should take vector as refernce type than only

// we can point to the passed value otherwise it will create new vector and adds value to that vector

//instead of original vector

void fillVector(vector<string> &vecName,int &dsize, ifstream &fin){

cout << "Starting to fill array" << endl;

int i =0;

string names;

while(!fin.eof()){

fin >> names;

vecName.push_back(names);

cout << vecName[i] << endl;

i++;

}

dsize = i;

}

void printVector(vector<string> vecName,int &dsize, ostream &out){

for(int i=0; i< vecName.size(); i++){

out << vecName[i] << " ";

}

out << endl;

}


int main(){

vector<string> vecName;

int dsize=0;

string iFilename = "words.txt";

string oFilename1 = "wordsoutput.txt";

string oFilename2 = "sortedWordsOutput.txt";

string oFilename3 = "uppercasewords.txt";

ifstream fin;

ofstream fout;

openifile(iFilename, fin);

fillVector(vecName, dsize,fin);

openofile(oFilename1, fout);

printVector(vecName,dsize,cout);

printVector(vecName,dsize,fout);

fout.close();

return 0;

}

Add a comment
Answer #2

It seems like your code is almost correct. There is just a small issue in the printVector function. Instead of passing ostream &out, you should pass ofstream &out in the function signature to correctly print the vector to an output file.

Here's the corrected version of the printVector function:

cppCopy codevoid printVector(vector<string> vecName, int &dsize, ofstream &out) {    for (int i = 0; i < dsize; i++) {
        out << vecName[i] << " ";
    }
    out << endl;
}

In this updated function, we pass the ofstream &out instead of ostream &out, which allows us to correctly print the vector to an output file. With this change, the function will now work as expected to print the vector to the specified output file.

Your main function and other functions look good, and the program should now successfully print the vector to both the console and the wordsoutput.txt file.

answered by: Hydra Master
Add a comment
Know the answer?
Add Answer to:
In c++ please How do I get my printVector function to actually print the vector out?...
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
  • /* 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&...

  • 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];...

  • The following is a sample inventory in C++, i want to ask the user to input a item number for removing from inventory. //CPP #include <iostream> #include <fstream> #include <cstdlib>...

    The following is a sample inventory in C++, i want to ask the user to input a item number for removing from inventory. //CPP #include <iostream> #include <fstream> #include <cstdlib> #include <iomanip> #define MAX 1000 using namespace std; //Function to Add a new inventory item to the data into the array in memory void addItem(string desc[],string idNum[], float prices[], int qty[],int &num) { cout<<"Enter the names:"; cin>>desc[num]; cout<<"Enter the item number:"; cin>>idNum[num]; cout<<"Enter the price of item:"; cin>>prices[num]; cout<<"Enter the...

  • Can someone help me put the string removee the return to an outfile and fix?? prompt: CS 575 LabEx14: no e’s Let the user specify an input file and an output file (text files). Read the input file and...

    Can someone help me put the string removee the return to an outfile and fix?? prompt: CS 575 LabEx14: no e’s Let the user specify an input file and an output file (text files). Read the input file and write the output file; the output file should be the same as the input file, except that it has no e’s. For example, if the input file had the line. Streets meet in the deep. Then the output file would have...

  • I need help debugging this C++ prgram. What Am i doing wrong? //******************************************************** // This program...

    I need help debugging this C++ prgram. What Am i doing wrong? //******************************************************** // This program reads two input files whose lines are //ordered by a key data field. This program should merge //these two files, writing an output file that contains //all lines from both files ordered by the same key field. // //********************************************************* #include <iostream> #include<string> #include<fstream> //prototype void mergeTwoFiles (ifstream&,ifstream&, ofstream&); using namespace std; int main() {string inFile1,inFile2,outFile; // input and output files ifstream in1; ifstream in2;...

  • Hi! 1. I need some help with sorting string in a text file. My goal is...

    Hi! 1. I need some help with sorting string in a text file. My goal is to 1 shift left strings for string.length time. I was able to do that successfully. However, I am also trying to sort, using insertion sort , the resulting shifts. I store all shifts in a vector (or is it better to use an array?!) , and try to sort them that way, but my output is just the shifted strings but not sorted. Can...

  • 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 update this C++ code according to these instructions. The team name should be...

    I need to update this C++ code according to these instructions. The team name should be "Scooterbacks". I appreciate any help! Here is my code: #include <iostream> #include <string> #include <fstream> using namespace std; void menu(); int loadFile(string file,string names[],int jNo[],string pos[],int scores[]); string lowestScorer(string names[],int scores[],int size); string highestScorer(string names[],int scores[],int size); void searchByName(string names[],int jNo[],string pos[],int scores[],int size); int totalPoints(int scores[],int size); void sortByName(string names[],int jNo[],string pos[],int scores[],int size); void displayToScreen(string names[],int jNo[],string pos[],int scores[],int size); void writeToFile(string...

  • 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...

  • C++

    /*Colesha PearmanCIS247CATM ApplicationMay 4,2020*///Bring in our libaries#include"stdafx.h" #include<iostream>#include<conio.h>#include<string>#include<fstream>//read/write to files#include<ctime>//time(0)#include<iomanip>//setpresision stdusing namespace std; //create constant vaules-- cannot be changedconst int EXIT_VALUE = 5;const float DAILY_LIMIT = 400.0f;const string FILENAME = "Account.txt"; //create balance variabledouble balance = 0.0; //prototypesvoid deposit(double* ptrBalance);void withdrawal(double* ptrBalance, float dailyLimit);  //overloaded method-this verision does not take withdrawal amount void withdrawal(double* ptrBalance, float dailyLimit, float amount);  //overloaded method that takes withdrawal amount ///Enrty point to the application int main(){                 //look for the starting balance; otherwise generate a random balance                 ifstream...

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