Here is a code I have been trying to finish but can't. I'm very new to this as you probably know by my other post but this program has been giving me a fit. I feel like I have hit a brick wall and it bothers me because I know the problem is probably simple. Look at this and let me know what's the best thing to do with it.

The goal is to have the program ask for the money, give the drink, give the change, keep a track of drink that was purchased and ask if the user wants another drink and if yes then loop back to the beginning.

The file is attached in C format.

Oh yeah in case you were wondering I'm using a simple free program called Miracle C to build my stuff. Please don't laugh about the Miracle C stuff. hahaha. Told ya I was very new to this.

Attachments
/*Justin Burdette
Week Three Individual Assignment
Soda Vending Machine*/

#include <stdio.h>

float insertmoney(float); //function
int menu(); //function
float selectionandchange(int, float); //function
float insertm;
float total;
float more;
float change;
float CountCoke;
float CountSprite;
float CountDrPepper;
int Drinkstotal;
char Y;
char N;
char i; 
char row;
int DrinkArray;
float fItemcost = 1.00;
float fTotalinserted = .0; 
float fDifference = 0.0;
int DrinkNum;
int initializer;

main()
{					    
					
int drinkchoice = 0;
float fChange = 0.00;

printf("How much do you wish to insert? ");
	scanf("%f", &insertm);
if (insertm < 1.00)	
{
	printf("\nYour current total inserted is: %.2f ", insertm);
	total = insertm;
do {
	printf("\nPlease insert more money");
	scanf("%f", &more);
	total = (total + more);
	printf("\nYour current total inserted is: %.2f ", total);
	}
	while (total < 1.00);
if(insertm == 1.00)
	change = (total - 1.00);
	
}	
			

drinkchoice = menu();
printf("\nHere is you selection and your change is $%.2f", selectionandchange(drinkchoice, change));

}

int menu()
{					//begin menu function
int DrinkNum = 0;

printf("Which drink do you wish for?\n");
printf("Please press 1 if you'd like a Coke\n");
printf("Please press 2 if you'd like a Sprite\n");
printf("Please press 3 if you'd like a Dr. Pepper\n");
scanf("%d", &DrinkNum);

return DrinkNum;

} 					//end menu function

