How to print out something according to the input?

Hi, I actually wanted to print out something according to the character that I typed but it doesn't show it out.

Example,
When I entered 'A' and its quantity or 'B' and its quantity, the inputs can be saved but until I entered X as I want to exit, it won't exit.
It shows:
Package A, B (X=exit): A
Quantity : 1
Package A, B (X=exit): B
Quantity : 1
Package A, B (X=exit): X
Quantity :
It didn't exit when I pressed 'X'.

It supposed to be like:
Package A, B (X=exit): A
Quantity : 1
Package A, B (X=exit): B
Quantity : 1
when I pressed 'X' on "Package A, B (X=exit): "
it will proceed to this part:

PACKAGE A: 1 @ RM 24.50 = RM 24.50
PACKAGE B: 1 @ RM 26.00 = RM 26.00

Here's the code that I had made:

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
#include<stdio.h>
#include<stdlib.h>
#pragma warning(disable:4996)

void menu(void);

int main()
{
	menu();

	return 0;
}

void menu(void)
{
	char package;
	int qty;
	float p;

	do {

		printf("  Package A, B (X=exit): ");
		scanf(" %c", &package);
		printf("  Quantity : ");
		scanf(" %d", &qty);

	} while (package == 'A'|| package == 'B');

	if (package == 'X') 
	{

		if (package == 'A') {
			p = qty * 24.50;
			printf("Package %c: %d @ RM24.50 = RM %6.2f", package, qty, p);
		}
		else if (package == 'B') {
			p = qty * 26.00;
			printf("Package %c: %d @ RM26.00 = RM %6.2f", package, qty, p);
		}
	}
	system("pause");

}


I tried to change the if-else part to while but it still doesn't work.
Thank you for helping!
Is this what you're after?

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
#include <stdio.h>
#include <ctype.h>

void menu(void);

int main()
{
	menu();

	return 0;
}

void menu(void)
{
	char package = 0;
	int qtyA = 0, qtyB = 0;

	do {
		printf("Package A, B (X = exit): ");
		scanf(" %c", &package);
		package = (char)toupper(package);

		if (package != 'X') {
			if (package == 'A' || package == 'B') {
				printf("  Quantity : ");
				int q = 0;

				scanf(" %d", &q);

				if (package == 'A')
					qtyA += q;
				else
					qtyB += q;
			} else
				puts("Invalid package");
		}
	} while (package != 'X');

	const float pA = qtyA * 24.50f;
	const float pB = qtyB * 26.00f;

	printf("\nPackage A: %d @ RM24.50 = RM %6.2f", qtyA, pA);
	printf("\nPackage B: %d @ RM26.00 = RM %6.2f", qtyB, pB);
}

Your logic makes no sense.


} while (package == 'A'|| package == 'B');
This means you will keep looping (and changing the value of package) ... even though you have a meaningful value of package.




