Thread: basic printf and fprintf

  1. #1
    Registered User CASHOUT's Avatar
    Join Date
    Jul 2011
    Location
    Florida
    Posts
    88

    basic printf and fprintf

    Instead of the final print out being: The sum of the dollar amounts is

    I would like for it to display: The sum of the (%d how ever many amounts they put in) is

    How do you set (the value of how many times the user input a dollar amount to be calculated) to a variable so that it can be referenced in the final printf?


    Code:
    void sumDoubles ()
    {
    	double num = 0;
    	double total = 0;
    	int doublesEntered = 0;
    	int index;
    	double amounts[100];
    	
    	FILE *myFile;
    	myFile = fopen("assignment3.txt", "w");
    
    		printf("\nI want to sum up dollar amounts.\n\n");
    		fprintf(myFile, "You chose to sum a list of dollar amounts.\n\nThese are the dollar amounts:\n\n");
    	
    		
    		do
    	{	
    		printf("\nEnter an dollar amount (enter -1 to quit): \n\n\t\t\t");
    		scanf("%lf", &num);
    					
    
    			if (num != -1 )
    				{    
    				amounts[doublesEntered] = num;  // store each num in the array
    				doublesEntered++;  // advance the index by 1 for the next num 
    			
    				fprintf(myFile, "$%.2f\n", num);
    			
    				total += num;
    				printf("\n\tThe total so far is: $%.2f\n", total);
    				}
    		
    			else
    		
    				break;
    				
    	}	while(num != -1);
    
    	
    
    								for (index = 0; index < num; index++) 
    								{
    								total += amounts[index];
    								}
    
    
    
    		fprintf(myFile, "\nThe sum of the dollar amounts is $%.2f", total);
    		printf("\nThe sum of the dollar amount is $%.2f\n\n\n", total);
    		printf("\tThank you for playing!\n\n\n\n");
    
    		fclose(myFile);
    
    	return;
    }

  2. #2
    Registered User CASHOUT's Avatar
    Join Date
    Jul 2011
    Location
    Florida
    Posts
    88
    In another words, how can I print the number of elements in the array?

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    The variable doublesEntered keeps track of the number of values entered. Print its value using the %d format.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  4. #4
    Registered User
    Join Date
    Apr 2013
    Posts
    8
    Instead of
    Code:
     if (num != -1)
    try
    Code:
     for(i = 0; i != -1; i++)
    this will help you keep track of the total number of inputs

    hopefully that helps. Im still a rookie so if not my bad.

  5. #5
    Registered User
    Join Date
    Apr 2013
    Posts
    8
    Quote Originally Posted by grumpy View Post
    The variable doublesEntered keeps track of the number of values entered. Print its value using the %d format.
    Grumpy is right though, doublesEntered keeps track of the number of values entered just as i in my previous post would.

  6. #6
    Registered User CASHOUT's Avatar
    Join Date
    Jul 2011
    Location
    Florida
    Posts
    88
    Thanks.

    I tried implementing the same fix for my other function and got a negative result. Can anyone help explain why I'm getting the wrong value for the number of elements in the array? And also what I should work on to fix it?



    Code:
    void sumIntegers ()
    {
    int arr[100];
    int i;
    int num = 0;
    int sum = 0;
    
    FILE *myFile;
    myFile = fopen("assignment3.txt", "w");
    	
    
    		printf("\nI want to sum up even integers <= 50.\n\n");
    
    
    		do
    	{
    		printf("\n\nPlease enter a positive integer:\n\n\t\t\t");
    		scanf("%d", &num);
    		
    		fprintf(myFile, "You chose to sum the even integers from 0 to %d.\n\n", num);
    		
    		
    			if(num < 0)
    				{
    				printf("\n*** Negative numbers are invalid! ***\n\n\n");
    				break;
    				}
    	
    			
    					
    							for (i = num - 4; i >= 0; i--)
    							{
    							arr[i] = (i + 1) * 2;	// add 1 to the index, then double it, and store that # in arr
    							printf ("\nIn index #%d of arr, we stored the value %d.\n", i, arr[i]);    //prints arr[i] with index reference
    					
    							fprintf(myFile, "%d\n\n", arr[i]);    //prints to txt file
    							}
    
    
    
    		printf("\n");
    					
    
    
    							for (i = num - 4; i >= 0; i--)
    							{
    							sum += arr[i]; // add each element to "sum"
    							}
    
    
    		printf("\n\nThe total of the %d even element in arr[i] = %d.\n\n\n\n", i, sum);
    		fprintf(myFile, "The sum of the %d even integers is: %d", i, sum);	//prints to txt file
    	
    	}	while(num <= 0);
    
    
    	fclose(myFile);
    	
    	return;
    }

  7. #7
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Code:
        for (i = num - 4; i >= 0; i--)
        {
             sum += arr[i]; // add each element to "sum"
        }
        printf("\n\nThe total of the %d even element in arr[i] = %d.\n\n\n\n", i, sum);
    Are you aware that this will alwais print
    Code:
    The total of the -1 even element ...
    because i will always be -1 after the loop.

    I do not understand why your loops need to count backwards

    Kurt

  8. #8
    Registered User CASHOUT's Avatar
    Join Date
    Jul 2011
    Location
    Florida
    Posts
    88
    Quote Originally Posted by ZuK View Post
    Code:
        for (i = num - 4; i >= 0; i--)
        {
             sum += arr[i]; // add each element to "sum"
        }
        printf("\n\nThe total of the %d even element in arr[i] = %d.\n\n\n\n", i, sum);
    Are you aware that this will alwais print
    Code:
    The total of the -1 even element ...
    because i will always be -1 after the loop.

    I do not understand why your loops need to count backwards

    Kurt
    Yes I'm aware. I seem to like making things harder than they are and need to be. Why do I tend to figure things out right after I post a thread asking for help?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fprintf and printf ?
    By vsovereign in forum C Programming
    Replies: 7
    Last Post: 03-01-2010, 01:13 AM
  2. fprintf vs printf
    By Tiago in forum C Programming
    Replies: 2
    Last Post: 12-12-2009, 01:03 PM
  3. printf,sprintf,fprintf
    By clique in forum C Programming
    Replies: 1
    Last Post: 10-11-2008, 12:48 AM
  4. Replies: 7
    Last Post: 03-26-2008, 03:21 AM
  5. printf /fprintf with __int64
    By stormbringer in forum C Programming
    Replies: 1
    Last Post: 11-12-2002, 07:56 AM