float selectionandchange(int selection, float change)
{
int initializer [3][2]; 
int DrinkArray[0] = 0;
int DrinkArray[1] = 0;
int DrinkArray[2] = 0;
{					//begin selectionandchange function
if(selection == 1)
printf("You chose Coke!");
DrinkArray[0] = DrinkArray[0] + 1;
if (selection == 2)
printf("You chose Sprite!");
DrinkArray[1] = DrinkArray[1] + 1;
if (selection == 3)
printf("You chose a Dr. Pepper!");
DrinkArray[2] = DrinkArray[2] + 1;

if(change == fItemcost)
change = 0;
else if (change > fItemcost)


return change;

printf("\nWould you like to purchase another drink? Y/N");
    if (Y);
    return main;
    
    if (N);
    break;
    
printf("\nYou have purchased %d Cokes, %d Sprites, %d DrPeppers", DrinkArray[0], DrinkArray[1], DrinkArray[2]);     



        
			
getchar();
}					//end main

1. its not necessary to prototype main() as you did in line 8 because main() is defined by c standards and can not be called anywhere else in your program.

2. delete the unnecessary brace on line 79 because it serves no useful purpose.

3. line 76
>>int DrinkArray[0] = 0;

Illegal -- array must have a size greater than 0

4. lines 76, 77 and 78. Arrays are initialized like this:

int DrinkArray[2] = {0};

5. same lines as above. duplicat object declaration. delete lines 77 and 78 to correct this.

6. >> return main;
illegal syntax. delete that line.

I'm not going to go thrugh the rest of the code. Make corrections as suggested, recompile then post corrected code with more questions.

Thanks for the help. Now it will compile but when it gets to the point after I insert the money it just runs the question, "would you like another drink" over and over just infinitely. It doesn't seem like it's recognizing the array so how do I fix this?


The updated program is attached.

Attachments
/*Justin Burdette
Week Three Individual Assignment
Soda Vending Machine*/

#include <stdio.h>

float insertmoney(float); //function
int menu(); //function
float selectionandchange(int, float); //function
float insertm;
float total;
float more;
float change;
char Coke;
char Sprite;
char DrPepper;
int Drinkstotal;
char Y;
char N;
char i; 
char row;
int DrinkArray;
float fItemcost = 1.00;
float fTotalinserted = .0; 
float fDifference = 0.0;
int DrinkNum;
int initializer;

main()
{					    
					
int drinkchoice = 0;
float fChange = 0.00;

printf("How much do you wish to insert? ");
	scanf("%f", &insertm);
if (insertm < 1.00)	
{
	printf("\nYour current total inserted is: %.2f ", insertm);
	total = insertm;
do {
	printf("\nPlease insert more money");
	scanf("%f", &more);
	total = (total + more);
	printf("\nYour current total inserted is: %.2f ", total);
	}
	while (total < 1.00);
if(insertm == 1.00)
	change = (total - 1.00);
	
}	
			

drinkchoice = menu();
printf("\nHere is you selection and your change is $%.2f", selectionandchange(drinkchoice, change));

}

int menu()
{					//begin menu function
int DrinkNum = 0;

printf("Which drink do you wish for?\n");
printf("Please press 1 if you'd like a Coke\n");
printf("Please press 2 if you'd like a Sprite\n");
printf("Please press 3 if you'd like a Dr. Pepper\n");
scanf("%d", &DrinkNum);

return DrinkNum;

} 					//end menu function

float selectionandchange(int selection, float change)
{
int initializer [3][2]; 
int DrinkArray[40] = {0};

					//begin selectionandchange function
if(selection == 1)
printf("You chose Coke!");
DrinkArray[40] = DrinkArray[40] + 1;
if (selection == 2)
printf("You chose Sprite!");
DrinkArray[40] = DrinkArray[40] + 1;
if (selection == 3)
printf("You chose a Dr. Pepper!");
DrinkArray[40] = DrinkArray[40] + 1;

if(change == fItemcost)
change = 0;
else if (change > fItemcost)


return change;

printf("\nWould you like to purchase another drink? Y/N");
    if (Y);
    
    
    if (N);
    break;
    
printf("\nYou have purchased %d Cokes, %d Sprites, %d DrPeppers", DrinkArray[0], DrinkArray[1], DrinkArray[2]);     



        
			
getchar();
}					//end main

I believe that more people are going to help you if you post your code , instead of send it as an attachment. Your code is not as long as to need to be an attached file.

You need to work in the formatting of the source file. As it stands, your code is very difficult to read just because it doesn't follow any proper indentation. Read here for some sugestions.


Bonus: These if statements are doing nothing by virtue of the `;' at the end of each one.

if (Y);
    
    
    if (N);
    break;

However even if you remove the `;' still I doubt that will do anything of what you intended for them to do. Since it would be like if you would have written it as:

if( Y )
    if( N )
        break;

And those ifs will do nothing for you, since if( Y ) is true, the nested if( N ) will never be true.

Ok maybe this will be more easier for everyone to get to. The only problem now is that it won't count the drinks and it doesn't revert back to the beginning if yes is chosen.

Soda Vending Machine*/

#include <stdio.h>

float insertmoney(float); //function
int menu(); //function
float selectionandchange(int, float); //function
float insertm;
float total;
float more;
float change;
char Coke;
char Sprite;
char DrPepper;
int Drinkstotal;
char Y;
char N;
char i; 
char row;
int DrinkArray;
float fItemcost = 1.00;
float fTotalinserted = .0; 
float fDifference = 0.0;
int DrinkNum;
int initializer;

main()
{					    
					
int drinkchoice = 0;
float fChange = 0.00;

printf("How much do you wish to insert? ");
	scanf("%f", &insertm);
if (insertm < 1.00)	
{
	printf("\nYour current total inserted is: %.2f ", insertm);
	total = insertm;
do {
	printf("\nPlease insert more money");
	scanf("%f", &more);
	total = (total + more);
	printf("\nYour current total inserted is: %.2f ", total);
	}
	while (total < 1.00);
if(insertm == 1.00)
	change = (total - 1.00);
	
}	
			

drinkchoice = menu();
printf("\nHere is you selection and your change is $%.2f", selectionandchange(drinkchoice, change));

}

int menu()
{					//begin menu function
int DrinkNum = 0;

printf("Which drink do you wish for?\n");
printf("Please press 1 if you'd like a Coke\n");
printf("Please press 2 if you'd like a Sprite\n");
printf("Please press 3 if you'd like a Dr. Pepper\n");
scanf("%d", &DrinkNum);

return DrinkNum;

} 					//end menu function

float selectionandchange(int selection, float change)
{
int initializer [3][2]; 
int DrinkArray[40] = {0};

					//begin selectionandchange function
if(selection == 1)
printf("You chose Coke!");
DrinkArray[40] = DrinkArray[40] + 1;
if (selection == 2)
printf("You chose Sprite!");
DrinkArray[40] = DrinkArray[40] + 1;
if (selection == 3)
printf("You chose a Dr. Pepper!");
DrinkArray[40] = DrinkArray[40] + 1;

if(change == fItemcost)
change = 0;
else if (change > fItemcost)


return change;

printf("\nWould you like to purchase another drink? Y/N");
    if (Y)
    if (N)
    break;
    
printf("\nYou have purchased %d Cokes, %d Sprites, %d DrPeppers", DrinkArray[0], DrinkArray[1], DrinkArray[2]);     



        
			
getchar();
}

//end main

Ok been working on the code here and now I think I have a problem with the placement of my brackets. I've looked it over and can't find the problem so maybe one of you guys can. Thanks

Here is the latest version.

Soda Vending Machine*/

#include <stdio.h>


float insertmoney(float); //function
int menu(); //function
float selectionandchange(int, float); //function
float insertm;
float total;
float more;
float change;
char Coke;
char Sprite;
char DrPepper;
int Drinkstotal;
char Y;
char N;
char i;
char row;
int DrinkArray;
float fItemcost = 1.00;
float fTotalinserted = .0;
float fDifference = 0.0;
int DrinkNum;
int initializer;
char Answer;


main()
{


do {


int drinkchoice = 0;
float fChange = 0.00;


printf("How much do you wish to insert? ");
scanf("%f", &insertm);
if (insertm < 1.00)
{
printf("\nYour current total inserted is: %.2f ", insertm);
total = insertm;
do {
printf("\nPlease insert more money");
scanf("%f", &more);
total = (total + more);
printf("\nYour current total inserted is: %.2f ", total);
}
while (total < 1.00);
if(insertm == 1.00)
change = (total - 1.00);


}



drinkchoice = menu();
printf("\nHere is you selection and your change is $%.2f", selectionandchange(drinkchoice, change));


}


int menu()
{                   //begin menu function
int DrinkNum = 0;


printf("Which drink do you wish for?\n");
printf("Please press 1 if you'd like a Coke\n");
printf("Please press 2 if you'd like a Sprite\n");
printf("Please press 3 if you'd like a Dr. Pepper\n");
scanf("%d", &DrinkNum);


return DrinkNum;


printf("\nWould you like to purchase another drink? Y/N");
scanf("%c", Answer);
while (Answer!="N");
//end menu function
}
float selectionandchange(int selection, float change)
{
int initializer [3][2];
int DrinkArray[40] = {0};


//begin selectionandchange function
if(selection == 1)
printf("You chose Coke!");
DrinkArray[40] = DrinkArray[40] + 1;
if (selection == 2)
printf("You chose Sprite!");
DrinkArray[40] = DrinkArray[40] + 1;
if (selection == 3)
printf("You chose a Dr. Pepper!");
DrinkArray[40] = DrinkArray[40] + 1;


if(change == fItemcost)
change = 0;
else if (change > fItemcost)



return change;


printf("\nWould you like to purchase another drink? Y/N");
if (Y)
if (N)
break;


printf("\nYou have purchased %d Cokes, %d Sprites, %d DrPeppers", DrinkArray[40], DrinkArray[40], DrinkArray[40]);


getchar();
}                   //end main

Edited 3 Years Ago by happygeek: fixed formatting

Excuse the last posting of code. I'm very new to this field and this website. Thanks.

/*Justin Burdette
Week Three Individual Assignment
Soda Vending Machine*/

#include <stdio.h>

float insertmoney(float); //function
int menu(); //function
float selectionandchange(int, float); //function
float insertm;
float total;
float more;
float change;
char Coke;
char Sprite;
char DrPepper;
int Drinkstotal;
char Y;
char N;
char i; 
char row;
int DrinkArray;
float fItemcost = 1.00;
float fTotalinserted = .0; 
float fDifference = 0.0;
int DrinkNum;
int initializer;
char Answer;

main()
 { 

do {					    
					
int drinkchoice = 0;
float fChange = 0.00;

printf("How much do you wish to insert? ");
	scanf("%f", &insertm);
if (insertm < 1.00)	
{
	printf("\nYour current total inserted is: %.2f ", insertm);
	total = insertm;
do {
	printf("\nPlease insert more money");
	scanf("%f", &more);
	total = (total + more);
	printf("\nYour current total inserted is: %.2f ", total);
	}
	while (total < 1.00);
if(insertm == 1.00)
	change = (total - 1.00);
	
}	
			

drinkchoice = menu();
printf("\nHere is you selection and your change is $%.2f", selectionandchange(drinkchoice, change));

}

int menu()
{					//begin menu function
int DrinkNum = 0;

printf("Which drink do you wish for?\n");
printf("Please press 1 if you'd like a Coke\n");
printf("Please press 2 if you'd like a Sprite\n");
printf("Please press 3 if you'd like a Dr. Pepper\n");
scanf("%d", &DrinkNum);

return DrinkNum;

printf("\nWould you like to purchase another drink? Y/N");
    scanf("%c", Answer);
while (Answer!="N");
 					//end menu function
}
float selectionandchange(int selection, float change)
{
int initializer [3][2]; 
int DrinkArray[40] = {0};

					//begin selectionandchange function
if(selection == 1)
printf("You chose Coke!");
DrinkArray[40] = DrinkArray[40] + 1;
if (selection == 2)
printf("You chose Sprite!");
DrinkArray[40] = DrinkArray[40] + 1;
if (selection == 3)
printf("You chose a Dr. Pepper!");
DrinkArray[40] = DrinkArray[40] + 1;

if(change == fItemcost)
change = 0;
else if (change > fItemcost)


return change;

printf("\nWould you like to purchase another drink? Y/N");
    if (Y)
    if (N)
    break;
    
printf("\nYou have purchased %d Cokes, %d Sprites, %d DrPeppers", DrinkArray[40], DrinkArray[40], DrinkArray[40]);     



        
			
getchar();
}					//end main

Here's a tip - press the preview button (go advanced) until you're sure you've got all the [code]
[/code]
tags in the right place. Random attempts at sprinkling the various bits of tags around the place doesn't work.

You can also edit your own posts as well (within a few minutes at least), so there's no need to repost just because you forgot something.

A possible function rutine solution for handling the money affair.

/*
 * money.c
 * Shows the use of a money handler function name riteamount.
 */
#include <stdio.h>
#define PRICE 1.00

float riteamount( float init, float total );

int main( void )
{
    
    float result = 0;

    puts( "\tEnter money" );
    puts( "\t===========" );
     
    /*
     * Call to the hard working function
     */   
    result =  riteamount ( result, PRICE );
    
    if( result > 0 ) /* Refund */
    {
        printf( "Your change is %.2f\n", result );
    }
    printf( "Choose your soda\n" ); /* Prelude to soda menu */

    getchar();
    return 0;
  }

/*
 * riteamount( float, float );
 * accepts two parameters: float init for input amount
 *                        float total for initial price
 * returns: float init as a possible refund.
 */
float riteamount( float init, float total )
{
    float enter = 0;
    char newline;
    
    do
    {
        printf( "Enter at least %.2f", total );
        fflush( stdout );
        
       /* 
        * Get money. No checks for wrong inputs from user 
        */
        scanf( "%f%c", &enter, &newline );
        init += enter;
        
       /*
        * compute write amount
        */
        if( init < total )
        {
             total = total - init;
             init = 0;        
        }
        else
        {
             init = init - total;
             total = 0;
        }            
    }
    while( total ); 
        
    return init;
}
/* My input / output:

        Enter money
        ===========
You need to enter at least 1.00: .8
You need to enter at least 0.20: .4
Your change is 0.20
Choose your soda


        Enter money
        ===========
You need to enter at least 1.00: 4
Your change is 3.00
Choose your soda
*/

For clarity purposes I made use of scanf() to read the float input.
As long as digits are entered the function will work correctly.
For an example of how to read a float from user, checking for proper input, read more here.

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