Hello,
I am attempting to rewrite a program in c. Here is what I have so far. Thanks in advance for assistance.

#include <stdio.h>
  
       void sales[ salesPerson ][ product ] += value;
  
      int main()
  
      {
  
      const int PEOPLE = 5, PRODUCTS = 6;
  
      double sales[ PEOPLE ][ PRODUCTS ] = { 0.0 }, value,
  
      totalSales, productSales[ PRODUCTS ] = { 0.0 };
  
      int salesPerson, product;
  
       
  
      printf("Enter the sales person (1 - 4), ");
  
      printf("product number (1 - 5)\nand total sales.");
  
     printf("Enter -1 for the sales person to end input.\n");
  
      scanf("%d", salesPerson);
  
       
  
      while ( salesPerson != -1 ) {
  
      scanf("%d" ,&product ;
	  scanf("%d", &value);
  
      sales[ salesPerson ][ product ] += value;
  
      scanf("%d", &salesPerson);
  
      }
  
       
  
      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");
  
      << setw( 10 ) << 1 << setw( 10 ) << 2					/*please help me with these four lines in converting them to c code*/
  
      << setw( 10 ) << 3 << setw( 10 ) << 4
  
      << setw( 10 ) << 5 << setw( 12 ) << "Total\n"
  
      << setiosflags( ios::fixed | ios::showpoint );
  
       
  
      for ( int i = 1; i < PEOPLE; ++i ) {
  
      totalSales = 0.0;
  printf("%d", &i);		/*is this correct c for cout >>i*/
  
       
  
      for ( int j = 1; j < PRODUCTS; ++j ) {
  
      totalSales += sales[ i ][ j ];   /*do I need to do something with this above main*/
  
      cout << setw( 10 ) << setprecision( 2 )  /*pleae help convert to c*/
  
      << sales[ i ][ j ];
  
      productSales[ j ] += sales[ i ][ j ];   /*pleae help convert to c*/
  
      }
  
       
  
      cout << setw( 10 ) << setprecision( 2 )
  
      << totalSales << '\n';
  
      }
  
       
  
      cout << "\nTotal" << setw( 6 ) << setprecision( 2 )		/*please convert to c*/
  
      << productSales[ 1 ];
  
       
  
      for ( int j = 2; j < PRODUCTS; ++j )
  
      cout << setw( 10 ) << setprecision( 2 )
  
      << productSales[ j ];
  
       
  
      cout << endl;
  
      return 0;
  
      }

Now here is the c++ code I am working from. Help is appreciated.

#include <iostream>

using std::cout;
using std::cin;
using std::endl;
using std::ios;

#include <iomanip>

using std::setiosflags;
using std::setprecision;
using std::setw;

int main()
{
   const int PEOPLE = 5, PRODUCTS = 6;
   double sales[ PEOPLE ][ PRODUCTS ] = { 0.0 }, value,
         totalSales, productSales[ PRODUCTS ] = { 0.0 };
   int salesPerson, product;
   
   cout << "Enter the sales person (1 - 4), "
        << "product number (1 - 5)\nand total sales."
        << "Enter -1 for the sales person to end input.\n";
   cin >> salesPerson;
   
   while ( salesPerson != -1 ) {
      cin >> product >> value;
      sales[ salesPerson ][ product ] += value;
      cin >> salesPerson;
   }

   cout << "\nThe total sales for each sales person "
        << "are displayed\nat the end of each row,"
        << "and the total sales for each\nproduct "
        << "are displayed at the bottom of each column.\n"
        << setw( 10 ) << 1 << setw( 10 ) << 2
        << setw( 10 ) << 3 << setw( 10 ) << 4
        << setw( 10 ) << 5 << setw( 12 ) << "Total\n"
        << setiosflags( ios::fixed | ios::showpoint );

   for ( int i = 1; i < PEOPLE; ++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';
   }
   
   cout << "\nTotal" << setw( 6 ) << setprecision( 2 )
        << productSales[ 1 ];

   for ( int j = 2; j < PRODUCTS; ++j )
      cout << setw( 10 ) << setprecision( 2 )
           << productSales[ j ];

   cout << endl;
   return 0;
}

Recommended Answers

All 10 Replies

>> setw( 10 ) << 3

That, in C would be printf("%10d", 3); >> setprecision( 2 )
That's the same as printf("%.2f", sales[ i ][ j ]);

>> setw( 10 ) << 3

That, in C would be printf("%10d", 3); >> setprecision( 2 )
That's the same as printf("%.2f", sales[ i ][ j ]);

Here is what I have so far. Suggestions??

#include <stdio.h>
  
       void sales[ salesPerson ][ product ] += value;
  
      int main()
  
      {
  
      const int PEOPLE = 5, PRODUCTS = 6;
 
      double sales[ PEOPLE ][ PRODUCTS ] = { 0.0 }, value,
  
      totalSales, productSales[ PRODUCTS ] = { 0.0 };
  
      int salesPerson, product;
	  int  i, j
  
       
  
      printf("Enter the sales person (1 - 4), ");
  
      printf("product number (1 - 5)\nand total sales.");
  
     printf("Enter -1 for the sales person to end input.\n");
  
      scanf("%d", salesPerson);
  
       
  
      while ( salesPerson != -1 ) {
  
      scanf("%d" ,&product ;
	  scanf("%d", &value);
  
      sales[ salesPerson ][ product ] += value;
  
      scanf("%d", &salesPerson);
  
      }
  
       
  
      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;
  
      }

Here is what I have so far. Suggestions??

I suggest you explain the problem. We aren't psychic...

Here is what I have so far. Suggestions??

Compile the code and start working through the errors/warnings from top to down. Once you think you have solved/fixed a specific error, re-compile and see the results, i.e. don't try to fix all errors at once.

commented: Good suggestions. +26

lines 13 and 15 of my code contain arrays. I can not figure out what I have done wrong here in terms of how I declared the arrays. I have read what I can but it is not clicking. Help with this would be appreciated.

When I run my program now it stops after I enter salesperson number and does not allow me to enter product number and total sales for product. Assistance correcting my code would be appreciated very much. Here is the code

#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;
  
      scanf("%d", &salesPerson);
  
      }
  
       
  
      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;
  
      }

Please format your code consistently. It is very hard to follow when indenting is all over the place with unnecessary blank lines between every statement.

Please format your code consistently. It is very hard to follow when indenting is all over the place with unnecessary blank lines between every statement.

OK...I have tried to neaten up my code to make it easier to follow. Keep in mind I have only written a handful of programs so my formatting may not be he best. Thanks for your input.

#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;
  			scanf("%d", &salesPerson);
        }
       		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;
        }

That's just as bad. Try clicking on the link. I put it there for a reason.

Or maybe you can explain how

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;

shows consistency in indentation.

tried to neaten up my code to make it easier to follow.

Most modern IDEs come with a decent auto-formatting/beautifier feature, meaning that you'll get a decent formatting at a click of a button or so. Maybe you have such a feature but don't know about it?

You might want to increase your compiler's warning level to the maximum, in other words, just let the compiler spot the mistakes. If you are using GCC, then compile the code with the -Wall option set (=enable all warnings).

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.