c++ two-dimensional arrays

syntax error

  • (2 Pages)
  • +
  • 1
  • 2

27 Replies - 4528 Views - Last Post: 29 June 2008 - 04:57 PM Rate Topic: -----

#1 chaoticabyss99  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 94
  • Joined: 14-June 08

c++ two-dimensional arrays

Post icon  Posted 24 June 2008 - 11:42 PM

I'm trying to use a two-dimensional array to display information from 4 different salespeople who are selling 5 different products. The program is supposed to show 4 columns for the salespeople and 5 rows for the differents products, then cross total all the columns and then the rows.

I am getting an error message from line 62 stating: error C2143: syntax error : missing ';' before '+=' and I dont' know how to fix it.

Here is the code


#include <iostream> 
using std::cin; 
using std::cout; 
using std::endl; 

#include <conio.h>

#include <iomanip>
using std::fixed;
using std::setw; 
using std::setprecision; 
using std::showpoint;

int main()
{
	const static int PEOPLE = 5; //number of sales people (4)
	const static int PRODUCTS = 6;	//number of different products (5)

	int sales[5][6] = {{1, 2, 3, 4}, {1, 2, 3, 4, 5}};
	double value;
	double totalSales;
	double productSales[PRODUCTS] = {0.0};
	int salesPerson;
	int product;

	cout << " Enter the salesperson's number (1-4), the product number (1-5), and total product sales.\n "
		 << "Enter -1 for salesperson to end input.\n";
	cin >> salesPerson;

	while (salesPerson != -1)
	{
		cin >> product >> value;

			value++;
		
		cin >> salesPerson;
	}

	cout << "\nThe total sales for each salesperson are displayed at the "
		<< "end of each row,\n" << "and the total sales for each product "
		<< "are displayed at the bottom of each column.\n " << setw( 12 ) 
		<< 1 << setw( 12 ) << 2 << setw( 12 ) << 3 << setw( 12 ) << 4 
		<< setw( 12 ) << 5 << setw( 13 ) << "Total\n" << fixed << showpoint;

	for (int i = 1; i < 4; i++)
	{
		totalSales = 0.0;
		cout << i;

		for (int j = 1; j < 5; j++)
		{
			++totalSales;

			cout << setw (12) << setprecision (2) << sales [i][j];

			+= productSales;
		}

		cout << setw (12) << setprecision (2) << totalSales << '\n';
   }

	cout << "\nTotal" << setw( 8 ) << setprecision( 2 ) 
	  << productSales[ 1 ];

   // display total product sales
   for ( int j = 2; j < totalSales; j++ )
	  cout << setw( 12 ) << setprecision( 2 ) << productSales[ j ];

   cout << endl;
   
   _getch();
} 





Any help would be appreciated!

View Postchaoticabyss99, on 24 Jun, 2008 - 11:41 PM, said:

I'm trying to use a two-dimensional array to display information from 4 different salespeople who are selling 5 different products. The program is supposed to show 4 columns for the salespeople and 5 rows for the differents products, then cross total all the columns and then the rows.

I am getting an error message from line 62 stating: error C2143: syntax error : missing ';' before '+=' and I dont' know how to fix it.

Here is the code


#include <iostream> 
using std::cin; 
using std::cout; 
using std::endl; 

#include <conio.h>

#include <iomanip>
using std::fixed;
using std::setw; 
using std::setprecision; 
using std::showpoint;

