hi everyone = )

i wrote program using C++
it's for type a small letter then it will print it back in a capital letter using file processing
i wrote the letters in txt file in the same c++ folder like this way

a A
b B
c B

and so on ...

this is the program

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

int main() 
{
char letter[1];
ifstream file("let.txt",ios::out);

file.close();

ofstream file2("let.txt",ios::in);
if (!file)
{
cout << "Can not Open The File";
exit(1); 
}

cout<<"Enter a Letter to Type it as a Capital Letter\n";
cin>>letter;

while (file >> letter)
{
cout<<"enter new letter";
}


cout << "The Capital Letter is :" << file << endl; 
return 0;
}

it's print just zeros as out put ..

what's the error with it

thank u for helpin me

For starters

char letter[1];
ifstream file("let.txt",ios::out);
 
file.close();
 
ofstream file2("let.txt",ios::in);

The variable "letter" doesn't need to be an array, get rid of the "[1]" part. You have your file streams configured wrong. An ifstream should be configured for ios::in and an ofstream should be configured for ios::out. Also, you close your input file immediately after opening it.

Next:

if (!file)
{
cout << "Can not Open The File";
exit(1); 
}
 
cout<<"Enter a Letter to Type it as a Capital Letter\n";
cin>>letter;

Most of this looks okay to me. Your conditional should be something more like if (!file.is_open()) though. That would have flagged your file close error that I mentioned earlier.

And now this:

while (file >> letter)
{
cout<<"enter new letter";
}
 
 
cout << "The Capital Letter is :" << file << endl; 
return 0;
}

There really isn't anything useful here. 1. How do you expect to get a valid input from a closed file? Refer back to the previous 2 sections. 2. Outputting directly from a pointer (file) rarely comes out correctly. 3. How do you expect the user to "enter new letter" if you don't provide an input statement?

Honestly, I think you should rework the whole program from scratch. You are performing various actions attemtping to satisfy little pieces of the required actions here and there, but your overall algorithm is highly disorganized. Start by focusing on obtaining your console input and outputting it to a file. Then, focus on converting that file to an input file and displaying the characters you read from the file. Once you have those 2 operations figured out, and functioning smoothly, add the data-manipulation functions you need to complete the assignment.

line 8: ios::out is not valid for ifstream. ifstream is inut stream, not output. Similar for line 13. You have the two backwards. You don't need either ios::in or ios::out because ifstream is always input and ofstream is always output. For tet files the only parameter that is needed is the filename. ifstream in("Textfile.txt"); line 23: since file is an ofstream you have to use << operator, not >>.

line 25: you need to add '\n' to move the cursor to the next line on the screen, otherwise the output will look pretty crappy.

You also need another cin between lines 25 and 26 so that you can enter another letter.

[edit]^^^ also what he said.

In addition to what's already stated above, I'd like to suggest a beginner-friendly C++ Tutorial.

Fbody

thank u for ur advices =)
i mean by [1] length of character not array
any way i changed it to string

when i made some changes it gave me about 47 errors :@
this is the first one

c:\myfile\n\n\n.cpp(32) : error C2784: 'std::basic_istream<_Elem,_Traits> &std::operator >>(std::basic_istream<_Elem,_Traits> &,const std::_Smanip<_Arg> &)' : could not deduce template argument for 'std::basic_istream<_Elem,_Traits> &' from 'std::ofstream'

i found that when i post letters in txt file
it's automatically cleared :-O

Ancient Dragon
i use this way becouse it's the only way i learned becouse we're beginners
i changed what did u said to me
thanks for helping me :)


mitrmkar
nice advice thank u
it's useful website

now this the new mad version
i know it has alot of troubles

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

using namespace std;

int main() 
{
    string letter;

	ofstream file("let.txt",ios::out);
    
	

	ifstream file2("let.txt",ios::in);
    if (!file.is_open())
	{
        cout << "Can not Open The File";
        exit(1); 
    }
    
	cout<<"Enter a Letter to Type it as a Capital Letter\n";
	cin>>letter;

    while (file >> letter)
	{
        cout<<"Enter New Letter\n";
		cin>>letter;
    }
    

	cout << "The Capital Letter is :" << file << endl; 
    return 0;
}
while (file >> letter)
	{
        cout<<"Enter New Letter\n";
		cin>>letter;
    }

This loop should be based on your console input, not your file input. How do you expect to get INPUT from an OUTPUT FILE??? This is why the compiler is screaming at you, and also what AD said something to you about. Input and Output streams are relative to your program, NOT the stream itself.

What you need to do is read the letter in from the console (extraction operator '>>' relative to cin), write the letter to your output file (injection operator '<<' relative to file), read the letter from the input file (extraction operator '>>' relative to file2), write the formatted letter to the console (injection operator '<<' relative to cout).

cout << "The Capital Letter is :" << file << endl;

Honestly, I don't think you listened to any of us. We've already told you this would not work the way you want.

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.