|
-
Nov 25th, 2001, 06:41 PM
#1
Thread Starter
Fanatic Member
Problems with this thing?
Code:
#include <iostream>
#include <fstream>
#include <math.h>
#include <time.h>
using namespace std;
#define MAX 256
FILE *f;
ofstream VB;
char I;int x,k;char p;
int main(){
x=time(NULL);
srand(x);
k=rand()%1000+1; strcpy(p,k); // variable 'k' holds the random number, p holds nothing.
char PATH[MAX];
strcpy(PATH,"c:/");
strcat(PATH,p);
strcat(PATH,".txt");
VB.open(PATH);
VB<<k;
VB.close();
cout<<PATH<<endl;
cout<<x<<endl;
cout<<k<<endl;
system("pause");
x=NULL;k=NULL;
return 0;
}
I don't know why I cannot convert char to const char. See it for yourself.

prog_tom
JOIN THE REVOLUTION!!!! Dual T3 backedup science community.
http://physics.sviesoft.com/forum
-
Nov 25th, 2001, 06:47 PM
#2
Fanatic Member
What are you talking about? Strcpy() won't work because both parameters have to be C strings (char[]). You can't use it with an int and a char.
Alcohol & calculus don't mix.
Never drink & derive.
-
Nov 25th, 2001, 06:51 PM
#3
Thread Starter
Fanatic Member
Help me out?
Can u try to help me out?

prog_tom
JOIN THE REVOLUTION!!!! Dual T3 backedup science community.
http://physics.sviesoft.com/forum
-
Nov 25th, 2001, 06:59 PM
#4
Fanatic Member
Yes, but only if you promise to indent, format, and otherwise make readable your code from now on 
Code:
#include <iostream>
#include <fstream>
using namespace std;
#include <cstdlib>
#define MAX 256
int main()
{
// Declare variables
ofstream VB;
char I;
int x, k;
char p[10], PATH[MAX];
srand(time(NULL)); // Initialize random number generator
k = (rand() % 1000) + 1; // Assign k a random number from 1 to 1000
itoa(k, p, 10); // Convert k to a C string and store it in p
// Create path
strcpy(PATH,"c:/");
strcat(PATH,p);
strcat(PATH,".txt");
// Write number to file
VB.open(PATH);
VB<<k;
VB.close();
//Print out numbers
cout<<PATH<<endl;
cout<<x<<endl;
cout<<k<<endl;
// End
system("pause");
return 0;
}
Alcohol & calculus don't mix.
Never drink & derive.
-
Nov 25th, 2001, 08:37 PM
#5
Thread Starter
Fanatic Member
Without Time
I would like to get Randomized Numbers without using
time(NULL);

prog_tom
JOIN THE REVOLUTION!!!! Dual T3 backedup science community.
http://physics.sviesoft.com/forum
-
Nov 25th, 2001, 08:47 PM
#6
As an alternative to itoa(), you can use sprintf:
Code:
sprintf(PATH, "c:\\%i.txt", k);
Z.
-
Nov 26th, 2001, 09:01 AM
#7
srand(time(NULL)) initializes the random number generator. If you do not write this (you don't have to) your random numbers will always start at the same point.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Nov 26th, 2001, 07:38 PM
#8
Fanatic Member
What library is sprintf() in?
Alcohol & calculus don't mix.
Never drink & derive.
-
Nov 26th, 2001, 08:22 PM
#9
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|