I have gotten my program to the point that it allows me to enter the data I want to enter. However, I do not know how to get the program to loop back correctly and ask for salesperson number and stop the loop when I enter -1.
Help!

#include <stdio.h>
     
      int main()
 {
      const int PEOPLE = 5, PRODUCTS = 6;
      double sales[ 5 ][ 6 ] = { 0.0 }, value;
      double totalSales, productSales[ 6 ] = { 0.0 };
      int salesPerson, product;
      int  i, j;
 
      salesPerson = 0;
      product =0;
      value = 0;
      printf("Enter the sales person (1 - 4) Enter -1 to end       input:");
      scanf("%d", &salesPerson);
      while ( salesPerson != -1 ) {
	    printf("Enter product number (1 - 5):\n");
            scanf("%d" ,&product) ;
	    printf("Enter total sales of product:\n");
	    scanf("%d", &value);
	    sales[ salesPerson ][ product ] += value;
  			     
      }
       	    printf("\nThe total sales for each sales person ");
  	    printf("are displayed\nat the end of each row,");
  	    printf("and the total sales for each\nproduct ");
  	    printf("are displayed at the bottom of each column.\n");
  	    printf("%10d", 1);
            printf("%10d", 2);
	    printf("%10d", 3);
	    printf("%10d", 4);
	    printf("%10d", 5);
	    printf("%12d", "Total\n");
            for ( i = 1; i < PEOPLE; ++i ) {
                totalSales = 0.0;
		printf("%d", i);		
                for ( j = 1; j < PRODUCTS; ++j ) {
  		    totalSales += sales[ i ][ j ];   
  		    printf("%.2f", sales [i][j]);
  		    productSales[ j ] += sales[ i ][ j ];   
                 }
		     printf("%.2f", sales[i][j]);
  
            }
                 printf( "%.2f,","\nTotal",productSales );

                 for ( j = 2; j < PRODUCTS; ++j ) {
                     printf("%.2f", productSales[j]);
	         }
return 0;
  }

Recommended Answers

All 6 Replies

Change your while loop to a do/while loop with the same loop condition. Move your prompt for the salesperson number into the loop. That way it's called the first time through and any subsequent times.

Line 20 has the incorrect format specifier for a double, it should be %lf
instead of %d.

I am getting a compile error I do not understand on the line printf statement right below the end of the do while loop. It says printf identifier. Help?

#include <stdio.h>
     
      int main()
 {
      const int PEOPLE = 5, PRODUCTS = 6;
      double sales[ 5 ][ 6 ] = { 0.0 }, value;
      double totalSales, productSales[ 6 ] = { 0.0 };
      int salesPerson, product;
      int  i, j;
 
      salesPerson = 0;
      product =0;
      value = 0;
      
      do while ( salesPerson != -1 ) {
        printf("Enter the sales person (1 - 4) Enter -1 to end       input:");
        scanf("%d", &salesPerson);
	    printf("Enter product number (1 - 5):\n");
            scanf("%d" ,&product) ;
	    printf("Enter total sales of product:\n");
	    scanf("%lf", &value);
	    sales[ salesPerson ][ product ] += value;
  			     
      }
       	    printf("The total sales for each sales person");
  	    printf("are displayed\nat the end of each row,");
  	    printf("and the total sales for each\nproduct ");
  	    printf("are displayed at the bottom of each column.\n");
  	    printf("%10d", 1);
            printf("%10d", 2);
	    printf("%10d", 3);
	    printf("%10d", 4);
	    printf("%10d", 5);
	    printf("%12d", "Total\n");
            for ( i = 1; i < PEOPLE; ++i ) {
                totalSales = 0.0;
		printf("%d", i);		
                for ( j = 1; j < PRODUCTS; ++j ) {
  		    totalSales += sales[ i ][ j ];   
  		    printf("%.2f", sales [i][j]);
  		    productSales[ j ] += sales[ i ][ j ];   
                 }
		     printf("%.2f", sales[i][j]);
  
            }
                 printf( "%.2f,","\nTotal",productSales );

                 for ( j = 2; j < PRODUCTS; ++j ) {
                     printf("%.2f", productSales[j]);
	         }
return 0;
  }

Although not perfect, your formatting is much better. Thank you.

I am getting a compile error I do not understand on the line printf statement right below the end of the do while loop. It says printf identifier. Help?

That's because there is no such thing as a do while statement. It is a do-while loop:

do
{
    -- stuff to do --
} while (exit-condition);

Did you look it up before using it?

I am trying to compile my file in Visual c++ 2008 and it i slinking to a file wit a similar name in another project...It says main is already defined there. Anyway here is my corrected code...Thank you Walt!

