Loop nonstop

Hello everyone..im new here and seeking help..hope you guys can help me..

I got this question and need to this in modular programming ( i need to create my own library and source file)

You need to create a simple C++ code at one restaurant. The program should ask you to input menu code and the amount for each menu.The program should keep on asking for menu code until the cashier enter -1 to indicate the end of order list.At the end, the program will print the total need to pay including calculation below.

- service charge of 10% per receipt
- government tax of 5%
- rounding off to the nearest 0.00 or 0.05 according to the new government regulation.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
pay.h
void all_pay(total_all);
#define BB 2.00
#define HH 2.00
#define FF 1.30
#define CC 1.50
#define PP 1.50
int total_menu,amount,total_all,total_pay,loop;
int code;

int main (){
		printf ("Please enter menu code shown as below:\n");
		printf ("Burger = BB\n");
		printf ("Hot Dog =HH\n");
		printf ("French Fries =FF\n");
		printf ("Coca cola =CC\n");
		printf ("Pepsi = PP\n");
		printf ("Press -1 to stop\n\n");

		for (loop=0;loop>=0;loop++)
		{
		printf ("Enter the code foods/drinks here:");
		scanf ("%d",&code);

			if (code =! -1)
			{
			printf ("Enter the amount foods/drinks here:");
			scanf ("%d",&amount);

			total_menu=code*amount;
			total_all=total_all+total_menu;
			}

			else 
			all_pay(total_all);

		}

}

1
2
3
4
5
6
7
8
9
10
11
pay.c
#include <stdio.h>
#include "pay.h"

void all_pay (total_all){
	double service_charge,gov_tax,overall;
	service_charge=total_pay*0.1;
	gov_tax=total_pay*0.05;
	overall=total_pay+service_charge+gov_tax;
	printf ("The total is :%d",overall);
}



Now my problem is..
1. The program when I enter the menu code or -1, the program loop non stop.
2. No calculation happen


Can some one correct this code for me? Can put some explanation also? Because i just learn C++. Thanks in advance!
for (loop=0;loop>=0;loop++) will continue while loop is greater or equal to 0 but if you set it to 0 at start you will have to wait until its value is wrapped over the maximum to negative values
I suppose if (code =! -1) should be if (code != -1)
Why is your main function implementation in the header?
Last edited on
Hahaha..because in my class..the lecturer teach me implement main function in header much easier..I don't know which one easier. I just do how my lecturer teach me. Will you help me create this code for me? Because i already stuck
Your lecturer is a moron. IMHO.

main() belongs in a c or cpp file.
Couldn't agree more. Someone using methods like that to teach is pathetic.
Haha..i could show the other example that using main function in header~hahaha!
By the way...
void all_pay(total_all);

should be
void all_pay(int total_all);

And i think the for loop is meant to create an infinite loop. Better would be
1
2
3
while (true) {
 ...
}

with a break; statement to exit the loop.
Actually i not sure because the variable code need to accept 2 type ( int and char).. now i still stuck here and can't continue..this assignment quite tough for me
Ok guys..i change my main function to source file

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <stdio.h>
#include "pay.h"
#define BB 2.00
#define HH 2.00
#define FF 1.30
#define CC 1.50
#define PP 1.50


int total_menu,amount,total_all,total_pay,loop;
int code;

int main ()
{
		printf ("Please enter menu code shown as below:\n");
		printf ("Burger = BB\n");
		printf ("Hot Dog =HH\n");
		printf ("French Fries =FF\n");
		printf ("Coca cola =CC\n");
		printf ("Pepsi = PP\n");
		printf ("Press -1 to stop\n\n");

		for (loop=1;loop<=10,code!=-1;loop++)
		{
		printf ("Enter the code foods/drinks here:");
		scanf ("%s",&code);

		if (code!=-1)
		{
			printf ("Enter the amount foods/drinks here:");
			scanf ("%d",&amount);

			total_menu=code*amount;
			total_all=total_all+total_menu;

			printf ("Total:%d\n",total_menu);

		}
		else if (code==-1)
		{
			all_pay(total_all);
		}
		else
			{
			printf ("Wrong code");
			}
		}
			
}



