what's wrong with this code?

Hello, I'm trying to figure out this problem I'm having so that I can finish the last portion of my programming lab for school. I know I've fixed this error before, I just can't seem to do it this time. Can anyone please point me in the right direction? (Also, it's a debugging lab, hence only comments being about what I've changed)

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
#include<iostream> //removed .h
#include<conio.h>
#include<string> //removed .h
using namespace std; //added this

class Order

{
	friend Order yearEndTotal(Order summary, Order oneOrder); //added friend
	private:
	char customerName[20];
	int orderQuantity;
	public:
	Order();
	void setValues(char customerName[], int orderQuantity); //added void and paramaters
	void displayValues(); //added void
};


Order::Order()
{
strcpy(customerName,

"Total");
orderQuantity=0;
};

void Order::setValues(char customerName[], int orderQuantity) //added the parameters, and Order::
{
	cout << "Enter customer name ";
	cin >> customerName;
	cout << "Enter quantity ordered: ";
	cin >> orderQuantity;
}

void Order::displayValues()
{
	cout << customerName[20] << " ordered " << orderQuantity << " items." << endl; //added a bunch of <<
}
Order yearEndTotal(Order summary, Order oneOrder)
{
	summary.orderQuantity += oneOrder.orderQuantity; //added +
	return(summary);
}

void main()
{
	const int ORDERS = 3; //changed 10 to 3
	Order anOrder[ORDERS];
	Order summary;
	int x;
	for(x=0; x<ORDERS; ++x) //removed ;
	anOrder[x].setValues(char customerName[20], int orderQuantity);
	for(x = 0; x< ORDERS; ++x)
	{
	summary = yearEndTotal(summary,anOrder[x]);
	}
	summary.displayValues();
	getch();
}


The bolded code near the bottom gives me the following errors:

C2059: Syntax error: ')'
C2144: Syntax errror: 'char' should be preceeded by ')'
(although it looks to me like it is?)
C2660: 'Order::setValues': function does not take 0 arguements
Intellisense: too few arguements in function call
Intellisense: type name is not allowed

Thanks in advance!
1
2
3
4
int orderQuantity;
char customerName[20];

anOrder[x].setValues(customerName, orderQuantity);
You're declaring two variables while calling a function. You can't do that.
If you moved declarations of customerName and orderQuantity, there would be another problem. orderQuantity would not get any input since setValues only has a copy of it and cannot affect the original. This could be solved with passing by reference. Although that's still not good. Order already has members customerName and orderQuantity, so why would you pass anything at all? You need to change setValues to take no arguments.
Oops. Didn't read the what the function was doing properly. Although my syntax still stands.

I would argue that you still need arguments in setValues, but the function body should be more like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// Function
void setValues(char name[], int quantity)
{
   for(int i=0; i<20; i++)
   {
      customerName[i] = name[i];
   }

   orderQuantity = quantity;
}

// In main

int quantity;
char name [20];

cout << "Enter name: ";
cin >> name;
cout << "Enter quantity: ";
cin >> quantity;

anOrder[x].setValues(name, quantity);


I would probably tidy that up to have some constants for max array sizes etc.
Last edited on
Well don't that beat all. I just removed all the paramters from setValues() and it worked, though I swear I had them empty before and I got an unresolved externals error.

Oh well, it works now. The code doesn't need to be necessarily the "best" way of doing it, it just needs to work as intended, so I'll leave it as is.

Thanks for the help!
Topic archived. No new replies allowed.