My code seems to stop working after first prompt for please insert money can anyone tell me where in my code am I lacking.

#include <stdio.h>
 
 
int main()
{
 char drinkchoice;
  float insert;
  float money;
  float totalinsert;
  

      
       
   
 printf("How much do you wish to insert: ");
 scanf("%f", &insert);
 if(insert < 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)
  {
   printf("\nWhat would you like.");
   printf("\nPress 1 for coke:\n,Press 2 for sprite:\n,Press 3 for drpepper: ");
   scanf("%c", &drinkchoice);
  
  
  if(drinkchoice == 1)
  printf("here is you coke");
  if(drinkchoice == 2)
  printf("here is your sprite");
  if(drinkchoice == 3)
  printf("here is your dr pepper");
  
  }
 
 getchar();
 return 0;
 
 
}

my guess is that the '\n' (<enter> ) needs to be flushed out of the keyboard after entering a number. you can use getchar() to do that. Example: call getchar() after line 20 to remove the '\n' from the keyboard buffer.

Take a look at these 2 snippets for reading floats without using scanf()
snippet one and snippet two.

#include <stdio.h>
 
int main()
{
 char drinkchoice;
  float insert;
  float money;
  float totalinsert;
  
 /* ... */
 if(insert < money)
 /* ... */
 if(totalinsert < money)
 /* ... */
 if(totalinsert < money)
 /* ... */
 if(totalinsert < money)
 /* ... */
 if(totalinsert == money)
  
 getchar();
 return 0;
}

It's never really a good idea to use variables which are uninitialised . In the first statement what's "money"? The value of money I mean. You should initialise on declaration of sometime before use:

float money = 24;
float somethingelse;

somethingelse = money-3;

/* etc */

Thanks that helped me alot, Now when it prompts for which drinkchoice would you like to choose it is not giving me the choice choosen back onto the screen. What is a better and easier way to associate my drinkchoices with the number given to the items listed.

#include <stdio.h>
  
int main()
{
  char drinkchoice;  /* ... */

  if(drinkchoice == 1)
    printf("here is you coke");
  if(drinkchoice == 2)
    printf("here is your sprite");
  if(drinkchoice == 3)
    printf("here is your dr pepper");
   
  /* ... */
}

Your problem lies here. You have chosen the drinkchoice as a character variable. Now. For if statements you need to have matching types for the left and right hand side of the == operator. If you look at the above code you can see that what's on the left is a character, as you have defined at the start, but what's on the right is actually a number. There are two ways you can go about modifying your code to make it work -

  1. change the type of drinkchoice to int, ie -- int drinkchoice;, and it should work.
  2. change the second (ie right hand side), parameters to a character, ie -- if ( drinkchoice == '1' ) { ... }

The ' which wrap around the number show you that it's a character.
You can, however, work with numbers in the if() statements, if you really want to. If you look at an ASCII table you can see the positioning of each letter with reference to two hex values. Use the hex value in the if statement, ie the number 1 (as a character), is 0x31 or the decimal equivalent of that.

And jlb please stop posting in bold. Its not that your post would assume utmost importance if you do so.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.