Problem with a filewrite

I have a program thats not working too well. It keeps in giving me an error.

#include <stdlib>
#include <iostream>
#include <fstream>

using namespace std;

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
Fout.open(FileName);

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <string>
#include <ctime>


using namespace std;

int main()
{
string 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.c_str());

for(Count=1;Count<=AmountRandNum;Count++)
{
Fout<<FileWrite;
}
cout<<"The "<<AmountRandNum<<" numbers are written in "<<FileName<<"."<<endl;
return 0;
}
Last edited on
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:

#include <time.h>

so what is the problem?
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

Happy coding
it works


Until you have a filename with more than 30 characters in it. Then it breaks.
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <iostream>
#include <stdlib>
#include <fstream>

using namespace 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.
go look in the previous code there I have a srand(time(NULL)) and it still doesnt work.

in the second program it generates random numbers that differ the whole time. Why does it work there?
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.
I think I see it now! His rand() thing is inside his for loop! and mine aint? is it the correct difference?
It works now! thanks man!
Bingo! :) No problem dude - glad I could help.
Topic archived. No new replies allowed.