1. Engineering
  2. Computer Science
  3. i can only seem to input numbers 1 2 3...

Question: i can only seem to input numbers 1 2 3...

Question details

i can only seem to input numbers 1 2 3 4 5 for my selection i need to input a1 a2 a3 a4 a5. help?

#include <stdio.h>
#include <stdlib.h>

char* selection[5] = {"A1","A2","A3","A4","A5"}; //selection choices
char* Bars[5] = {"Mars","Bounty","Curly Wurly","Toffee Crisp","Double Decker"}; //names of items
int Stock[5] = {2,2,2,2,2}; //stock of items
float Price[5] = {0.90,1.20,1.10,0.75,1.01}; //price of items
//function to display vending machine table
void display_table()
{
printf("\t|---------------------------------------------------------------------------| ");
printf("\t\t|selection | %s | %s | %s | %s | %s | \n",selection[0],selection[1],selection[2],selection[3],selection[4]);
printf("\t|---------------------------------------------------------------------------| ");
printf("\t\t|Bars | %s | %s | %s | %s| %s |\n ",Bars[0],Bars[1],Bars[2],Bars[3],Bars[4]);
printf("\t|---------------------------------------------------------------------------| ");
printf("\t\t|Stock | %d | %d | %d | %d | %d |\n ",Stock[0],Stock[1],Stock[2],Stock[3],Stock[4]);
printf("\t|---------------------------------------------------------------------------| ");
printf("\t\t|Price | %.2f | %.2f | %.2f | %.2f | %.2f | \n ",Price[0],Price[1],Price[2],Price[3],Price[4]);
printf("\t|---------------------------------------------------------------------------| ");
}

int main(void) {
int choice=-1;
float amount=0.0;
float other_amount = 0.0;
while(1)
{
fflush(stdout);
display_table(); //displays table
printf("\t\tPlease make a selection (1,2,3,4,5):");
scanf("%d",&choice); //makes a selection
//if machine out of order
if(Stock[0] == 0 && Stock[1] == 0 && Stock[2] == 0 && Stock[3] == 0 && Stock[4] == 0)
{
printf("Out of order.\a ");
return EXIT_FAILURE;
}
//process selection
//first check stock
if(Stock[choice-1]<=0)
printf("\tItem %s is not in stock. Please choose another item./n ",Bars[choice-1]);
else{ //now process selection for purchase
printf("\tPrice of item %s is %.2f. \n ",Bars[choice-1],Price[choice-1]);
printf("\tEnter amount of money you wish to insert:");
scanf("%f",&amount);
if(amount == Price[choice-1])
{
printf("\tThank you very much for this purchase.\n \n ");
Stock[choice-1]-=1;
}
else if(amount > Price[choice-1])
{
printf("\tThank you very much for this purchase.\n \n ");
printf("\tPlease receive the change %.2f. \n \n",amount-Price[choice-1]);
Stock[choice-1]-=1;
}
else if(amount < Price[choice-1])
{
printf("\tAmount inserted by you is not sufficient. ");
printf("\tPlease insert %.2f more.\n\n\ \a ",Price[choice-1]-amount);
while(1)
{
printf("\tEnter amount of money you wish to insert:");
scanf("%f",&other_amount);
amount+=other_amount;
if(amount == Price[choice-1])
{
printf("\tThank you very much for this purchase. \n \n ");
Stock[choice-1]-=1;
break;
}
else if(amount > Price[choice-1])
{
printf("Thank you very much for this purchase.\n \n ");
printf("Please receive the change %.2f. \n \n ",amount-Price[choice-1]);
Stock[choice-1]-=1;
break;
}
}
}
}

}
return EXIT_SUCCESS;
}

Solution by an expert tutor
Blurred Solution
This question has been solved
Subscribe to see this solution