Thread: Major Noob help

  1. #1
    Registered User
    Join Date
    Jun 2005
    Posts
    4

    Major Noob help

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        cout<<"HEY, you how ya doin' bro.\n";
        cin.get();
        
        return l;
    }
    I'm having a problem with this compiling in the return line.(line 10)
    This is what it says `l undeclared (first use this function) (Each undeclared identifier is reported only once for each function it appears in.)
    Thanks

  2. #2
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    I am guessing you put an lower case L and not the number 1 in your return, although typically to indicate that your program did not encounter any errors, you should return 0. (That's zero and not O)

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    4
    oh its a 1...that explains something...ya I thought a letter made no sense at all....

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    4
    ok here is another program that I made before I got frustrated and quit programming for a while...(I was starting my first year of college so I kinda didn't have any time for it anyways..drinking etc..became more important...) Anyways my goal is to try and understand what each line of code means again because I totally can't figure it out now.
    Code:
    #include <iostream>
    #include <cstdlib> //for atoi(), rand() and srand()
    #include <ctime> //for time() and stuff
    #include <cstring> //for strlen()
    #include <windows>using namespace std;
    
    bool isnum(const char * str) {
    bool r =true;
    
    for (int i =0; i< strlen(str);i++) {
    if (str[i] < '0' || str[i] > '9') r =false;
    }
    
    return r;
    }
    
    int main(int argc, char ** arg) {
    unsigned int Secret;
    unsigned int Guess;
    unsigned int max;int limit = (max/8)+12;
    Secret = (rand()%max)+1;
    int guesses = 1;
    
    time_t seconds;
    time(&seconds);
    srand((unsigned int) seconds);
    
    if (argc >= 2) {
    if (isnum(arg[1])) {
    max = atoi(arg[1]);
    } else {
    max = 1000;
    }
    } else {
    max = 1000;
    }
    
    Secret = (rand()%max) +1; // random number form 1 to max
    
    cout << "Try and Guess my number. Hint: It's from 1 to " << max<< endl;
    cin >> Guess;
    
    while (Guess != Secret && guesses < limit) {
    guesses++;
    if (Guess > Secret) {
    cout << "Too high.";
    } else {
    cout << "Too low.";
    }
    cout << endl << endl;
    cin >> Guess;
    }
    
    if (Guess != Secret) {
       cout << "You failed! It took you " << guesses
    << " guesses." << endl << endl << endl;
       } else {
       	 cout << "You guessed right! It took you " <<
    guesses << " guesses. " << endl << endl << endl;
    		}
    
    Sleep'(5000); //5 second wait		
    return 0;
    }
    Last edited by XShadow; 06-01-2005 at 01:27 PM.

  5. #5
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    Surely, you are not expecting someone to comment each line of your code? Maybe you should go back and indicate which lines you don't understand.

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    4
    lol I forgot to do that...I don't understand the stuff that is in red.

  7. #7
    Registered User
    Join Date
    Jun 2004
    Posts
    722
    First ident your code well

    Code:
    #include <iostream>
    #include <cstdlib> //for atoi(), rand() and srand()
    #include <ctime> //for time() and stuff
    #include <cstring> //for strlen()
    #include <windows> //-> you don't need this for sure!and it's windows.h
    
    using namespace std;
    
     //a function which receives a pointer to char, which is typically, a C-style string
    //an array of chars, and returns a boolean value - true or false...
    bool isnum(const char * str) {
        //this is the var you're going to return, lets assume the string is indeed a number
        bool r =true;
    
        //now loop the string - if you find a char that is not a number return false
        for (int i =0; i< strlen(str);i++) {
            if (str[i] < '0' || str[i] > '9')
                r =false;
        }
    
        return r;
    }
    A small but important recomendation !
    for (int i =0; i< strlen(str);i++) {

    Code:
    //this does N^2 iterations
    for(int i=0;i<strlen(str);i++)
    //this does 2*N iterations
    int len = strlen(str);
    for(int i=0;i<len;i++)
    //this does N iterations
    for(int i=0;str[i];i++) //str[i] != 0
    The rest of the code... for me some other time
    Last edited by xErath; 06-01-2005 at 02:15 PM.

  8. #8
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    atoi() converts C style string to an int
    rand() generated a (psuedo) random number
    srand() seeds the random number generator to try to prevent the same "random" sequence
    time() returns values that are "random" to seed the random generator with
    strlen() returns the number of non null char in a C style string
    #include <windows.h> I believe is where Sleep() is located
    argc is the number of parameters sent to the program by use of the command line
    arg[1] is the second argument sent to the program by use of the command line
    if arg[1] exits, then it becomes the value of max, otherwise the value of max defaults to 1000
    You're only born perfect.

  9. #9
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    Quote Originally Posted by elad
    srand() seeds the random number generator to try to prevent the same "random" sequence
    ...or to allow the same random sequence
    time() returns values that are "random" to seed the random generator with
    While your intentions may have been well, this is a very misleading statement. You are stating how time() is being used and not what it does

    The time function returns the number of seconds elapsed since midnight (00:00:00), January 1, 1970

Popular pages Recent additions subscribe to a feed