Hello everyone,

I need help. In my assignment i have an input file that contains string data: ----> i love music and chicken. today is the first day of the rest of your life. my name is john.

I must change it to the following in the output file: ----> I love music and chicken. Today is the first day of the rest of your life. My name is john.

I need to use this function to help achieve this:
void initialCap(string&);


No classes are required.

All the white space is erased initially that's why i use the insert function. However, the last word "john" is erased when i use this function. How do i increase the size of the string in order to still have john included in the output. I realize string is an array and arrays are immutable but i know there has to be a way around this. Also, I can't capitalize the words i need to capitalize. I need to use the replace function but, i'm not sure how the parameters work.

Any help is greatly appreciated

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


void initialCap(string&);

int main () {	
	ifstream inputFile;	
	inputFile.open("C:/Notepad++/input.txt");
	ofstream outputFile;	
	outputFile.open("c:/Notepad++/output.txt");
	
	string baseSentence = "";
	string str2 = " dave";
	
	

	if (inputFile) {
			while (!inputFile.eof()) {	
				string str1 = baseSentence;
				str1.insert(0," ");
				inputFile>>baseSentence;
				cout<<str1;	
				outputFile<<baseSentence;	
		}		
		
	}
	else {
		cout<<"this file does not exist dave!";
	}	

	inputFile.close();
	outputFile.close();
	cout << "\n\n";
	system("pause");
	return 0;
}

First of all the while loop is incorrect. Note that the insert() method is not neeced because simple concantination is all that is needed.

while( inputFile>>baseSentence )
{
    str1 = str1 + baseSentence + " ";

}

An even better way to do that is to replace that while loop with getline(), assuming the sentences are all on the same line. getline() will preserve all the white spaces.

getline(inputfile, baseSentence);

You did not post initialCap() function, so I can't help you with that.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.