⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tut2-1.cpp

📁 good tutorial examples of c++
💻 CPP
字号:
// *****************************************
//        cplusplus language tutorial
//                section 2.1
//
//            "Guess the number"
// Shows:
//   - do-while
//   - if-else
//
// Briefing:
//  The computer generates a random number
//  between 1 and MAX_RANGE. The user must
//  guess the number. Each time the user
//  makes an attempt the computer tells if
//  the number is greater or less than.
// *****************************************

#include <iostream.h>
#include <stdlib.h>
#include <time.h>

// Define the greatest possible value:
#define MAX_RANGE 100

main ()
{
  int counter=0;
  long value,input;

  srand ( time (NULL) );         // Initialize random generator
  value = rand()%MAX_RANGE+1;    // Get random between 1 and MAX_RANGE

  cout << "\nInsert a value between 1 and " << MAX_RANGE << " : ";

  do {

    cin >> input;                // Get user input
    counter++;                   // increase attempts counter

    if (value>input)             // is value grater than the input?
      cout << "Value is greater than " << input << ". Try again : ";

    else if (value<input)        // if not, is it less?
      cout << "Value is less than " << input << ". Try again : ";

    else {                       // else it is the correct number!
      cout << "That's right! Value was " << input;
      cout << "\nYou have needed " << counter <<" attempts.";
      }

  } while (value!=input);
  return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -