Is there any other code written for a coke machine program.
We’ll start by having the user “insert” their money. Since they can’t physically insert the money, we will just ask them how much they wish to insert and we will keep track of how much money they have inserted. The money insertion will be handled in a SEPARATE function. For example, before they start, their total inserted is $0. let’s say that a drink costs $1.00. If they say that they are inserting “$0.25” then we will add $0.25 to $0 and tell them to please insert $0.75. Then we will ask them how much they are inserting. If they say “$0.25”, then we add $0.25 to the $0.25 that they have already inserted giving them $0.50. Once they have inserted $1.00, we can give them their drink options. If they have not entered at least $1.00, we will invoke the money function so they can insert more.

The above (money insertion) will typically be handled in a loop.
I am just wondering if there is anyone else who have a simplier code willing to look at mine and give me some pointers on making my code simplier using For loop, while loop, or if else loop.

Here is my code:

#include <stdio.h>
 
int main()
{
  char drinkchoice;     //gives drinkchoice a named character to be recognized
  float insert= 0.00;    //This allows the insert function to be used throughout code
  float money = 1.00;   //Same here as well as totalinsert function
  float totalinsert = 0.00;    //this allows the totalinsert function to be carried out as the insert function
 
  
  
 printf("\nHow much do you wish to insert: ");    //prompts the user for their amount
 scanf("%f", &insert);                       //recieves input from user
 
 
 
 
 if(totalinsert < money)
 {
   printf("\nPlease insert more money: ");
   scanf("%f", &insert);
   totalinsert = (insert + totalinsert);
  }
  
  if(totalinsert < money)
  {
   printf("\nPlease insert more money: ");
   scanf("%f", &insert);
   totalinsert = (insert + totalinsert);
   }
   
   if(totalinsert < money)
   {
    printf("\nPlease insert more money: ");
    scanf("%f", &insert);
    totalinsert = (insert + totalinsert);
    }
    
    if(totalinsert < money)
    {
     printf("\nPlease insert more money: ");
     scanf("%f", &insert);
     totalinsert = (insert + totalinsert);
     }
 
 
 if(totalinsert == money)        //This is the same as the decision process as the statement before but this function evaluates if the statement is true to perform future function bypasses the other code functions
  {
   printf("\nWhat would you like.");  //This is a menu selection window allowing the user to make a selection after the correct amount of money is enterd
   printf("\nPress 1 for coke\nPress 2 for sprite\nPress 3 for drpepper: ");
   scanf("%c", &drinkchoice);
   totalinsert = (totalinsert == insert);
          getchar();
          }
          
          
          
          if(drinkchoice == '1')
          {
          printf("here is your coke");
          scanf("%c", &drinkchoice);
          getchar();
          }
          
          if(drinkchoice == '2')
          {
          printf("here is your sprite");
          scanf("%c", &drinkchoice);
          getchar();
          }
          
          if(drinkchoice == '3')
          {
          printf("here is your Dr Pepper");
          scanf("%c", &drinkchoice);
          getchar();
          }
 
  
  
 int NumArray[10];
 
 
 
 for(int counter = 0; counter < 10; counter ++)
  {
  
     printf{"\nPlease make your selection");
     scanf("%d", soda);
     NumArray[counter];
     
    }
    
    
  
  
  
 
 
 getchar();
 return 0;
 
 
}

>I am just wondering if there is anyone else who have a simplier code willing to look at mine and give me some >pointers on making my code simplier using For loop, while loop, or if else loop.

Is this your code?.
Has you tried to compile it?. I know you could have not made it work.

This is not valid in C:

for(int counter = 0; counter < 10; counter ++)
  {
  
     printf{"\nPlease make your selection");
     scanf("%d", soda);
     NumArray[counter];
}

The counter variable can not be declared in the loop construction.
The variable soda is not declared anywhere.
However, that whole "for loop" doesn't do anything for your code.

printf("\nWhat would you like."); //This is a menu selection window allowing the user to make a selection after the correct amount of money is enterd
   printf("\nPress 1 for coke\nPress 2 for sprite\nPress 3 for drpepper: ");
   scanf("%c", &drinkchoice);

In this part you are asking the user for a pre-selected number that
map the kind of soda, but then you store it in a character variable.
That will never do. Check and you'll see that pattern in other parts
of the code you have posted.

Now you seems to know that to make it simpler a loop form is needed. Why don't you try first by yourself and then report again?.

dude please study c well before u start writing code u have used an if 4 times wht if after 4th time money is still less use a dowhile loop....and study a lot before u get to coding....

>dude please study c well before u start writing code u have used an if 4
>times wht if after 4th time money is still less use a dowhile loop
Wow, do you even know what punctuation is? We recommend that you use proper English. There's no reason to use chat abbreviations and poor grammar here. You have plenty of time to compose your post and make it correct. If you don't, you make things harder on non-native English speakers.

>and study a lot before u get to coding....
You can't learn C without writing code.

#include <stdio.h>

int main()
{
  char drinkchoice;     //gives drinkchoice a named character to be recognized
  float insert= 0.00;    //This allows the insert function to be used throughout code
  const float money = 1.00;   //Same here as well as totalinsert function
  float totalinsert = 0.00;    //this allows the totalinsert function to be carried out as the insert function



 printf("\nHow much do you wish to insert: ");    //prompts the user for their amount
 scanf("%f", &insert);                       //recieves input from user
 while (totalinsert < money)
 {
   printf("\nPlease insert  $", money-totalinsert);
   scanf("%f", &insert);
   totalinsert = (insert + totalinsert);
 }
  printf("You get back %f $!!!",money-totalinsert);
 printf("\nWhat would you like.");  //This is a menu selection window allowing the user to make a selection after the correct amount of money is enterd
   printf("\nPress 1 for coke\nPress 2 for sprite\nPress 3 for drpepper: ");
   scanf("%c", &drinkchoice);

          switch (drinkchoice)
          {
             1:{printf("here is your coke");break;}
             2:{printf("here is your sprite");break;}
             3:{printf("here is your Dr Pepper");break;}
           }
}

Now, you can insert 0.00001$ too. Since money won't change in your program, you should declare it as constant.

Edited 3 Years Ago by pyTony: fixed formatting

This article has been dead for over six months. Start a new discussion instead.