1
2
3
4
5
6
7
8
9
10
11
12
void all_pay (int total_all)
{
	double service_charge,gov_tax,overall;
	service_charge=total_all*0.1;
	gov_tax=total_all*0.05;

	overall=total_all+service_charge+gov_tax;


	printf ("The total is :%d\n",overall);

}



It be will i show above.



Now my problem is

1. After I enter code for example BB then the program ask me the amount..But when to print the total, the value is incorrect.

2.Even I enter code "-1", the program still asking the amount. Suppose when I enter "-1", the program should stop and calculate the overall total. (To calculate the overall total is in my header file).


Guys please help me! Correct me..I already 3 days less sleep..This program so tough for newbie like me..Soo sad!

closed account (z05DSL3A)
You are trying to put a code such as 'BB' (as string) into an integer!

This will get you more on course, but still you need to address how you get input from the user (this will loop out of controll if a letter is used instead of numbers).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
int main(int argc, char *argv[])
{
    double total_menu = 0;
    double amount = 0;
    double total_all = 0;
    double total_pay = 0;
    int code = 0;

    double prices[6] = {0.0,  //dummy value
                        2.00, // Burger
                        2.00, // Hot Dog
                        1.30, // French Fries
                        1.50, // Coca cola
                        1.50  // Pepsi
                       };

    bool finished = false;
    while(!finished)
    {
        printf ("Please enter menu code shown as below:\n");
        printf ("Burger       = 1\n");
        printf ("Hot Dog      = 2\n");
        printf ("French Fries = 3\n");
        printf ("Coca cola    = 4\n");
        printf ("Pepsi        = 5\n");
        printf ("Enter -1 to finish\n\n");

        printf ("Enter the code foods/drinks here:");
        scanf ("%d",&code);

        if ((code>= 1) && (code <= 4))
        {
            printf ("Enter the amount foods/drinks here:");
            scanf ("%d",&amount);

            total_menu = prices[code] * amount;
            total_all = total_all+total_menu;

            printf ("Total:%d\n",total_menu);

        }
        else if(code == -1)
        {
            finished = true;
        }
        else
        {
            printf ("Wrong code");
        }
    }
    all_pay(total_all);

    return 0;
}
Last edited on
@grey wolf.

thanks for your support..but i afraid that i got this kinda error

error C2065: 'bool' : undeclared identifier
error C2146: syntax error : missing ';' before identifier 'finished'
error C2065: 'finished' : undeclared identifier
error C2065: 'false' : undeclared identifier
error C2065: 'true' : undeclared identifier
warning C4244: 'function' : conversion from 'double ' to 'int ', possible loss of data


Furthermore, i would like to ask what is "bool"? Boolean?
closed account (z05DSL3A)
Are you using an old compiler?

Yes, bool is Boolean. You could for now define 'finished' as an int and use 0 and 1to controll the loop.

1
2
3
4
5
6
7
8
int finished = 0;
while ( finished == 0)
{
...
    {
         finished =1;
    }
}


As for warning C4244 I gforgot to mention that all_pay should be changed to:
void all_pay (double total_all)
{
...
}
You may have to include stdbool.h
Yes, bool means boolean and it can take the value "false" (== 0) and "true" (any other number).

What compiler do you use? Some old compilers don't work with bool. But you can define a simple kind of bool by adding these lines to the top of your program:

1
2
3
#define bool int
#define true 1
#define false 0 
Maybe he's using C, instead of C++.
I'm using Microsoft visual basic c++ 6.0 at my lab. Thanks a lot for all your feedback. I program now totally work. Thanks again !
Sorry i forgot to mention this.. the program not complete yet..

- rounding off to the nearest 0.00 or 0.05 according to the new government regulation.

0.06>>0.07 will round to 0.05, 0.08>>0.09 will round to 0.10
0.01>>0.02 will round to 0.00, 0.03>>0.04 will round to 0.05
example
$1.37 will $be 1.35
$1.38 will $be 1.40
$1.33 will $be 1.35
$1.32 will $be 1.30

can help me?
Topic archived. No new replies allowed.