Hello world, ok so i need a suggestion for my code.


this part of my code i need help

cout <<"\nEnter Each Sales Person # : ";
		 cin >> salesPerson;
	     
		 while ( salesPerson != -1 ) {
			 cout << "\nPlease Enter the product number: "; 
		     cin >> product;
          sales[ salesPerson ][ product ] += value;
		  cin >> salesPerson; // <== this part is my problem
		 }

==>>I need a suggestion: I want to enter a "product number" that wont needed me to enter the salesPerson # every time I enter product numbers however i still want to exit the loop. (as you know only by entering -1 you can exit from the loop and i needed to put salesPerson there to exit the loop but it looks so weird that everytime i enter a product number i also needed to enter the salesperson number)

**I want an output like this

Enter Each Sales Person # : 1

Please Enter the product number: 123

Please Enter the product number: 456

Please Enter the product number: 789

Enter Each Sales Person # : -1

**Not like this

Enter Each Sales Person # : 1

Please Enter the product number: 123
0

Please Enter the product number: 456
0

Please Enter the product number: 789
-1

Any suggestion: Thank you very much

=================================================
This is the whole code if you wanna see it

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


	   cout <<"\nEnter Each Sales Person # : ";
		 cin >> salesPerson;
	     
		 while ( salesPerson != -1 ) {
			 cout << "\nPlease Enter the product number: ";
		     cin >> product;
          sales[ salesPerson ][ product ] += value;
		  cin >> salesPerson;
		 }

  
    cout << "-----------------------------------------------------"
                  << "--------------------------\n\n\n"
                  << "Sales |\t<---------------------- Products -----------"
                  << "---------->\n"
                  << "Person|\n"
                  << "_____________________________________________________"
                  << "__________________________\n"
          << setw( 10 ) << 1 << setw( 13 ) << 2
          << setw( 13 ) << 3 << setw( 13 ) << 4
          << setw( 13 ) << "Total\n"
                  << "_____________________________________________________"
                  << "__________________________\n\n";
        
   
   for ( int i = 1; i < SALESP ; ++i ) {
      totalSales = 0.0;
      cout << i;
      
      for ( int j = 1; j < PRODUCTS; ++j ) {
         totalSales += sales[ i ][ j ];
         cout << setw( 10 ) << setprecision( 2 )
              << sales[ i ][ j ];
         productSales[ j ] += sales[ i ][ j ];
      }

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

   for ( int j = 2; j < PRODUCTS; ++j )
      cout << setw( 10 ) << setprecision( 2 )
           << productSales[ j ];
   cout << "\n_____________________________________________________"
        << "__________________________\n"
        << setw( 80 ) << " Grand Total\n"
        << setw( 80 ) << "-----------\n";
   cout << "Total " << setw( 6 ) << setprecision( 2 )
        << productSales[ 1 ];
      
   for ( int j = 2; j < PRODUCTS; ++j )
      cout << setw( 12 ) << setprecision( 2 )
           << productSales[ j ];
                for ( int j = 0; j < PRODUCTS; ++j)
            grandTotal += productSales[j];
                 cout << setw( 13 ) << grandTotal
                          << "\n" << endl;



	//Print programmer's name
	cout << endl << "Programmer: Trowa Barton" <<endl;

	return 0;
}

Well, you can try 2 things:

1) Ask the user for how many products he wants.

2) Let entering -1 for a product be the end of the product list.

ie:
1)

while ( salesPerson != -1 ) {
          cout << "Enter the number of products";
          cin  >> productCount;      
 
          for (i = 0; i < productCount; i++) {
	    cout << "\nPlease Enter the product number: ";
            cin >> product;
            sales[ salesPerson ][ product ] += value;
          }

          cout <<"\nEnter Each Sales Person # : ";
          cin >> salesPerson;
  }

2) The second solution is the same how you have provided an option of entering -1 for sales person. You could use a similar method for this too.

Is this what you were looking for?

This article has been dead for over six months. Start a new discussion instead.