int main()
{
	const static int PEOPLE = 5; //number of sales people (4)
	const static int PRODUCTS = 6;	//number of different products (5)

	int sales[5][6] = {{1, 2, 3, 4}, {1, 2, 3, 4, 5}};
	double value;
	double totalSales;
	double productSales[PRODUCTS] = {0.0};
	int salesPerson;
	int product;

	cout << " Enter the salesperson's number (1-4), the product number (1-5), and total product sales.\n "
		 << "Enter -1 for salesperson to end input.\n";
	cin >> salesPerson;

	while (salesPerson != -1)
	{
		cin >> product >> value;

			value++;
		
		cin >> salesPerson;
	}

	cout << "\nThe total sales for each salesperson are displayed at the "
		<< "end of each row,\n" << "and the total sales for each product "
		<< "are displayed at the bottom of each column.\n " << setw( 12 ) 
		<< 1 << setw( 12 ) << 2 << setw( 12 ) << 3 << setw( 12 ) << 4 
		<< setw( 12 ) << 5 << setw( 13 ) << "Total\n" << fixed << showpoint;

	for (int i = 1; i < 4; i++)
	{
		totalSales = 0.0;
		cout << i;

		for (int j = 1; j < 5; j++)
		{
			++totalSales;

			cout << setw (12) << setprecision (2) << sales [i][j];

			+= productSales;
		}

		cout << setw (12) << setprecision (2) << totalSales << '\n';
   }

	cout << "\nTotal" << setw( 8 ) << setprecision( 2 ) 
	  << productSales[ 1 ];

   // display total product sales
   for ( int j = 2; j < totalSales; j++ )
	  cout << setw( 12 ) << setprecision( 2 ) << productSales[ j ];

   cout << endl;
   
   _getch();
} 





Any help would be appreciated!




Line 62 is the += productSales;

Is This A Good Question/Topic? 0
  • +

Replies To: c++ two-dimensional arrays

#2 no2pencil  Icon User is offline

  • Original Digital Gansta
  • member icon

Reputation: 4503
  • View blog
  • Posts: 24,971
  • Joined: 10-May 07

Re: c++ two-dimensional arrays

Posted 24 June 2008 - 11:50 PM

You have a space between the text sales & the array.
cout << setw (12) << setprecision (2) << sales [i][j];



Change that line like so :

cout << setw (12) << setprecision (2) << sales[i][j];


Was This Post Helpful? 0
  • +
  • -

#3 chaoticabyss99  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 94
  • Joined: 14-June 08

Re: c++ two-dimensional arrays

Posted 24 June 2008 - 11:53 PM

View Postno2pencil, on 24 Jun, 2008 - 11:50 PM, said:

You have a space between the text sales & the array.
cout << setw (12) << setprecision (2) << sales [i][j];



Change that line like so :

cout << setw (12) << setprecision (2) << sales[i][j];




I changed it so now there is no space, however, I am still getting the same error message. Any thoughts?
Was This Post Helpful? 0
  • +
  • -

#4 no2pencil  Icon User is offline

  • Original Digital Gansta
  • member icon

Reputation: 4503
  • View blog
  • Posts: 24,971
  • Joined: 10-May 07

Re: c++ two-dimensional arrays

Posted 24 June 2008 - 11:57 PM

Oh, you know what, I didn't see the other spaces also.

Replace it with this text :
cout << setw(12) << setprecision(2) << sales[i][j];  



Also, note that you have created main to have a return type of int, yet I don't see a return at the end of your main loop.
Was This Post Helpful? 0
  • +
  • -

#5 chaoticabyss99  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 94
  • Joined: 14-June 08

Re: c++ two-dimensional arrays

Posted 25 June 2008 - 12:03 AM

View Postno2pencil, on 24 Jun, 2008 - 11:57 PM, said:

Oh, you know what, I didn't see the other spaces also.

Replace it with this text :
cout << setw(12) << setprecision(2) << sales[i][j];  



Also, note that you have created main to have a return type of int, yet I don't see a return at the end of your main loop.



OK, I got rid of the spaces and added a return 0;, however, I am still getting the same error message.

This is driving me nuts!!
Was This Post Helpful? 0
  • +
  • -

#6 AmitTheInfinity  Icon User is offline

  • C Surfing ∞
  • member icon

Reputation: 109
  • View blog
  • Posts: 1,530
  • Joined: 25-January 07

Re: c++ two-dimensional arrays

Posted 25 June 2008 - 12:25 AM

View Postchaoticabyss99, on 25 Jun, 2008 - 12:33 PM, said:

View Postno2pencil, on 24 Jun, 2008 - 11:57 PM, said:

Oh, you know what, I didn't see the other spaces also.

Replace it with this text :
cout << setw(12) << setprecision(2) << sales[i][j];  



Also, note that you have created main to have a return type of int, yet I don't see a return at the end of your main loop.



OK, I got rid of the spaces and added a return 0;, however, I am still getting the same error message.

