Help

A buddy of mine asked me to help him with his assignment as he's just recently started learning c++ so I thought I'd give it a shot, he sent me this program, but its got so many errors that I have no clue where to start, being a beginner myself it's a bit over my knowledge so I thought you guys could help.

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
#include <iostream>
using namespace std;

main(){

double calculateItemCost(double&, char&, unsigned&);
void DisplayTotalCost(double&, unsigned&);
double ItemPrice,TotalPrice=0;
char DiscountType;
int ItemQuantioy, recordNum=0,ItemId;
bool aborted= false;


cout<< "Enter\n -> Item Id:";
cin>>ItemId;
cout<< "\n -> Item Price:";
cin>>ItemPrice;
cout<< "\n -> Discount Type";
cin>>DiscountType;
cout<< "\n -> Item Quantioy";
cin>>ItemQuantioy;
//funtion call
TotalPrice =+ calculateItemCost(ItemPrice, DiscountType, ItemQuantioy);
recordNum=+1;
//loop end //if exits becouse of error aborted = True;
//function call
DisplayTotalCost(TotalCost, recordNum, aborted);

}

//Question 2 //Calculate Item Cost
double calculateItemCost(double ItemPrice, char DiscountType, unsigned ItemQuantioy );{

Total = ItemPrice*0.0*ItemQuantioy};

switch (DiscountType){
case 'N': Total = ItemPrice*ItemQuantioy;brack; //N normal price
case 'B':Total = ItemPrice*0.0*ItemQuantioy;brack; //B 10% off
case 'D':Total = ItemPrice*ItemQuantioy;brack; //D 20% off
case 'T':Total = ItemPrice*ItemQuantioy;brack; //T 30% off
return TotalPrice;
}

//Question 3//Display Total Cost
void DisplayTotalCost(double TotalCost, unsigned recordNum, bool aborted); {

cout>> "\n\nTotal sale cost" <<recordNum<<TotalCost<<endl;
if(aborted==true){ cout>>"Input Terminated by invalid data at record /d.",recordNum+1;
}
}

These are the errors I was getting trying to compile.

C:\Users\~\Desktop\jewellery sale.cpp: In function `int main()':
C:\Users\~\Desktop\jewellery sale.cpp:23: error: invalid initialization of reference of type 'unsigned int&' from expression of type 'int'
C:\Users\~\Desktop\jewellery sale.cpp:6: error: in passing argument 3 of `double calculateItemCost(double&, char&, unsigned int&)'
C:\Users\~\Desktop\jewellery sale.cpp:27: error: `TotalCost' undeclared (first use this function)
C:\Users\~\Desktop\jewellery sale.cpp:27: error: (Each undeclared identifier is reported only once for each function it appears in.)

C:\Users\~\Desktop\jewellery sale.cpp: At global scope:
C:\Users\~\Desktop\jewellery sale.cpp:32: error: expected unqualified-id before '{' token
C:\Users\~\Desktop\jewellery sale.cpp:32: error: expected `,' or `;' before '{' token
C:\Users\~\Desktop\jewellery sale.cpp:36: error: expected unqualified-id before "switch"
C:\Users\~\Desktop\jewellery sale.cpp:36: error: expected `,' or `;' before "switch"
C:\Users\~\Desktop\jewellery sale.cpp:45: error: expected unqualified-id before '{' token
C:\Users\~\Desktop\jewellery sale.cpp:45: error: expected `,' or `;' before '{' token

Execution terminated



From what I'm told this program of his is supposed manage a list of sale records for a jewelery shop.
Use += not =+.
Declare TotalCost in main.
Check your brackets in "calculateItemCost" and don't put a semi colon after the function declaration.
You should use a "break;" after every switch case unless you want it to fall through to the next case. In this program it doesn't really matter, but it could cause trouble somewhere else.
line 4) main needs a return type. (ie int main())

line 6, 7) The parameters here are taking references (notice the & symbol), yet the parameters in the actual functions do not take references (no & symbol). Those must match. I recommend getting rid of the '&'s because there is no reason to be passing these parameters by reference.

line 27) Trying to use a variable named "TotalCost", but there is no such variable. he probably meant to say "TotalPrice"

line 32) Should not have a semicolon before the {

line 34) 'Total' needs a type. It has not been defined yet. Ie, it needs to be double total = ...

line 37-40) It should be break, not brack

line 41) Trying to return "TotalPrice", but there is no TotalPrice. He probably meant to say "Total"