#include <stdio.h>
     
      int main()
 {
      const int PEOPLE = 5, PRODUCTS = 6;
      double sales[ 5 ][ 6 ] = { 0.0 }, value;
      double totalSales, productSales[ 6 ] = { 0.0 };
      int salesPerson, product;
      int  i, j;
 
      salesPerson = 0;
      product =0;
      value = 0;
      
      do 
	  {
          printf("Enter the sales person (1 - 4) Enter -1 to end       input:");
          scanf("%d", &salesPerson);
	  printf("Enter product number (1 - 5):\n");
          scanf("%d" ,&product) ;
	  printf("Enter total sales of product:\n");
	  scanf("%lf", &value);
	  sales[ salesPerson ][ product ] += value;
  	  }
	  while (salesPerson = -1);

       	    printf("The total sales for each sales person");
  	    printf("are displayed\nat the end of each row,");
  	    printf("and the total sales for each\nproduct ");
  	    printf("are displayed at the bottom of each column.\n");
  	    printf("%10d", 1);
            printf("%10d", 2);
	    printf("%10d", 3);
	    printf("%10d", 4);
	    printf("%10d", 5);
	    printf("%12d", "Total\n");
            for ( i = 1; i < PEOPLE; ++i ) {
                totalSales = 0.0;
		printf("%d", i);		
                for ( j = 1; j < PRODUCTS; ++j ) {
  		    totalSales += sales[ i ][ j ];   
  		    printf("%.2f", sales [i][j]);
  		    productSales[ j ] += sales[ i ][ j ];   
                 }
		     printf("%.2f", sales[i][j]);
  
            }
                 printf( "%.2f,","\nTotal",productSales );

                 for ( j = 2; j < PRODUCTS; ++j ) {
                     printf("%.2f", productSales[j]);
	         }
         
return 0;
  }

Although not perfect, your formatting is much better. Thank you.


That's because there is no such thing as a do while statement. It is a do-while loop:

do
{
    -- stuff to do --
} while (exit-condition);

Did you look it up before using it?

Here is the output I am getting when i run my program. Any Help would be appreciated.:
Enter the sales person (1 - 4) Enter -1 to end input:1
Enter product number (1 - 5):
2
Enter total sales of product:
44
Enter the sales person (1 - 4) Enter -1 to end input:2
Enter product number (1 - 5):
2
Enter total sales of product:
33
Enter the sales person (1 - 4) Enter -1 to end input:3
Enter product number (1 - 5):
2
Enter total sales of product:
55
Enter the sales person (1 - 4) Enter -1 to end input:4
Enter product number (1 - 5):
2
Enter total sales of product:
66
The total sales for each sales personare displayed
at the end of each column,and the total sales for each product are displayed at
the bottom of each row.
1 2 3 4 1509563610.0000000.0000000.0000000.0
0000020.0000000.0000000.0000000.00000030.0000000.0000000.0000000.00000040.000000
0.0000000.000000-92559631349317831000000000000000000000000000000000000000000000.
0000000.000000,0.0000000.0000000.000000Press any key to continue . . .

#include <stdio.h>
     
      int main()
 {
      const int PEOPLE = 4, PRODUCTS = 5;
      double sales[5 ][ 4 ] = { 0.0 }, value;                         /*people in columns*/
      double totalSales, productSales[ 6 ] = { 0.0 };
      int salesPerson, product;
      int  i, j,counter;
 
      salesPerson = 0;
      product =0;
      value = 0;
      counter = 1;
	  totalSales =0;
      do 
	  {
          printf("Enter the sales person (1 - 4) Enter -1 to end       input:");
          scanf("%d", &salesPerson);
	      printf("Enter product number (1 - 5):\n");
          scanf("%d" ,&product) ;
	      printf("Enter total sales of product:\n");
	      scanf("%2f", &value);
	      sales[ product ][ salesPerson ] += value;             
  	  }
	  while (++counter <=4);

       	printf("The total sales for each sales person");
  	    printf("are displayed\nat the end of each column,");
  	    printf("and the total sales for each product ");
  	    printf("are displayed at the bottom of each row.\n");
  	    printf("%10d", 1);
        printf("%10d", 2);
	    printf("%10d", 3);
	    printf("%10d", 4);
	   	printf("%12d", "Total\n");
            for ( i = 1; i < PRODUCTS; ++i ) {
                
		printf("%d", i);		
                for ( j = 1; j < PEOPLE; ++j ) {
					totalSales+= sales[ i ][ j ];   
  		    printf("%2f", sales [i][j]);
  		    productSales[i] += sales[ i ][ j ];   
                 }
		     printf("%2f", sales[i][j]);
  
            }
                 printf( "%2f,","\nTotal",productSales );

                 for ( i = 2; i < PRODUCTS; ++i ) {
                     printf("%2f", productSales[i]);
	         }
         
return 0;
  }

Line 25 is incorrect.
It should be: while (salesPerson != -1); what you were doing was assigning not comparing. I'm not sure how you were getting output at all with that code.
Within your while you might want to enclose lines 19-24 in an if statement with if(salesPerson != -1) so that you can skip over the data entry if the user has opted to exit.
See if these two help towards the other problem but there's probably still some inconsistencies there.

EDIT: note the != I was mixed up.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.