Thread: Random Line (pls comment)

  1. #1
    Registered User sikamikaniko's Avatar
    Join Date
    Mar 2003
    Posts
    28

    Post Random Line (pls comment)

    /*
    Random Line 1 - The program copies random line from one file in other.
    It is good for servers, where You need a random message to pop-up.
    Programming by sikamikaniko
    */
    #include<iostream.h>
    #include<fstream.h>
    #include<time.h>
    #include<stdlib.h>

    int countln(int ln); // functions prototype

    ofstream ptr_write; //pointer to the file, the text will be written
    ifstream ptr_read; //pointer to the file, the program will read from

    // main function ---

    main()
    {
    int i_ctrln=0;
    int ln=0;
    int i_gotoln=ln;
    char c_ctrln;
    char c_joke;


    ln=countln(ln); //
    srand(time(NULL)); //randomizacion
    i_gotoln=rand()%ln; //

    ptr_read.open("jokes.txt", ios::in);
    while(ptr_read.get(c_ctrln)) // finds the line, that must be copied
    {
    if(c_ctrln=='\n')
    {
    i_ctrln++;
    }
    if(i_ctrln==i_gotoln)
    {break;}

    }
    ptr_write.open("joke.txt", ios:ut);
    while(ptr_read.get(c_joke)) // copies the line in jokes.txt
    {
    cout << c_joke;
    ptr_write << c_joke;
    if(c_joke=='\n')
    {
    break;

    }
    }

    ptr_read.close();
    ptr_write.close();
    return 0;
    }

    // Counter ---

    int countln(int ln) // counts the lines in jokes.txt
    {
    char c_ctrln;
    ptr_read.open("jokes.txt", ios::in);
    while(ptr_read.get(c_ctrln))
    {
    if(c_ctrln=='\n')
    {
    ln++;
    }
    }
    ptr_read.close();
    ln++;
    return (ln);
    }

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    comment: use code tags to get more comments

    gg

  3. #3
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    Firstly, don't use global variables when you don't need to.

    Why not use getline() instead if the line length is not going to exceed a certain length.

    Also, there is a bug in your code. I tried it and when it printed the line to the ouput file, it didn't print the first character of the line. This only happens if it prints the first line of the input file.

  4. #4
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Code:
    #include<iostream.h>
    #include<fstream.h>
    #include<time.h>
    #include<stdlib.h>
    Should be:

    Code:
    #include <iostream>
    #include <fstream>
    #include <ctime>
    #include <cstdio>
    
    using namespace std;
    Code:
    main()
    {
    That's a C-style main function. In C++, main always returns an int.

    Code:
    int main( void )
    {
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  5. #5
    Registered User sikamikaniko's Avatar
    Join Date
    Mar 2003
    Posts
    28

    Cool Thank's all :)

    but I don't have any problems with running the program. anyway, thanks 4 the advices

  6. #6
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    That's a C-style main function. In C++, main always returns an int.
    In C main() returns integer always just as in C++.

  7. #7
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    >>In C main() returns integer always just as in C++.

    Should have been more clear. In C, it is implicit, while it must be explicitly stated in C++.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  8. #8
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    Well, trying to compile a code without using int main, a C code:
    [itzick@localhost Desktop]$ gcc prove.c -o prove
    prove.c: In function `main':
    prove.c:3: warning: return type of `main' is not `int'
    [itzick@localhost Desktop]$
    Same when I compile a C++ code

  9. #9
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    "Implicitly returns int", is old school. But to be backwards compatable, only a warning is issued by gcc.

    gg

  10. #10
    Registered User sikamikaniko's Avatar
    Join Date
    Mar 2003
    Posts
    28

    Question ok

    so, in this case, how should return look like?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help again with scrolling without wrapping
    By Dukefrukem in forum C Programming
    Replies: 8
    Last Post: 09-21-2007, 12:48 PM
  2. Random line from txt file
    By dogbert234 in forum C++ Programming
    Replies: 5
    Last Post: 06-01-2005, 02:05 AM
  3. Can someone help me understand this example program
    By Guti14 in forum C Programming
    Replies: 6
    Last Post: 09-06-2004, 12:19 PM
  4. Pls comment some lines for me.
    By Nutshell in forum C Programming
    Replies: 0
    Last Post: 01-07-2002, 08:51 PM
  5. Best way to generate a random double?
    By The V. in forum C Programming
    Replies: 3
    Last Post: 10-16-2001, 04:11 PM