1
2
3
	if (package == 'X') 
	{
		if (package == 'A')

This is impossible. package can't take both values 'X' and 'A'. This isn't quantum mechanics fuzziness!



Considering a potential value of 'X' is pointless. You only need to loop until package is either A or B.
@seeplus ,
I had changed the code according to your code and I edited some parts, but there's one problem, when I chose only 'A', both 'A' and 'B' came out in the printf part, is it because I put
1
2
printf("\nPackage A : % d @ RM24.50 = RM % 6.2f", qtyA, pA);
         printf("\nPackage B : % d @ RM26.00 = RM % 6.2f", qtyB, pB);


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
55
56
57
58
59
60
61
62
63
64
65
66
#include<stdio.h>
#include<stdlib.h>
#pragma warning(disable:4996)

void menu(void);

int main()
{
	menu();

	return 0;
}

void menu(void)
{
	char package = 0;
	int qtyA = 0, qtyB = 0, q = 0;
	float pA, pB;

	do {

		printf("  Package A, B (X=exit): ");
		scanf(" %c", &package);

		if (package == 'A' || package == 'B'|| package == 'X') {
			if (package == 'A' || package == 'B') {
				printf("  Quantity : ");
				scanf(" %d", &q);
			}

			if (package == 'A') {
				qtyA = q;
			}
	
			else if (package == 'B') {
				qtyB = q;
			}

			else {
				q = 0;
			}
		}
		else {
			puts("Invalid package");
		}
	} while (package == 'A' || package == 'B');

	if (package == 'X') {

		const float pA = qtyA * 24.50f;
		const float pB = qtyB * 26.00f;

		if (package == 'A') {
			printf("\nPackage A : % d @ RM24.50 = RM % 6.2f", qtyA, pA);
		}

		if (package == 'B') {
			printf("\nPackage B : % d @ RM26.00 = RM % 6.2f", qtyB, pB);
		}
	
		else {
			printf("\nPackage A : % d @ RM24.50 = RM % 6.2f", qtyA, pA);
			printf("\nPackage B : % d @ RM26.00 = RM % 6.2f", qtyB, pB);
		}
	}
}


Here's the output when I entered 'A' as input:
Package A, B (X=exit): A
Quantity : 1
Package A, B (X=exit): X

Package A : 1 @ RM24.50 = RM 24.50
Package B : 0 @ RM26.00 = RM 0.00

When I insert 'A' for input, the "Package B : 0 @ RM26.00 = RM 0.00" came out.
It actually going to be like:

Package A, B (X=exit): A
  Quantity : 1
  Package A, B (X=exit): X

Package A :  1 @ RM24.50 = RM  24.50


When I insert 'B' for input, the "Package A : 0 @ RM24.50 = RM 0.00" came out.

Here's the output when I entered 'B' as input:

  Package A, B (X=exit): B
  Quantity : 1
  Package A, B (X=exit): X

Package A :  0 @ RM24.50 = RM   0.00
Package B :  1 @ RM26.00 = RM  26.00


It actually going to be like:

Package A, B (X=exit): A
  Quantity : 1
  Package A, B (X=exit): X

Package B :  1 @ RM26.00 = RM  26.00

Last edited on
when I chose only 'A', both 'A' and 'B' came out in the printf part,


This is as per design, as the spec of the program wasn't really provided.

If you want no output if no A or no B entered, then change L42 43 of my code above to:

1
2
3
4
5
if (qtyA)
    printf("\nPackage A: %d @ RM24.50 = RM %6.2f", qtyA, pA);

if (qtyB)
    printf("\nPackage B: %d @ RM26.00 = RM %6.2f", qtyB, pB);



Your logic:
1
2
3
4
5
6
7
8
9
10
11
12
13
if (package == 'X') {
  // Fact: package == 'X'

  if (package == 'A') {
    // This cannot happen, because package == 'X'
  }

  if (package == 'B') {
    // This cannot happen, because package == 'X'
  } else {
    // This will happen, because package != 'B'
  }
}
@seeplus

if the program is looped, example:
First person ordered 1 Package A, so output will show:
Package A :  1 @ RM24.50 = RM  24.50

and then the program is looped, then second person ordered 1 Package B, but the output is:
Package A :  1 @ RM24.50 = RM  24.50
            Package B :  1 @ RM26.00 = RM  26.00

it includes the first person's order instead of showing only 'Package B : 1 @ RM26.00 = RM 26.00'
I tried to change some code but it ended up showing the same output.

Package A, B (X=exit): A
  Quantity : 1
  Package A, B (X=exit): X

Package A: 1 @ RM24.50 = RM  24.50     Next customer (Y=yes?) : Y
  Package A, B (X=exit): B
  Quantity : 1
  Package A, B (X=exit): X

Package A: 1 @ RM24.50 = RM  24.50
Package B: 1 @ RM26.00 = RM  26.00     Next customer (Y=yes?) : N


           DAILY SALES SUMMARY REPORT
PACKAGE         Quantity Sold   Sales Amount
   A                         1               24.50
   B                         1               26.00
========        ===          ========
 TOTAL                     2               50.50                                                 


Here's the code:
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include<stdio.h>
#include<stdlib.h>
#pragma warning(disable:4996)
#define packageA 24.50
#define packageB 26.00

void menu(void);

int main()
{
	menu();

	return 0;
}

void menu(void)
{
	char package = 0, yesno;
	int QsA = 0, QsB = 0, qtyA = 0, qtyB = 0, q = 0, Totalqty;
	float SaA=0, SaB=0, pA, pB, Totalamount;

	do {
		do {

			printf("  Package A, B (X=exit): ");
			scanf(" %c", &package);

			if (package == 'A' || package == 'B' || package == 'X') {
				if (package == 'A' || package == 'B') {
					printf("  Quantity : ");
					scanf(" %d", &q);
				}

				if (package == 'A') {
					qtyA = q;
				}

				else if (package == 'B') {
					qtyB = q;
				}

				else {
					q = 0;
				}
			}
			else {
				puts("Invalid package");
			}
		} while (package == 'A' || package == 'B');

		if (package == 'X') {

			const float pA = qtyA * 24.50f;
			const float pB = qtyB * 26.00f;

			if (package == 'A') {
				printf("\nPackage A : % d @ RM24.50 = RM % 6.2f", qtyA, pA);
			}

			if (package == 'B') {
				printf("\nPackage B : % d @ RM26.00 = RM % 6.2f", qtyB, pB);
			}

			else {
				if (qtyA)
					printf("\nPackage A: %d @ RM24.50 = RM %6.2f", qtyA, pA);

				if (qtyB)
					printf("\nPackage B: %d @ RM26.00 = RM %6.2f", qtyB, pB);

				printf("     Next customer (Y=yes?) : ");
				scanf(" %c", &yesno);
			}

		}
	} while (yesno == 'Y' || yesno == 'y');
	if (yesno = 'N' || yesno == 'n')
	{
		QsA = QsA + qtyA;
		SaA = QsA * packageA;


		QsB = QsB + qtyB;
		SaB = QsB * packageB;

		Totalqty = QsA + QsB;
		Totalamount = SaA + SaB;

		printf("\n");
		printf("           DAILY SALES SUMMARY REPORT      \n");
		printf("PACKAGE\t\tQuantity Sold\tSales Amount\n");
		printf("   A\t\t%d\t\t%-0.2f\n", QsA, SaA);
		printf("   B\t\t%d\t\t%-0.2f\n", QsB, SaB);
		printf("========\t==============\t============\n");
		printf(" TOTAL\t\t%d\t\t%-0.2f\n", Totalqty, Totalamount);
	}
}
Regarding your lines 51, 56, and 60: see keskiverto's post again.

Line 77: == should be used to test for equality, not =.

Also, your yesno variable is not initialized with a valid value, and it's possible to read line 76 without it being assigned to.
Last edited on
Ok. perhaps:

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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

#define packageA 24.50f
#define packageB 26.00f

void menu(void);

int main()
{
	menu();

	return 0;
}

void menu(void)
{
	char yesno = 0;
	int QsA = 0, QsB = 0;

	do {
		int qtyA = 0, qtyB = 0;
		char package = 0;

		do {
			int q = 0;

			printf("  Package A, B (X = exit): ");
			scanf(" %c", &package);
			package = (char)toupper(package);

			if (package == 'A' || package == 'B' || package == 'X') {
				if (package == 'A' || package == 'B') {
					printf("  Quantity : ");
					scanf(" %d", &q);

					if (package == 'A')
						qtyA += q;
					else if (package == 'B')
						qtyB += q;
				}
			} else
				puts("Invalid package");
		} while (package == 'A' || package == 'B');

		const float pA = qtyA * packageA;
		const float pB = qtyB * packageB;

		QsA += qtyA;
		QsB += qtyB;

		if (qtyA)
			printf("\nPackage A: %d @ RM24.50 = RM %6.2f", qtyA, pA);

		if (qtyB)
			printf("\nPackage B: %d @ RM26.00 = RM %6.2f", qtyB, pB);

		printf("\n\n     Next customer (Y = yes?) : ");
		scanf(" %c", &yesno);
		yesno = (char)toupper(yesno);

	} while (yesno == 'Y');

	if (yesno = 'N') {
		const float SaA = QsA * packageA;
		const float SaB = QsB * packageB;
		const int Totalqty = QsA + QsB;
		const float Totalamount = SaA + SaB;

		printf("\n           DAILY SALES SUMMARY REPORT      \n");
		printf("PACKAGE\t\tQuantity Sold\tSales Amount\n");
		printf("   A\t\t%d\t\t%-0.2f\n", QsA, SaA);
		printf("   B\t\t%d\t\t%-0.2f\n", QsB, SaB);
		printf("========\t==============\t============\n");
		printf(" TOTAL\t\t%d\t\t%-0.2f\n", Totalqty, Totalamount);
	}
}


Topic archived. No new replies allowed.