Advertisement
Guest User

Session1GETS CHAR

a guest
Nov 23rd, 2014
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.37 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h> //cls clear screen
  3. #include <ctype.h>
  4.  
  5. #define MAX_ITEMS 3
  6. #define MAX_CHOICES 10
  7.  
  8. int Menu(char Items[][50], int Price[], int NoItems){ /*Declares the function prototype*/
  9.     //SHOW MENU
  10.     printf("------------- ITEMS IN STORE ---------------\n");
  11.     printf("    Item \t Price \n");
  12.     /*Creates a list with a variable number of items*/
  13.     int i = 0;
  14.     for (i = 0; i < NoItems; i++)
  15.     {
  16.         //aqui estas mostrando ITEM 0, ITEM 1, ITEM 2.
  17.         //tienes que mostrar desde 1
  18.         printf("%d - %s \t %d \n", i+1, Items[i], Price[i]);
  19.     }
  20.     //SELECT ITEM
  21.     printf("Please, select the number of the items you want from the menu \n");
  22.     int selection;
  23.     scanf("%d", &selection);
  24.     // si quiero el item 1
  25.     // escribo
  26.     // 1<enter>
  27.     // guarda el 1
  28.     // deja el <enter> en flujo
  29.     getchar();
  30.     // getchar se come ese enter que despues nos jode la leida de otro *caracter*
  31.     return selection-1;
  32. }
  33.  
  34. int stillBuying(char option){
  35.     return option == 'y';
  36. }
  37. int main(void)
  38. {
  39.     /*Creates the arrays for both Items and Price*/
  40.  
  41.     char Items[MAX_ITEMS][50] = { "Lamp", "Toothbrush", "Battery" };
  42.     int Prices[MAX_ITEMS] = { 450, 368, 99 };
  43.     int NoItems = MAX_ITEMS;
  44.     int NoBought = 0;
  45.     int choices[MAX_CHOICES];
  46.     char answer = '\0';
  47.     char respuesta[10] = "";
  48.     /*Calls the menu function*/
  49.     //noitems nos dice la cantidad de items en el vector
  50.     do
  51.     {
  52.             choices[NoBought] = Menu(Items, Prices, NoItems);
  53.             printf("Added a %s\n", Items[choices[NoBought]]);
  54.             printf("Do you want another item? (y/n) \n");
  55.             gets(respuesta);
  56.             if( toupper(respuesta[0]) == 'Y'){
  57.                 puts("i want to still buying items");
  58.             }
  59.             scanf("%c", &answer);
  60.             NoBought++;
  61.             //si quiero otro item, borro pantalla
  62.             if (stillBuying(answer)){
  63.                 system("cls");
  64.             }
  65.     }
  66.     while (NoBought < MAX_CHOICES && stillBuying(answer));
  67.     //MOSTRAR CARRITO DE COMPRAS
  68.     int i = 0;
  69.     int sum = 0;
  70.     for ( i = 0; i < NoBought; i++){
  71.         sum = sum + Prices[choices[i]];
  72.         printf("Item %d: %s \t %d\n", choices[i] + 1, Items[choices[i]], Prices[choices[i]] );
  73.     }
  74.     puts("");
  75.     printf("GIEF ME %d DOLLARS \n", sum);
  76.     return 0;
  77.  
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement