If command practice

This is a discussion on If command practice within the C++ Programming forums, part of the General Programming Boards category; >Main returns an int: Are you in the right thread?...

  1. #16
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >Main returns an int:
    Are you in the right thread?

  2. #17
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    I made some minor changes to the example I posted (changed variable names, added an extra call to rand, since the first number is always low on my machine (probably due to the seed used for srand()).
    Code:
    #include <cstdlib>
    #include <ctime>
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
       int guess;
    
       //Seed the pseudo random number generator.  Do this only once.
       srand(time(NULL));
       //Throw away first number generated (always low on my machine)
       int number = rand();
    
       //Generate a number between 1 and 100
       number = static_cast<int>(1 + rand() / (RAND_MAX + 1.0) * 100);
    
       do {
          cout<<"Please input a number: " << flush;
          cin>> guess;
          cin.ignore();
          cout<<"You inputted: "<< guess << "\n";
          cin.ignore();
    
          if ( guess < number ) 
             cout<< " You are too low\n";
          else if ( guess > number ) 
             cout<< " You are too high\n";
       } while (guess != number);
                  
       cout<< " Congrats you win!!\n";
         
       cin.get();
    }

  3. #18
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    >> Main returns an int
    Perhaps you are referring to the lack of a return 0? That is not necessary for the main function. Zero is returned automatically if the function ends without a return statement.

  4. #19
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >Perhaps you are referring to the lack of a return 0?
    I hadn't thought of that, I bet that's to what Ideswa indeed was referrring. I normally add the return 0; also just for consistency.

  5. #20
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Perhaps you are referring to the lack of a return 0?
    I was trying to get him to say it explicitly. Vague good advice is no better than bad advice.
    My best code is written with the delete key.

  6. #21
    Registered User
    Join Date
    Aug 2006
    Posts
    15
    Yea thanks for the catch, i forget to return, but i should just start doing it and remembering it. Also anyone have any sites or tutorials on small games that are used in examples. Most books like my textbook C++ learning by deitel, gives me some stupid gradebook example which I hate.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21