I tried to put some decimal numbers and save it as a binary numbers in the hard drive .rft file.I wrote the code but somehow its not works. Any help for that?
#include <iostream>
usingnamespace std;
int binary(int);
int main(void) {
int number;
ofstream MosFile("Milloin_two.rtf");
if(MosFile.is_open()) {
cout<<"ok the file is open"<<endl;
}
else{
cout<<"Its messed up"<<endl;
}
cout<<"Enter a positive integer:";
cin>>number;
for(int i=1;i<=number;i++)
{
MosFile<<MosFile.binary(i)<<"\n"; // I wanted to use binary function to convert decimal to binary number. MosFile for using binary function.
}
MosFile.close();
}
int binary(int number) {
ofstream MosFile;
int rem;
if(number <= 1) {
cout << number;
return;
}
rem= number%2;
binary(number>> 1);
MosFile<<rem;
}
Jumper is correct. std::ofstream doesn't have a method (a function that's associated with a class/struct) called binary(). Instead, it's a global function that needs to be invoked without being qualified with a class name.
for( int i = 0 ; i < number ; i++ )
{
MosFile<<binary( number )<<"\n" ;
}
//this is the binary function .
1 2 3 4 5 6 7 8 9 10 11 12 13 14
int binary(int number)
{
if( number <= 1 )
{
cout << "Number ="<number ;
return 0 ;
}else
{
int rem = number %2 ;
binary ( number >> 1 ) ;
return rem;
}
}
I am not able to understand why you are using this recursive function binary ( number > > 1 ) over here , what will be the funtionality . of this or what does it exactly do
When void is used in this context, it indicates explicitly that the function doesn't require any arguments to be passed to it prior to its invocation. In C++, a set of empty parentheses mean exactly the same thing.
First, you check to see if the opening of the file fails. However, if the opening fails, you don't handle it appropriately. Regardless, you continue to write to a file that may be non-existent.
Also, in function binary(), I would parenthesise this expression: number %2. Finally, why do you invoke binary() and ignore the value it yields?
The file is opened. Now, its running but not doing the same what I needed to do. I want to put some decimal numbers using for loop. Say, I put 13. Then, it will take 1,2,3,4,5,...,13 & converts them to binary numbers that will be saved in a file in hard drive. The output in cmd is now:
1 2 3 4 5 6 7
ok the file is open
Enter a positive integer:5
Number =1Number =1Number =1Number =1Number =1Number =1Number =1Number =1Number =
1Number =1Number =1Number =1Number =1Number =1Number =1Number =1Number =1Number
=1Number =1Number =1Number =1Number =1Number =1Number =1Number =1
Process returned 0 (0x0) execution time : 9.078 s
Press any key to continue.
The file Milloin_two.rtf was also created but theres not expected output. Any suggestion from you ?
You cannot declare int i two times in a loop!! (sic)
Actually, you can. The compiler will assume the innermost i variable.
@arefe. You're still doing it. You're not handling a file opening failure correctly. It should resemble something like this:
1 2 3 4 5 6 7
if(!MosFile.is_open())
{
// The file failed to open. We cannot write to the file.
std::cout << "Fatal: The file failed to open." << std::endl;
return(1);
}
Also, the innermost loop is pointless. You need to remove it. If you don't, in total, your loops will loop 25 times.
O.o My teacher always told me to stop using the same variable two times in loop (int i). She said that the compiler will return errors. Can you explain that in detail please ? Or give me a link ?
And, I also have a thing that I'm not sure about. I know the difference between ++a, and a++.
But what would be the difference if instead of i++ i would write ++i (in a for loop) ?
1 2 3 4
for (int i=0; i<=n; ++i)
{
//for loop will start from i=1 ? or from i=0 ?
}
Here's how it works: For each scope created, a new stack frame is created on the program's stack. Each stack frame has it's own variables, function calls, etc. A for loop is no different. Now, here's our two loops:
1 2
for(int X(0); ...)
for(int X(0); ...);
OK, now. Two stack frames have been created here: 1 for the innermost, and 1 for the outermost. Both stack frames have their own X variable pushed onto them. Since the innermost loop is the most recent stack frame, the compiler will look into it, and will see X there, and it'll use that. It won't search any further, because what's the point is searching for something that you've found? I guess you could rename the innermost X to disambiguate it.
jumper007 wrote:
But what would be the difference if instead of i++ i would write ++i (in a for loop) ? (sic)
There's no difference unless the expression has more than two operands/sub-expressions, like so:
for(int X(0); ... ; ((++X) + 1))
The above expression within the increment section is when the post-fix/pre-fix ++/-- matters.