If you don't understand pointers then compile this and see what it does. If you didn't have a pointer, every guess the random number would change making it harder to get it right. I made this thread because my friend didn't know what the purpose of pointers was and I thought it was a good idea to share with everyone.
#include <stdlib.h>
#include <iostream.h>
int main()
{
? ? short guess;
? ? short number = rand()%8; ?/* Creates a random # 1-8 */
? ? short* pNumber = &number; ? ?/* Creates the Pointer */
? ? do
? ? {
? ? ? ? ?cout << "Guess a # 1 - 8: ";
? ? ? ? ?cin >> guess;
? ? ? ? ?if(guess == *pNumber) ? /* If the guess is the first random # generated in variable number */
? ? ? ? ?{
? ? ? ? ? ? ? cout << "\n\nCorrect!\n";
? ? ? ? ? ? ? return 0;
? ? ? ? ?}
? ? }
? ? while(guess != *pNumber);
}
Question
Ultra Frosty
If you don't understand pointers then compile this and see what it does. If you didn't have a pointer, every guess the random number would change making it harder to get it right. I made this thread because my friend didn't know what the purpose of pointers was and I thought it was a good idea to share with everyone.
#include <stdlib.h> #include <iostream.h> int main() { ? ? short guess; ? ? short number = rand()%8; ?/* Creates a random # 1-8 */ ? ? short* pNumber = &number; ? ?/* Creates the Pointer */ ? ? do ? ? { ? ? ? ? ?cout << "Guess a # 1 - 8: "; ? ? ? ? ?cin >> guess; ? ? ? ? ?if(guess == *pNumber) ? /* If the guess is the first random # generated in variable number */ ? ? ? ? ?{ ? ? ? ? ? ? ? cout << "\n\nCorrect!\n"; ? ? ? ? ? ? ? return 0; ? ? ? ? ?} ? ? } ? ? while(guess != *pNumber); }Link to comment
Share on other sites
37 answers to this question
Recommended Posts