Hi, here you will find a program which taking some inputs calculates the  sales of 5 items on daily and weekly basis. Prevously I wrote it with no function, and it was quite a very long program, 9 A4 sheets, but this is written by using a function. As long as I calculate the sales of  a day, it's all right, but when I try to do it for the week, I get a strange value, namely, 0.0000.
Why???
Here the code:
/*Program calculating the daily and weekly sales of 5 items*/

#include<stdio.h>
/*Prototype function daily_sales*/
float daily_sales(int day_number,int item , float quantity1, float quantity2,float quantity3, float quantity4, float quantity5,float value1,float value2, float value3,float value4,float value5, float val1, float val2,float val3, float val4, float val5, float Tot_value);
/*End Prototype function daily_sales */

float main ()
{
  int day ;
  int id;
  float q1,q2,q3,q4,q5;
  float value1_day1 = 0;
  float value2_day1 = 0;
  float value3_day1 = 0;
  float value4_day1 = 0;
  float value5_day1 = 0;
  float value1_day2 = 0;
  float value2_day2 = 0;
  float value3_day2 = 0;
  float value4_day2 = 0;
  float value5_day2 = 0;
  float value1_day3 = 0;
  float value2_day3 = 0;
  float value3_day3 = 0;
  float value4_day3 = 0;
  float value5_day3 = 0;
  float value1_day4 = 0;
  float value2_day4 = 0;
  float value3_day4 = 0;
  float value4_day4 = 0;
  float value5_day4 = 0;
  float value1_day5 = 0;
  float value2_day5 = 0;
  float value3_day5 = 0;
  float value4_day5 = 0;
  float value5_day5 = 0;
  float value1_day6 = 0;
  float value2_day6 = 0;
  float value3_day6 = 0;
  float value4_day6 = 0;
  float value5_day6 = 0;
  float value1_day7 = 0;
  float value2_day7 = 0;
  float value3_day7 = 0;
  float value4_day7 = 0;
  float value5_day7 = 0;
  float val1_day1 = 0;
  float val2_day1 = 0;
  float val3_day1 = 0; 
  float val4_day1 = 0;
  float val5_day1 = 0;
  float val1_day2 = 0;
  float val2_day2 = 0;
  float val3_day2 = 0; 
  float val4_day2 = 0;
  float val5_day2 = 0;
  float val1_day3 = 0;
  float val2_day3 = 0;
  float val3_day3 = 0; 
  float val4_day3 = 0;
  float val5_day3 = 0;
  float val1_day4 = 0;
  float val2_day4 = 0;
  float val3_day4 = 0; 
  float val4_day4 = 0;
  float val5_day4 = 0;
  float val1_day5 = 0;
  float val2_day5 = 0;
  float val3_day5 = 0; 
  float val4_day5 = 0;
  float val5_day5 = 0;
  float val1_day6 = 0;
  float val2_day6 = 0;
  float val3_day6 = 0; 
  float val4_day6 = 0;
  float val5_day6 = 0;
  float val1_day7 = 0;
  float val2_day7 = 0;
  float val3_day7 = 0; 
  float val4_day7 = 0;
  float val5_day7 = 0;
  float Tot_value_day1 = 0;
  float Tot_value_day2 = 0;
  float Tot_value_day3 = 0;
  float Tot_value_day4 = 0;
  float Tot_value_day5 = 0;
  float Tot_value_day6 = 0;
  float Tot_value_day7 = 0;
  float Tot_week = 0;
  
  printf("Please enter a value -1 to 7- to end\n");
  scanf("%d",&day);
 
  while(day<=7)
  {
    printf("Please enter the day number -1 to 7 - to end\n");
    scanf("%d",&day);
	switch(day) /*Start switch day 1*/
	{
	  case 1:/*Start case day 1*/
	  daily_sales(day,id,q1,q2,q3,q4,q5,value1_day1,value2_day1,value3_day1,value4_day1,value5_day1,val1_day1,val2_day1,val3_day1,val4_day1,val5_day1,Tot_value_day1); 
	  
	  break;/*break case1 day*/

	  
	printf("Please enter another day number\n");
	scanf("%d",&day);
	    
	  
	  case 2:/*Start case day 2*/
	  daily_sales(day,id,q1,q2,q3,q4,q5,value1_day2,value2_day2,value3_day2,value4_day2,value5_day2,val1_day2,val2_day2,val3_day2,val4_day2,val5_day2,Tot_value_day2); 
	  
	  break;/*break case1 day 2*/
	 
	  
	 printf("Please enter another day number\n");
	 scanf("%d",&day);
	  
	  
	   case 3:/*Start case day 3*/
	   daily_sales(day,id,q1,q2,q3,q4,q5,value1_day3,value2_day3,value3_day3,value4_day3,value5_day3,val1_day3,val2_day3,val3_day3,val4_day3,val5_day3,Tot_value_day3);  
	     
	   break;/*break case1 day 3*/
	
	  
	printf("Please enter another day number\n");
	scanf("%d",&day);
	  
	  
	
	   case 4:/*Start case day 4*/
	   daily_sales(day,id,q1,q2,q3,q4,q5,value1_day4,value2_day4,value3_day4,value4_day4,value5_day4,val1_day4,val2_day4,val3_day4,val4_day4,val5_day4,Tot_value_day4);
	     
	   break;/*break case1 day4*/
	
	  
	printf("Please enter another day number\n");
	scanf("%d",&day);  
	    
	
	   case 5:/*Start case day 5*/
	   daily_sales(day,id,q1,q2,q3,q4,q5,value1_day5,value2_day5,value3_day5,value4_day5,value5_day5,val1_day5,val2_day5,val3_day5,val4_day5,val5_day5,Tot_value_day5);
	      	  
	   break;/*break case1 day5*/
	 
	  
	  printf("Please enter another day number\n");
	  scanf("%d",&day); 
	    
	  
	   case 6:/*Start case day 6*/
	   daily_sales(day,id,q1,q2,q3,q4,q5,value1_day6,value2_day6,value3_day6,value4_day6,value5_day6,val1_day6,val2_day6,val3_day6,val4_day6,val5_day6,Tot_value_day6);  
	      	  
	   break;/*break case day6*/

	  
	  printf("Please enter another day number\n");
	  scanf("%d",&day);
	  
	  
	   case 7:/*Start case day 7*/
	   daily_sales(day,id,q1,q2,q3,q4,q5,value1_day7,value2_day7,value3_day7,value4_day7,value5_day7,val1_day7,val2_day7,val3_day7,val4_day7,val5_day7,Tot_value_day7);  
	     
	   break;/*break case day7*/
	  
	  
	  printf("Please enter another day number\n");
	  scanf("%d",&day);
	  
	default:
	printf("The sales for this week are over\n");
	break;
	}/*End  days switch*/
	  
	
  }/*End outer while  cycle*/
  Tot_week = Tot_value_day1 + Tot_value_day2 + Tot_value_day3 + Tot_value_day4 + Tot_value_day5 + Tot_value_day6 + Tot_value_day7;
  printf("The total value for this week is: %f\n", Tot_week);
  return 0; 
} 