This is driving me nuts!!



as you said in first post, your line 62 is : += productSales; what this line is expected to do?
Was This Post Helpful? 0
  • +
  • -

#7 chaoticabyss99  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 94
  • Joined: 14-June 08

Re: c++ two-dimensional arrays

Posted 25 June 2008 - 12:49 AM

View PostAmitTheInfinity, on 25 Jun, 2008 - 12:25 AM, said:

View Postchaoticabyss99, on 25 Jun, 2008 - 12:33 PM, said:

View Postno2pencil, on 24 Jun, 2008 - 11:57 PM, said:

Oh, you know what, I didn't see the other spaces also.

Replace it with this text :
cout << setw(12) << setprecision(2) << sales[i][j];  



Also, note that you have created main to have a return type of int, yet I don't see a return at the end of your main loop.



OK, I got rid of the spaces and added a return 0;, however, I am still getting the same error message.

This is driving me nuts!!



as you said in first post, your line 62 is : += productSales; what this line is expected to do?



I'm not too sure what it is supposed to do, really. Weird, I know. I was hoping it would compile correctly so I could see what the output looked like and then modify it from there.

Here is how the problem reads:
Use a two-dimensional array to solve the following problem. A company has four salespeople (1-4) who sell five different products (1-5). Once a day, each salesperson passes in a slip for each different type of product sold. Each slip contains the following:
a) The salesperson number
B) The product number
c) The total dollar value of that product sold that day

Thus, each salesperson passes in between 0 and 5 sales slips per day. Assume that all the information from all of the slips for last month is available. Write a program that will read all this information for last month's sales and summarize the total sales by salesperson by product. All totals should be stored in the two-dimensional array sales. After processing all the information for last month, print the results in tabular format with each of the columns representing a particular salesperson and each of the rows representing a particular product. Cross total each row to get the total sales of each product for last month; cross total each column to get the total sales by salesperson for last month. Your tabular printout should include these cross totals to the right of the totaled tows and to the bottom of the totaled columns.
Was This Post Helpful? 0
  • +
  • -

#8 no2pencil  Icon User is offline

  • Original Digital Gansta
  • member icon

Reputation: 4503
  • View blog
  • Posts: 24,971
  • Joined: 10-May 07

Re: c++ two-dimensional arrays

Posted 25 June 2008 - 12:58 AM

What AmitTheInfinity is asking, I think, is what do you expect that line of code to perform?

With the equals sign in that statement, something must absorb the value of the incremented value of the variable.

Something = Something

If you just want the variable to increment by one, then just use something++
Was This Post Helpful? 0
  • +
  • -

#9 chaoticabyss99  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 94
  • Joined: 14-June 08

Re: c++ two-dimensional arrays

Posted 25 June 2008 - 01:22 AM

View Postno2pencil, on 25 Jun, 2008 - 12:58 AM, said:

What AmitTheInfinity is asking, I think, is what do you expect that line of code to perform?

With the equals sign in that statement, something must absorb the value of the incremented value of the variable.

Something = Something

If you just want the variable to increment by one, then just use something++


OK, I tried to do productSales++, but it gives me the error message: error C2105: '++' needs l-value
Was This Post Helpful? 0
  • +
  • -

#10 no2pencil  Icon User is offline

  • Original Digital Gansta
  • member icon

Reputation: 4503
  • View blog
  • Posts: 24,971
  • Joined: 10-May 07

Re: c++ two-dimensional arrays

Posted 25 June 2008 - 01:28 AM

View Postchaoticabyss99, on 25 Jun, 2008 - 04:22 AM, said:

OK, I tried to do productSales++, but it gives me the error message: error C2105: '++' needs l-value

can you provide us with the code... it's easier to troubleshoot.
Was This Post Helpful? 0
  • +
  • -

#11 chaoticabyss99  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 94
  • Joined: 14-June 08

Re: c++ two-dimensional arrays

Posted 25 June 2008 - 01:31 AM

View Postno2pencil, on 25 Jun, 2008 - 01:28 AM, said:

View Postchaoticabyss99, on 25 Jun, 2008 - 04:22 AM, said:

