int main()
{
char FileName;
int Count, AmountRandNum;
long double FileWrite;
ofstream Fout;
cout<<"Please enter a filename."<<endl;
cin>>FileName;
cout<<endl<<"Please insert an amount of numbers to be written to file."<<endl;
cin>>AmountRandNum;
srand(time(0));
FileWrite = rand()%99999+1;
Fout.open(FileName);
for(Count=1;Count<=AmountRandNum;Count++)
{
Fout<<FileWrite;
}
cout<<"The "<<AmountRandNum<<" numbers are written in "<<FileName<<"."<<endl;
return 0;
}
The error is E2285 Could not find a match for 'ofstream::open(char' in function main
FileName is a char. This function does not accept a single char. It wants a char pointer.
You seem to be trying to store an entire filename in a single char. Let's examine that carefully. How many chars can be stored in a single char? One. So what happens when you try to store lots of chars in a single char? Nothing good.
I suggest you stop using char and use proper C++ strings.
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <string>
#include <ctime>
usingnamespace std;
int main()
{
string FileName;
int Count, AmountRandNum;
longdouble FileWrite;
ofstream Fout;
cout<<"Please enter a filename."<<endl;
cin>>FileName;
cout<<endl<<"Please insert an amount of numbers to be written to file."<<endl;
cin>>AmountRandNum;
srand(time(0));
FileWrite = rand()%99999+1;
Fout.open(FileName.c_str());
for(Count=1;Count<=AmountRandNum;Count++)
{
Fout<<FileWrite;
}
cout<<"The "<<AmountRandNum<<" numbers are written in "<<FileName<<"."<<endl;
return 0;
}
Thanks for the help. I asked my one friend that has class with me and he said that I can also just say:
char FileName[30];
it works. But I have another problem that the random numbers that are written to the user imputed file are all the same. Why is that? It isn't supposed to be like that. Can someone help. Even if I use:
Well, of course they are - you're only setting FileWrite to a random number once during your output. At what point during your for loop does a different random number get generated? :) The answer is: never!
So, modify your code a bit so that you're creating a new random number each time your program outputs an additional line.
Also, add an <<endl after Fout<<FileWrite to make it Fout<<FileWrite<<endl; , otherwise all your numbers will be on the same line. :P
I dont know how to do that. How do i make it to generate a new number every time it is written to the file? it worked fine with my friend's program. Why doesnt it wana work with mine. He just did something a bit different.
#include <iostream>
#include <stdlib>
#include <fstream>
usingnamespace std;
int main()
{
//Declare variables
int AmountRandNum, Count, Random;
ofstream Fout;
char FileName[30];
char Line[30000];
//Asks the user to input a filename not longer than 30 characters to write
//the random numbers to.
cout<<"Please enter a filename not longer than 30 characters."<<endl
<<"If the file does not exist then it will be created."<<endl;
cin>>FileName;
//Now ask the user for the amount of random numbers that must be generated
cout<<"Please enter the amount of random numbers that must be generated."
<<endl<<"You can use anything between 1 and 5000 numbers."<<endl;
cin>>AmountRandNum;
if ((AmountRandNum >= 1) && (AmountRandNum <= 5000))
{ //Beginning of if statement
//Opens and creates the file
Fout.open(FileName);
//Loops the amount entered 5000 times
for (Count = 1; Count <= AmountRandNum; Count++)
{//Beginning of for loop
Random = rand()%20 + 1 ;
Fout << Random << " ";
}//End of for loop
}//End of if statement
else
{//Beginning of else statement
cout << "Invalid entry! The number entered must between "
<< "1 and 5000!"<< endl;
}//End of else statement
//Tell the user that the random numbers is written in the file
cout<<"The "<<AmountRandNum<<" numbers are written in "<<FileName<<"."<<endl;
return 0;
}
Possible suggestion: rand() isn't necessarily random. We need the stand(time(NULL)) command to reseed a (pseudo)random number. When not using this, rand() will remain the same every time it's called.
Do not take Whovian's advice (no offense Whovian). You should only seed your random number generator once, and that's it. Seeding your RNG more than once will create a mathematical anomoly where random randomness turns out to be very unrandom. :)
rand() will generate a new number based on this seed each time it's called. Therefore, just make sure you're calling rand() to generate this new number before you try displaying it on the screen. Your friend's "small difference" makes a huge difference when it comes to displaying the data. :) Take a look at line 36 on his code and tell me the fundamental difference between that, and what your code is doing.