Results 1 to 9 of 9

Thread: Problems with this thing?

  1. #1

    Thread Starter
    Fanatic Member prog_tom's Avatar
    Join Date
    May 2001
    Location
    Los Angeles and Little Rock
    Posts
    810

    Post 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

  2. #2
    Fanatic Member Wynd's Avatar
    Join Date
    Dec 2000
    Location
    In a bar frequented by colossal death robots
    Posts
    772
    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.

  3. #3

    Thread Starter
    Fanatic Member prog_tom's Avatar
    Join Date
    May 2001
    Location
    Los Angeles and Little Rock
    Posts
    810

    Post 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

  4. #4
    Fanatic Member Wynd's Avatar
    Join Date
    Dec 2000
    Location
    In a bar frequented by colossal death robots
    Posts
    772
    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.

  5. #5

    Thread Starter
    Fanatic Member prog_tom's Avatar
    Join Date
    May 2001
    Location
    Los Angeles and Little Rock
    Posts
    810

    Post 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

  6. #6
    Zaei
    Guest
    As an alternative to itoa(), you can use sprintf:
    Code:
    sprintf(PATH, "c:\\%i.txt", k);
    Z.

  7. #7
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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.

  8. #8
    Fanatic Member Wynd's Avatar
    Join Date
    Dec 2000
    Location
    In a bar frequented by colossal death robots
    Posts
    772
    What library is sprintf() in?
    Alcohol & calculus don't mix.
    Never drink & derive.

  9. #9
    Zaei
    Guest
    stdio.h

    Z.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width