/*Definition function daily_sales*/  
float daily_sales(int day_number,int item , float quantity1, float quantity2,float quantity3, float quantity4, float quantity5,float value1,float value2, float value3,float value4,float value5, float val1, float val2,float val3, float val4, float val5, float Tot_value)
{
printf("Please enter the id number - 1 to 5 - to end\n");
	      scanf("%d",&item);
		while(item<=5)/*Start while case 1*/
		{ 
		  printf("All right!! Please enter the id number - 1 to 5 - to end\n");
		  scanf("%d",&item);
		  switch(item)/*Start switch id day 1*/
		  {
		  case 1:
		    printf("Please enter the quantity\n");
		    scanf("%f",&quantity1);
		    value1 = quantity1 * 10;
		    val1 += value1;
		    printf("The value of id 1 for day %d is: %f\n",day_number, val1);
		    break;
		    
		    case 2:
		    printf("Please enter the quantity\n");
		    scanf("%f",&quantity2);
		    value2 = quantity2 * 20;
		    val2 += value2;
		    printf("The value of id 2 for day %d is: %f\n",day_number, val2);
		    break;
		    
		    case 3:
		    printf("Please enter the quantity\n");
		    scanf("%f",&quantity3);
		    value3 = quantity3 * 30;
		    val3 += value3;
		    printf("The value of id 3 for day %d is: %f\n",day_number, val3);
		    break;
		    
		    case 4:
		    printf("Please enter the quantity\n");
		    scanf("%f",&quantity4);
		    value4 = quantity4 * 40;
		    val4 += value4;
		    printf("The value of id 4 for day %d is: %f\n",day_number, val4);
		    break;
		    
		    case 5:
		    printf("Please enter the quantity\n");
		    scanf("%f",&quantity5);
		    value5 = quantity5 * 50;
		    val5 += value5;
		    printf("The value of id 5 for day %d is: %f\n",day_number, val5);
		    break;
		    
		    default:
		    printf("For now the sales are over!!\n");
		    break;
		  }/*End switch*/
		}/*End  while case*/
          Tot_value = val1 + val2 + val3 + val4 + val5;
	  printf("The total value of day %d is: %f\n",day_number, Tot_value);
	  return Tot_value;
} /*End definition function daily_sales  */
Is there anyone able to help me?

