can you tell me what's wrong?

hello I have this homework and I get the answer but it's didn't work with me ,

this is the first Q:
Exercise 1 (10 marks)
Write a program that takes two integers L and H (i.e. lowest and
highest integer), and calls a function CalculateSum that computes the
sum of all the numbers between L and H. Finally, the program will
print the average of all these integer numbers by dividing the sum by
the number of integers between L and H. The program should assert if
L is less than or equal to H.


this is my work can any boady tellme why it dosen't calaulate the averge???

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
//Exercise 1 
/
course : programming concept fall 2009
*/

#include<iostream>

using namespace std;

int CalculateSum(int L,int H);
void main()
{
int L,H,sum,n=0,average;
    cout<<"Please enter the Lower number\n";
    cin>>L;
    cout<<"Please enter the Higher number\n";
    cin>>H;
	
  
 sum=CalculateSum(L,H);
 cout<<sum;
 //here I don't why it dosen't work !
	while( L<H){
  
           n++;
	}
 
 average=sum/n;
    cout<<"The average = "<<average<<endl;


  
}

int CalculateSum(int A,int B)
{
    int n,average,sum=0;

    for(A++;A<B;A++)
    {
        sum+=A;
    }


   
    
	return sum;
}






the second Q is very complicated. look to my answer and please tell me what's wrong :



Exercise 2 (50 marks)
A store has 6 items. Each item has a price and a bar-code that uniquely
identifies it. To simulate such a store, the program offers a menu showing the
codes, their corresponding items, and their prices. The user chooses one or
several items to buy, and pays for them. The program then returns the right
change (big bills first). For example, if the user pays 150 QR and the items cost
101 QR, then the amount returned is 49 QR. So, the change will be four 10
bills, one 5 bill, and four 1 bills. We assume that the user will buy one or more
items and checkout by choosing the code 0. If the amount is not sufficient, the
user is asked to repay for the items.
The codes, the items, and the prices to use are shown in the following table
(The code 0 is used to signal that the user completed the shopping):
Code Item Price
0 - -
1 Bread 5
2 Oil 8
3 Pizza 43
4 Laptop 1199
5 Macaroni 20
6 Tuna 7
Due to the problem of debugging and understanding programs with global
variables, your implementation must not contain any global variables.
1- [5 pts] Decompose the program into small tasks (to be implemented as
functions). Document your program decomposition and highlight the
design rational (why did you decompose your solution the way you did) -
max 1 page document -
2- [10 pts] Draw a flowchart diagram (manual and scan it or use any drawing
tool, PowerPoint or Word) illustrating how the program algorithm and
control flow.
3- [25 pts] Implement the functions identified in question 1.
4- [5 pts] Add the main method to your implementation
5- [5 pts] Run and test the program with following scenarios:
The user buys Pizza (item 3) and pays 45.
The user buys Laptop (item 4) and pays 1500.
The user did not buy anything by choosing the code zero.
The user buys two Pizzas (3), Macaroni (5), and Oil (2). The user pays 100.
Capture screen shots of the results of each scenario as evidence that your
program is working correctly. Add the captured screen shots to your program
documentation (a word document that you should add to your submitted zip
file).




and when I did it there is something wrong first the remain will be short by 1 and also the calculaition is not wrigh can you please Explain tome what's wrong ??

my anser is


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
//Exercise 2 
/
course : programming concept fall 2009
*/

#include<iostream>

using namespace std;

int GetPrice (int);
void main()
{
	int n=1,cash,price,code,remain,Quantity;
	
	while (n!=0){

	cout<<"Welcome... please Enter the code of your order "<<endl;
	cin>>code;
	cout<<"please Enter the Quantity"<<endl;//how much/many  you want?
	cin>>Quantity;
	cout<<"if you don't want to continue shopping  Enter 0 else Enter any other number"<<endl;
	cin>>n,
	

	price= GetPrice(code)*Quantity;
	price++;
	
	}
cout<<"please Enter the amount of money you will pay"<<endl;
	cin>>cash;

		if (price>cash)
			cout<<"please pay Extra money "<<endl;
		else {
	remain=cash%price;
	cout<<"the remain is QR "<<remain+1 <<endl;
		}

}


int GetPrice (int x){ 
	//here we use the swich satatment because we have more than one condition 

	int price ;
		switch (x)
		{
		case (1):    { //the customer order breade 
		price=5;
		break;
		}
		case (2):     {
		//the customer order oil

		price=8;
		break;
		}
		case (3):     {
		//the customer order pizza
		price=43;
		break;
		} 
		case (4):     {
		//the customer order laptop

		price=1199;
		break;
		}

		case (5):     {
		//the customer order macaroni

		price=20;
		break;

		}
		case (6):     {
		//the customer order tuna

		price=7;
		break;
		}

		default:                        {
		cout<<"Enter the correct code";
		break;
		}
		} 
		return price;
		}




thank you and t's due by tommorow midnight please don't copy on me for tommorow submition ,
Last edited on
first question:

1
2
3
4
 //here I don't why it dosen't work !
while( L<H){
  n++;
}


because it's an unending loop. If you make correct inputs, L is alway lower than H and the loop will never end. If you wonna get the difference betwwen both take n=H - L;, a for loop
1
2
3
4
for(int i=L; i<H; i++)
{
  n++;
}
or something like this.

second question:

You say cout<<"if you don't want to continue shopping Enter 0 else Enter any other number"<<endl; but the while-loop while (n!=0){...} ends if there's a 0 as input. If you wonna continue shopping as long as input equals 0 take while (n==0){}. Otherwise change your output in "if you wonna stop shopping Enter 0... "

In this place you overwrite price with the new value if gonna buy a second thing
1
2
	price= GetPrice(code)*Quantity;
price++;

And after this you are adding 1 to the price .. this should be the tax or is it a corrupt shop? :P
I think what you wonna reach is price += GetPrice(code)*Quantity;. To use this you have to change the first line of main into int n=1,cash,price=0,code,remain,Quantity;

There are any errors in function GetPrice?
if yes, it could be that you are using switch-case not correctly. I whould use it withooz "{}" inside, but maybe your way also works - I'm not sure.
1
2
3
4
5
6
7
8
9
10
switch (x)
{
  case (1):     //the customer order breade 
  price=5;
  break;
  case (2):    //the customer order oil
  price=8;
  break;
  ...
}

If you enter a wrong code you return price without a value. Maybe it works but I think there should be an error. So change your default into
1
2
3
4
  default:                        {
  cout<<"Enter the correct code";
  price = 0;
  break;



So for what I found on my first watch. If there are any other errors or an output which you don't want, write them into this thread.
Topic archived. No new replies allowed.