So this is assignment number 14 and this is the fourth time Im bothering you...I hope you can help me.
I think Im almost done with it but I can not get "toupper" my name and lastname so it would be copied to the outfile.txt file.

Description: The program writes a while loop that copies all the characters from an input file stream(The file will have my name and lastname) to an output file stream.The only exeption will be that every lowecase is converted to uppercase.the loop should terminate when end-of-file is detected. the information should be writen to the new file.

#include<iostream>
#include<cctype>
#include<fstream>
#include <string>

using namespace std;
int main()
{

char ch=0;	
char name=touppercase(ch);
//reads from a previously created file that contains my name and lastname

ifstream fin;
ifstream infile("infile.txt"); //opens the file
if(!infile.good()) throw "I/O error"; 

while(!infile.eof()) //while loop as specified by the teacher.
{

infile.get(ch);
fin>>ch;
cout<<ch;

}
infile.close();

//process

//this part creates the output file and should write my lastname but this time it
//should be in UPPER cases. this is where I cant get it to work!!!! HELP!

ofstream outfile;
outfile.open("outfile.txt");
if(!outfile.good()) throw "I/O error";
while (!outfile.eof())
{
	name=tolower(ch);
cout<<name;
outfile>>name<<endl;
}
outfile.close();


//close file
//opens the file and reads the content to then close the file and program.

ifstream outf;
outf.open("outfile.txt");
if(!outf.good()) throw "I/O error";
while (!outf.eof())
{
outf<<name;
cout<<name;

}

system("pause");
return 0;
}

Recommended Answers

All 18 Replies

In line 38 of your program, why have to used tolower() if you want it to be in uppercase?

you are right and its toupper. I changed it and it still doesnt work. I believe that the problem is in the second while loop(statement) but i do not know how to change it :(

I want to say something like... while characters are still in lower case...
{
convert to upper case;
print it to the outfile.txt;
}

What do you get as output on console of that while loop, i mean is it in lowercase or uppercase? (after changing that statement to uppercase)

I "changed it to uppercase but It still prints lower cases

Okay, correct me if I am wrong:

In your console, first you getting the character from file which you are printing on the screen, just making sure that you are getting it. You are getting the character on screen.

Then because of your second loop, let suppose in the first loop you got data which was in lower case, then you are getting the same character as the first one in your console.

Right??

Thats exactly what is happening. the cout will be delete it once I figure out how to get it to work. so this is what is happening (I think you said it already )

Mario


//then here comes the TOUPPER(ch)
.....
it does not print it in the console and it does not copy it to the file.


EDIT.
I changed the position of the << and it prints AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA which means its getting only the last character from my lastname hmmm i think Iam almost done... gotta figure it out how to get all the CHARACTERS =/

Do you know how to use a debugger? it will be very easy for you to locate why its printing only the last character of your name.

I do lol.. but it says it does not have bugs which means I did something legal but doesnt mean it was correct whichh sucks :D lol :(

------ Build started: Project: assignment14, Configuration: Debug Win32 ------
Compiling...
assignment14.cpp
Linking...
Embedding manifest...
Build log was saved at "file://h:\CS110\assignment14\Debug\BuildLog.htm"
assignment11 - 0 error(s), 0 warning(s)
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========

I mean just put a break point in the second while loop and check what are the characters that you are getting and see if they are being changed to uppercase.

wow yeah I tought using a break but didnt work the first time. now it prints it in the command prompt . It does not copy it to the file... hmm i will try to use functions now. maybe if i return the variable that contains the upper cases ... :P let me see. oh and btw thats for the help ;)

No problem! :)

ok couldnt figure out how to print it in outfile.txt :@ its 5 am ive been trying since 11pm and I just need to go to sleeeep :/

#include<iostream>
#include<cctype>
#include<fstream>
#include <string> 

using namespace std;

void closefile();
void process(char name);
int openfile(char ch,char name);