Thank you very much, indeed!!!

Fab2

You realize C supports an array type, right? When you find yourself naming variables x1, x2, x3, etc... that's a good time to consider an array.

I applaud your perseverance, but that is not an acceptable way to program. Use arrays and loops, especially for and while loops, to cut your program down to 1/10th that size and number of separate variables.

I do agree with you that using an array will be much better, but since I'm following a course on a book, and the functions chapter follows the while loops one and it's before the array one, I am trying to test my knowledge about functions, by rewriting a program previously written with endless loops.

Anyway, thank you for your advice

Bye bye

My advice is to throw the book away. Programming should start with basic data types (integers, floating point, characters, arrays, pointers), and then move into basic control structures (if/else, for/next, do/while), then functions. Each domain builds upon the one learned before, expanding your ability to use those concepts as you learn the next. Just my professional opinion.

Anyway, ranting aside, the answer to your problem, as far as I can see from a quick look at your code, is that you don't assign the output from daily_sales() to any of the variables that you are adding up for the weekly total... doh! Actually, I think you are trying to do that by passing the Tot_Value_DayN into the daily_sales() function; however, to do that, you need to pass it as a reference parameter. IE, the last parameter Tot_value in the daily_sales() function should be a float&, not a simple float type. IE,

float daily_sales(int day_number,int item , float quantity1, float quantity2,float quantity3, float quantity4, float quantity5,float value1,float value2, float value3,float value4,float value5, float val1, float val2,float val3, float val4, float val5, float& Tot_value);

Hope this helps.

For Rubberman:
Thank you for your reply, but would you like to explain me the difference between a float and a float&? As far as I know the "&" is for pointers, and I never read
that this symbol can be added to a type of number. I would like to know more about it.

Thank you

Fab2

Quick translation: &name means "take the address of 'name', so if you have an integer variable named anInt, &anInt returns the address (pointer to) the 'anInt' variable. As a result, this:

int anInt = 999;
int* pToInt = &anInt;
printf("anInt == %d\n", anInt);

and this

int anInt = 999;
int* pToInt = &anInt;
printf("anInt == %d\n", *pToInt);

will both print this:

anInt == 999

Thank You very much!!!
By the way, the book you told me to throw away is written by one of the "best teacher"of programming, namely H.M. and P.J. Deitel. At least, according to what others are saying about them. But, are they saying really the truth??

Anyway, thank You very much again!!

Bye, bye

Fab2

I have Deitel's book "How to Progeram C++" by Deitel & Deitel, and while it covers a lot of stuff quite well, following it point-by-point may not be the best way to learn the subject. Personally, I read it about 10 years ago (3rd edition, 2001) after I had spent 10 years programming major systems using C++, and thought to myself, yawn... I just pulled it off of my bookshelf (100 linear feet of programming and computer software books), and looked again. There are issues, so I'm not sure I'd recommend it as a self-teaching tool all by itself. If you are interested in what those issues are, let me know and I will get into some detail. Right now, I need to get back to cooking dinner for out-of-town friends that are coming to give a house concert tonight.

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