line 45) same as line 32



Those are all I saw at a glance. There might be more.

Tell your friend to compile after every few lines of code he adds and fix errors as he goes. Writing a full program and then having a million errors makes it much harder to work out -- especially when you don't know how to read error messages.
You can not define function within your main ; ->

incorrect ->
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
#include <iostream>
using namespace std;

main(){

double calculateItemCost(double&, char&, unsigned&);
void DisplayTotalCost(double&, unsigned&);
double ItemPrice,TotalPrice=0;
char DiscountType;
int ItemQuantioy, recordNum=0,ItemId;
bool aborted= false;


cout<< "Enter\n -> Item Id:";
cin>>ItemId;
cout<< "\n -> Item Price:";
cin>>ItemPrice;
cout<< "\n -> Discount Type";
cin>>DiscountType;
cout<< "\n -> Item Quantioy";
cin>>ItemQuantioy;
//funtion call
TotalPrice =+ calculateItemCost(ItemPrice, DiscountType, ItemQuantioy);
recordNum=+1;
//loop end //if exits becouse of error aborted = True;
//function call
DisplayTotalCost(TotalCost, recordNum, aborted);

}


Correct ->

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
#include <iostream>
using namespace std;

double calculateItemCost(double&, char&, unsigned&);
void DisplayTotalCost(double&, unsigned&);

main(){


double ItemPrice,TotalPrice=0;
char DiscountType;
int ItemQuantioy, recordNum=0,ItemId;
bool aborted= false;


cout<< "Enter\n -> Item Id:";
cin>>ItemId;
cout<< "\n -> Item Price:";
cin>>ItemPrice;
cout<< "\n -> Discount Type";
cin>>DiscountType;
cout<< "\n -> Item Quantioy";
cin>>ItemQuantioy;
//funtion call
TotalPrice =+ calculateItemCost(ItemPrice, DiscountType, ItemQuantioy);
recordNum=+1;
//loop end //if exits becouse of error aborted = True;
//function call
DisplayTotalCost(TotalCost, recordNum, aborted);

}


incorrect recordNum=+1 ;
correct -> recordNum +=1;


you can not put semi column (;) before {
incorrect ->

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
//Question 2 //Calculate Item Cost
double calculateItemCost(double ItemPrice, char DiscountType, unsigned ItemQuantioy );{

Total = ItemPrice*0.0*ItemQuantioy};

switch (DiscountType){
case 'N': Total = ItemPrice*ItemQuantioy;brack; //N normal price
case 'B':Total = ItemPrice*0.0*ItemQuantioy;brack; //B 10% off
case 'D':Total = ItemPrice*ItemQuantioy;brack; //D 20% off
case 'T':Total = ItemPrice*ItemQuantioy;brack; //T 30% off
return TotalPrice;
}

//Question 3//Display Total Cost
void DisplayTotalCost(double TotalCost, unsigned recordNum, bool aborted); {

cout>> "\n\nTotal sale cost" <<recordNum<<TotalCost<<endl;
if(aborted==true){ cout>>"Input Terminated by invalid data at record /d.",recordNum+1;
}
}


correct->

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
//Question 2 //Calculate Item Cost
double calculateItemCost(double ItemPrice, char DiscountType, unsigned ItemQuantioy ){

Total = ItemPrice*0.0*ItemQuantioy};

	switch (DiscountType){
	case 'N': Total = ItemPrice*ItemQuantioy;brack; //N normal price
	case 'B':Total = ItemPrice*0.0*ItemQuantioy;brack; //B 10% off
	case 'D':Total = ItemPrice*ItemQuantioy;brack; //D 20% off
	case 'T':Total = ItemPrice*ItemQuantioy;brack; //T 30% off
	return TotalPrice;
	}

//Question 3//Display Total Cost
void DisplayTotalCost(double TotalCost, unsigned recordNum, bool aborted){

	cout>> "\n\nTotal sale cost" <<recordNum<<TotalCost<<endl;
	if(aborted==true){ 
		cout>>"Input Terminated by invalid data at record /d.",recordNum+1;
	}
}


incorrect -> Total = ItemPrice*0.0*ItemQuantioy};
incorrect ->return TotalPrice;

correct ->
1
2
double Total = ItemPrice*0.0*ItemQuantioy ;
return Total;


There are a lot of other errors find it yourself out
Topic archived. No new replies allowed.