PDA

Click to See Complete Forum and Search --> : Problems with this thing?


prog_tom
Nov 25th, 2001, 05:41 PM
#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.

Wynd
Nov 25th, 2001, 05:47 PM
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.

prog_tom
Nov 25th, 2001, 05:51 PM
Can u try to help me out?

Wynd
Nov 25th, 2001, 05:59 PM
Yes, but only if you promise to indent, format, and otherwise make readable your code from now on ;)#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;
}

prog_tom
Nov 25th, 2001, 07:37 PM
I would like to get Randomized Numbers without using
time(NULL);

Zaei
Nov 25th, 2001, 07:47 PM
As an alternative to itoa(), you can use sprintf:

sprintf(PATH, "c:\\%i.txt", k);


Z.

CornedBee
Nov 26th, 2001, 08:01 AM
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.

Wynd
Nov 26th, 2001, 06:38 PM
What library is sprintf() in?

Zaei
Nov 26th, 2001, 07:22 PM
stdio.h

Z.