Thread: problem with a tutorial

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    3

    problem with a tutorial

    hi everyone, i am fairly new to c++ and in one of my first works i had to implement the method "rand()" and since i had no idea how to use it i went looking for a tutorial and found this website.

    the tutorial i am refering to is this one : http://www.cprogramming.com/tutorial/random.html

    so i started reading it and when i got to the code part i copied it and tried to compile it but i got this problem :

    ../main.cpp: In function `int main()':
    ../main.cpp:23: error: `cout' undeclared (first use this function)
    ../main.cpp:23: error: (Each undeclared identifier is reported only once for each function it appears in.)
    ../main.cpp:23: error: `endl' undeclared (first use this function)
    make: *** [main.o] Error 1


    line 23 refers to this : cout << rand() << endl;

    if you do not wish to go to the linked page here is the code :
    Code:
    #include <iostream>
    #include <cstdlib>
    #include <time.h>
    int main()
    {
    /*
    Declare variable to hold seconds on clock.
    */
    time_t seconds;
    /*
    Get value from system clock and
    place in seconds variable.
    */
    time(&seconds);
    /*
    Convert seconds to a unsigned
    integer.
    */
    srand((unsigned int) seconds);
    /*
    Output random values.
    */
    cout << rand() << endl;
    cout << rand() << endl;
    cout << rand() << endl;
    return 0;
    }
    any help will be much apreciated.
    thank you for the time spent reading my post
    Last edited by kUr4m4; 09-21-2006 at 07:29 AM.

  2. #2
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    The faq is better.

    For ease of use, just insert
    Code:
    using namespace std;
    just prior to the 'main' function.

    Alternatively and arguably preferably, prefix each of those points giving rise to undeclared errors with std:: eg std::cout, std::endl etc.

    You might want to change #include <time.h> to #include <ctime>, too.

    edit:

    The errors arise because the functions you are calling are declared within the scope of the std namespace. You access the contents of a namespace with either the using directive or by using the fully qualified namespace name. In the former case there are two approaches, the first brings everything into scope - for example, where I suggested 'using namespace std;' above that simply brings everything within the std namespace into current scope. The second use of the 'using' directive brings a single entitiy within the given namespace into scope, eg 'using std::cout' would brings cout into current scope ie make it usable in that form within that part of your program.

    There's more information about namespaces in this faq and this tutorial.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    3
    thank you very much, problem is fixed

    you might want to change the code in the tutorial i mentioned

  4. #4
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    you might want to change the code in the tutorial i mentioned
    One more thing you should be aware of -

    Whenever you see a standard C header with a dot-h suffix, you should add a c-prefix , and drop the .h like this:

    #include <time.h> // This is C
    #include <ctime> // This is C++

  5. #5
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    You know, you should go read up on the tutorials first before approaching these things. I routinely download code for Unix systems which don't compile on my Windows box, so I have to edit the code to make it work. If you don't even know that cout is part of std (which is what most of the errors are about actually) you should really go back to basics.

    In any case, I don't think RoD is still active around here, so it might be sacrilege to edit his craft Quzah would scream at the indentation though.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    3
    thanks for all your replies, as previously mentioned i am indeed still beginning c++ and very rusty with programming in general so please don't be to hard on me as i didn't want to "hurt" anybody's feelings

    anyway, have a nice day and thank you again for your help

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Tutorial problem (headers)
    By AsiNisiMasa in forum C++ Programming
    Replies: 1
    Last Post: 08-08-2005, 11:24 PM
  2. MSVC Tutorial problem - Monochrome Palette
    By colinH in forum C++ Programming
    Replies: 4
    Last Post: 10-30-2002, 03:57 AM
  3. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM
  4. binary tree problem - help needed
    By sanju in forum C Programming
    Replies: 4
    Last Post: 10-16-2002, 05:18 AM
  5. Problem running a Cboard C++ tutorial!
    By Monkey Liar in forum C++ Programming
    Replies: 6
    Last Post: 02-15-2002, 03:32 AM