OK, I tried to do productSales++, but it gives me the error message: error C2105: '++' needs l-value

can you provide us with the code... it's easier to troubleshoot.




#include <iostream> 
using std::cin; 
using std::cout; 
using std::endl; 

#include <conio.h>

#include <iomanip>
using std::fixed;
using std::setw; 
using std::setprecision; 
using std::showpoint;

int main()
{
	const static int PEOPLE = 5; //number of sales people (4)
	const static int PRODUCTS = 6;	//number of different products (5)

	int sales[5][6] = {{1, 2, 3, 4}, {1, 2, 3, 4, 5}};
	double value;
	double totalSales;
	double productSales[PRODUCTS] = {0.0};
	int salesPerson;
	int product;

	cout << " Enter the salesperson's number (1-4), the product number (1-5), and total product sales.\n "
		 << "Enter -1 for salesperson to end input.\n";
	cin >> salesPerson;

	while (salesPerson != -1)
	{
		cin >> product >> value;

			value++;
		
		cin >> salesPerson;
	}

	cout << "\nThe total sales for each salesperson are displayed at the "
		<< "end of each row,\n" << "and the total sales for each product "
		<< "are displayed at the bottom of each column.\n " << setw(12) 
		<< 1 << setw(12) << 2 << setw(12) << 3 << setw(12) << 4 
		<< setw(12) << 5 << setw(13) << "Total\n" << fixed << showpoint;

	for (int i = 1; i < 4; i++)
	{
		totalSales = 0.0;
		cout << i;

		for (int j = 1; j < 5; j++)
		{
			++totalSales;

			cout << setw(12) << setprecision(2) << sales[i][j];

			productSales++;
		}

		cout << setw(12) << setprecision(2) << totalSales << '\n';
   }

	cout << "\nTotal" << setw(8) << setprecision(2) 
	  << productSales[1, 2, 3, 4, 5];

   // display total product sales
   for ( int j = 2; j < totalSales; j++ )
	  cout << setw(12) << setprecision(2) << productSales[j];

   cout << endl;
   
   _getch();

}



Was This Post Helpful? 0
  • +
  • -

#12 no2pencil  Icon User is offline

  • Original Digital Gansta
  • member icon

Reputation: 4503
  • View blog
  • Posts: 24,971
  • Joined: 10-May 07

Re: c++ two-dimensional arrays

Posted 25 June 2008 - 01:36 AM

double productSales[PRODUCTS] = {0.0};

You've defined the variable productSales as an array. What part of the array are you wanting to increment with productSales++?
Was This Post Helpful? 0
  • +
  • -

#13 chaoticabyss99  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 94
  • Joined: 14-June 08

Re: c++ two-dimensional arrays

Posted 25 June 2008 - 01:50 AM

View Postno2pencil, on 25 Jun, 2008 - 01:36 AM, said:

double productSales[PRODUCTS] = {0.0};

You've defined the variable productSales as an array. What part of the array are you wanting to increment with productSales++?



I don't really know at this point. It's late and I think I need some sleep and start fresh tomorrow. Thank you for your time tonight (this morning). I'll see what I can do tomorrow; I'm sure I'll be posting more questions. Thanks.
Was This Post Helpful? 0
  • +
  • -

#14 chaoticabyss99  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 94
  • Joined: 14-June 08

Re: c++ two-dimensional arrays

Posted 25 June 2008 - 12:49 PM

OK, I've made some changes and I think I'm making some progress, however, I'm still not getting done what I want to get done. Here is the code:


#include <iostream> 
using std::cin; 
using std::cout; 
using std::endl; 

#include <conio.h>

#include <iomanip>
using std::fixed;
using std::setw; 
using std::setprecision; 
using std::showpoint;