int main()
{   char ch=0;
	char name=0;
	openfile(ch,name);
	process(name);
	closefile();

	system("pause");
	return 0;
}
int openfile(char ch,char name  )
{
	ifstream fin;
	ifstream infile("infile.txt");
	//opens the file
	if(!infile.good()) throw "I/O error";
	while(!infile.eof()) 
	{
		infile.get(ch);
		fin>>ch;
		name=toupper(ch);
		cout<<name;
	}
return name;
}//reads from a previously created file that contains my name and lastname

void process(char name)
{	
	ofstream fout;
	ofstream outfile("outfile.txt");
	if(!outfile.good()) throw "I/O error";
		while(!outfile.eof())
		{
		fout<<name<<endl;
		break;
		}
}//this part creates the output file and should write my lastname but this time it should be in UPPER cases

void closefile()
{
	ifstream infile;
	ofstream outfile;
	infile.close();
	outfile.close();
}
//closes all the files.

It would be great if someone could give me a hint if not then i will be here tomorrow trying to do it XD :(

A quick hint. In the function that starts on line 38 you create one file stream, then create another, only open a file in the second one and then try to write to the first one. It's ok to be frustrated but take a step back and ask if what you are doing makes sense. You do the same thing with the infile/fin in the function starting on line 22. Open one stream and do what you need to do with it.

Your function on line 50 does absolutely nothing. If you're going to close the file streams (and you should), close them within the function that opened them. If you really wanted to you could pass around references to your infile and outfile but that's a bit tedious.

Hi thx for the help. Ive been trying to do this assignment for more than 3 days now... yeah I tried to do what you told me but now I do not even know if i did what you told me or not... the reason? well i changed a lot of things copied, deleted and even used some "brute force" that will be corrected once i get to know how to get all the characters from a variable(name, that converts lower cases to uppercases )to then print it in a new file?
It is printing only the first letter of my name to the new file. e.g
Old file:
Mario

new file:
M

I mean something should be correct to print something in the new file but something should be so WRONG to do not copy all the characters =?

heres my edit.....
//////////////////////////////////////////////////////////////////////////////////

#include<iostream>
#include<cctype>
#include<fstream>
#include <string> 
#include<iomanip>

using namespace std;

int openfile(char & ch,char name,ifstream & fin);
void process(char name,char ch,ofstream & fout);


int main()
{
	char ch;
	char name=1;
	ifstream fin;
	ofstream fout;
 
	openfile(ch,name,fin);
	process(name,ch,fout);

	system("pause");
	return 0;
}
int openfile(char & ch,char name,ifstream & fin)
{

	ifstream infile("infile.txt");
	//opens the file
	if(!infile.good()) throw "I/O error";
	while(!infile.eof()) 
	{
		infile.get(ch);
		fin>>ch;	
		name=toupper(ch);	
		cout<<name;
	}
	
fin.close();
	
return name;
}//reads from a previously created file that contains my name and lastname

void process(char name,char ch,	ofstream & fout)
{	ifstream fin;
	ifstream infile("infile.txt");
	fout.open("outfile.txt");
	if(!fout.good()) throw "I/O error";
			infile.get(ch);
		fin>>ch;	
		name=toupper(ch);	
			fout<<(name=toupper(ch));

		
	fout.close();
		
}//this part creates the output file and should write my lastname but this time it should be in UPPER cases


//closes all the files.

Okay, you're still a little off base here. I'm not understanding why you need two functions, openFile and process. I would not read input in one character at a time. I would use fin.getline() to get the whole line and then process the string "in place". You'd need to step through the string because toupper only works on one character at a time. I would forgo the openFile function. Create one ifstream (still not sure why you have 2 in the process function). Read the string in, process it, and write it out in one fell swoop. That way you don't have to even pass in the fstreams, just create them locally in the function.

Hi jonsca I finally finished it ... thank you and thanks achieve_goals for the help. I couldnt go to college the day the prof. explained this topic because I was selling my old baby(aka car bmw 325 1987). the prof. usually uses handouts rather than the book because the book is way too basic and its used as reference rather than to resolve tasks and complicated stff. I had to research everything myself so thats the main reason It took me this long :D. thanks again-

what I did was to write a single while loop using && to use conditions for both files instead of passing information from while loop to the other while loop via variables and so on...
so the while loop reads from infile and writes back to outfile.

Mario G.

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.