Hal 3000 Code#include <stdio.h>
#include <stdlib.h> #include <string.h> #include <time.h> void delay(int milliseconds); int main() { char Name[25]; char PlayerChoice; srand(time(NULL)); int R = rand(); printf("What is your name? "); scanf(" %s",Name); printf("Hello %s, My name is HAL3000. Please pick which program to activate......\n",Name); delay(1000); printf("Pick A, B, or C... "); scanf(" %c",&PlayerChoice); if(PlayerChoice=='B'){ int BaconNumber; printf("The HAL 3000 Bacon Printer, good choice."); delay(1000); printf("How much bacon would you like would you like? "); scanf(" %d",&BaconNumber); delay(1000); printf(" %d ....Sorry the Bacon Printer is broken try again later!\n",BaconNumber); delay(2000); } else if(PlayerChoice=='A'){ printf("Thank you for choosing the HAL3000 Temperature Converter!\n"); delay(1000); printf("Enter Temperature in Celsius. "); int C; scanf("%d",&C); int F = C*9/5 + 32; delay(1000); printf("%d degrees Fahrenheit\n",F); delay(2000); } else if(PlayerChoice=='C'){ printf("You have picked the HAL3000 Mail Check Program. \n"); delay(2000); printf("You 1 new message!\n"); delay(1000); printf("......\n"); delay(1000); printf("Hello %s, Make sure to leave the ship before 12:00pm because the ship will be \ngoing into self destruct mode for salvaging and testing purposes!\n", Name); delay(1000); printf(".....\n"); delay(1000); printf(" %s, It's 11:59am you have 15 seconds to evacuate... It was nice knowing you %s !!\n",Name,Name); delay(2000); } else{ printf("Error character is unknown to HAL3000!!!\n"); delay(2000); } printf("Thank you for using HAL3000 have a nice day!\n"); return 0; } void delay(int milliseconds) { long pause; clock_t now,then; pause = milliseconds*(CLOCKS_PER_SEC/1000); now = then = clock(); while((now-then)<pause) now = clock(); } |
Hal 3000 DetailsThis code was one of my favorites to build. The code uses a delay function, strings, and if statements. The point of this code is to have a conversation with the computer. The code starts with asking the user to enter their name. The char Name[25] records the name after being entered. The [25] means that the name can be 25 letters long. After entering your name the computer says hello and then recalls your name where the %s are located. You will see between lines in the code there are delays. These are here so the text on the screen appears more smoothly instead of all popping up at once. The computer will ask you to pick A , B, or C. Depending on which letter you pick the computer will run a different conversation/program. This is where the if statements come in to play. If you enter something besides the given options A, B , and C the code will skip to the else statement and tell you that it is an unknown answer. The void delay at the bottom of the code is the function used to run the delay feature in the code. This code also requires the #include <string.h> and #include<time.h> for the char Name[] and delay function to work.
Real Time ApplicationsThis code can be used for many things because of its if statement set up. One of its best uses could be for a text base game like "Hitch Hikers Guide to the Galaxy". It can also be used to combine other projects together into one system. In the code you can see I used my temperature converter as one of the options.
|