int main()
{
	const int PEOPLE = 5;   //number of sales people (4)
	const int PRODUCTS = 6; //number of different products (5)

	int sales[5][6] = {{1}, {2}, {3}, {4}};
	double value;
	double totalSales;
	double productSales[PRODUCTS] = {0.0};
	int salesPerson;
	int product;

	cout << "Enter the salesperson's number (1-4),\nthe product number (1-5),"
		 << " and total product sales.\n "
		 << "\nEnter -1 for salesperson to end input.\n";
	cin >> salesPerson;

	while (salesPerson != -1)
	{
		cin >> product >> value;

			value++;
		
		cin >> salesPerson;
	}

	cout << "\nThe total sales for each salesperson are displayed at the "
		<< "bottom of each column,and the total sales for each product "
		<< "are displayed at the end of each row.\n\n " << setw(12) 
		<< 1 << setw(12) << 2 << setw(12) << 3 << setw(12) << 4 
		<< setw(12) << "Total\n" << fixed << showpoint;

	for (int i = 1; i < 6; i++)
	{
		totalSales = 0.0;
		cout << i;

		for (int j = 1; j < 5; j++)
		{
			++totalSales;

			cout << setw(12) << setprecision(2) << sales[i][j];

		}

		cout << setw(12) << setprecision(2) << totalSales << '\n';
	}

	cout << "\nTotal" << setw(8) << setprecision(2) 
	  << productSales[1];
	
   // display total product sales
   for (int j = 2; j < totalSales; j++)
	  cout << setw(12) << setprecision(2) << productSales[j];

   cout << endl;
   
   _getch();

} 





and the output I'm getting is:


Enter the salesperson's number (1-4),
the product number (1-5), and total product sales.

Enter -1 for salesperson to end input.
4
5
1236
1
4
5896
3
2
1247
2
3
4756
-1

The total sales for each salesperson are displayed at the bottom of each column,
and the total sales for each product are displayed at the end of each row.

				   1		   2				3				   4	  Total
1				 0		   0				 0				  0		 4.00
2				 0		   0				 0				  0		 4.00
3				 0		   0				 0				  0		 4.00
4				 0		   0				 0				  0		 4.00
5  -858993460		   6  -858993460  -858993460		 4.00

Total	0.00		0.00		0.00		0.00





Any suggestions as to what I'm not doing correctly?
Was This Post Helpful? 0
  • +
  • -

#15 chaoticabyss99  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 94
  • Joined: 14-June 08

Re: c++ two-dimensional arrays

Posted 25 June 2008 - 01:39 PM

I have yet again made some changes and it seems like I'm getting somewhere because my output is looking a little better. However, I am still needing some help.

Updated code


#include <iostream> 
using std::cin; 
using std::cout; 
using std::endl;
using std::ios;

#include <conio.h>

#include <iomanip>
using std::fixed;
using std::setw; 
using std::setprecision; 
using std::showpoint;

int main()
{
	const int PEOPLE = 5;
	const int PRODUCTS = 6;
	double sales[PEOPLE][PRODUCTS] = {0.0};
	double value;
	double totalSales;
	double productSales[PRODUCTS] = {0.0};
	int salesPerson;
	int product;

	cout << "Enter the salesperson's number (1-4),\nthe product number (1-5),"
		 << " and total product sales.\n "
		 << "\nEnter -1 for salesperson to end input.\n";
	cin >> salesPerson;

	while (salesPerson != -1)
	{
		cin >> product >> value;

			value++;
		
		cin >> salesPerson;
	}

	cout << "\nThe total sales for each salesperson are displayed at the "
		<< "bottom of each column,and the total sales for each product "
		<< "are displayed at the end of each row.\n\n " << setw(12) 
		<< 1 << setw(12) << 2 << setw(12) << 3 << setw(12) << 4 
		<< setw(12) << "Total\n" << fixed << showpoint;

	for (int i = 1; i < 6; i++)
	{
		totalSales = 0.0;
		cout << i;

		for (int j = 1; j < 5; j++)
		{
			++totalSales;

			cout << setw(12) << setprecision(2) << sales[PEOPLE][PRODUCTS];

		}

		cout << setw(12) << setprecision(2) << totalSales << '\n';
	}

	cout << "\nTotal" << setw(8) << setprecision(2) 
	  << productSales[1];
	
   // display total product sales
   for (int j = 1; j < totalSales; j++)
	  cout << setw(12) << setprecision(2) << productSales[j];

   cout << endl;
   
   _getch();

} 





In my output, all of the fields are 0.00 except for the Total column that's supposed to be adding the values in each row. The Total fields show '4.00'.

Please help!
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2