Advertisement
Guest User

friendshipalgorithm

a guest
Nov 12th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.50 KB | None | 0 0
  1. // Das Hauptprogramm
  2. int main(){
  3.     int control = 0;            // First state
  4.     int nextYes, nextNo;        // To decide the state from the answer
  5.     bool getInput = true;       // So we can skip yes / no from the user to show different paths
  6.     string userInput;           // The cin from the user is written to this string
  7.  
  8.     cout << "The friendship algortihm!" << endl << endl;
  9.     while (control != 99){
  10.  
  11.         switch (control) {
  12.         case 0:
  13.             cout << "Place phone call!" << endl;
  14.             cout << "Is someone at home?" << endl;
  15.             nextYes = 2;
  16.             nextNo = 1;
  17.             break;
  18.         case 1:
  19.             cout << "Leave message!" << endl;
  20.             cout << "Wait for callback.." << endl;
  21.             getInput = false;
  22.             control = 0;
  23.             break;
  24.         case 2:
  25.             cout << "Hey would you like to share a meal some time?" << endl;
  26.             nextYes = 3;
  27.             nextNo = 4;
  28.             break;
  29.         case 3:
  30.             cout << "Dine together!" << endl;
  31.             control = 99;
  32.             getInput = false;
  33.             break;
  34.          case 4:
  35.             cout << "Do you enjoy a hot beverage?" << endl;
  36.             nextYes = 5;
  37.             break;
  38.          case 5:
  39.             cout << "What would you like to drink?" << endl;
  40.             bool correctInput = false;
  41.  
  42.             while (!correctInput)   // Loop while NOT a correct input ("!" means not)
  43.             {
  44.                 cout << "Choose between tea, coffee and cocoa!" << endl;
  45.                 cin >> userInput;
  46.                 if (userInput == "tea")
  47.                     correctInput = true;
  48.                 else if (userInput == "coffee")
  49.                     correctInput = true;
  50.                 else if (userInput == "cocoa")
  51.                     correctInput = true;
  52.             }
  53.             cout << "Have " << userInput << endl;
  54.             control = 99;
  55.             break;
  56.  
  57.  
  58.         // If we need a new input we ask for it
  59.         if (getInput)
  60.         {
  61.             cin >> userInput;
  62.             if (userInput == "yes")
  63.             {
  64.                 control = nextYes;
  65.             }
  66.             else if (userInput == "no")
  67.             {
  68.                 control = nextNo;
  69.             }
  70.             else
  71.                 cout << "Please write yes or no!" << endl;
  72.         }
  73.  
  74.         getInput = true;    // Enables input for next question if needed
  75.     }
  76.     }
  77.  
  78.     cout << "Begin friendship! <3" << endl;     // The goal <3
  79.  
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement