Get multiple outputs under same printf()

Trying to get text, and a number to display with printf

Page 1 of 1

4 Replies - 2959 Views - Last Post: 31 October 2008 - 01:25 AM Rate Topic: -----

#1 bbarker3  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 33
  • Joined: 12-October 08

Get multiple outputs under same printf()

Post icon  Posted 28 October 2008 - 07:47 AM

I'm trying to get numbers to display with the text. The part that is not working(I think) is the end of the code, where the last three printf()'s are. Can anyone help with this?

#include <stdio.h>

int main()
{
	 int  num, count = 0, total = 0;

	 printf("Enter a whole number, and -99 to quit:");
	 scanf_s("%d", &num); // user input
	 while (num != -99) {
		  count++; // user input counter 
		  total += num; //sums up numbers entered 
		  printf("Enter a whole number, and -99 to quit:");
		  scanf_s("%d", &num); // user input
	 } 
	 
	 printf("You keyed in ", num, " numbers \n");
	 printf("The total is ", total);
	 printf("The average is ", total/count);
	 return(0);
}


Is This A Good Question/Topic? 0
  • +

Replies To: Get multiple outputs under same printf()

#2 Gloin  Icon User is offline

  • Expert Schmexpert...
  • member icon

Reputation: 235
  • View blog
  • Posts: 4,489
  • Joined: 04-August 08

Re: Get multiple outputs under same printf()

Posted 28 October 2008 - 07:55 AM

Looks like you're used to Java. In C you add all the text and put marks where you want to put in variables, then you add the variables after the text in the same order as the markers in the text. I'll give you an example..

Let str be the string "it's a wonderful"
printf("Hello, %s world!", str);

would result in the output: Hello, it's a wonderful world!

%d (integers)
%f (floats)
%c (chars)

the most common types, look up the rest on your own.
Was This Post Helpful? 0
  • +
  • -

#3 David W  Icon User is offline

  • DIC supporter
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,788
  • Joined: 20-September 08

Re: Get multiple outputs under same printf()

Posted 28 October 2008 - 07:58 AM

Try this ... :D

#include <stdio.h>

int main()
{
     int  num, count = 0, total = 0;

     printf("Enter a whole number, and -99 to quit: ");
     scanf("%d", &num); // user input
     while (num != -99) 
     {
          count++; // user input counter
          total += num; //sums up numbers entered
          printf("Enter a whole number, and -99 to quit: ");
          scanf("%d", &num); // user input
     }
    
     printf( "\nYou keyed in %d numbers.", count );
     printf( "\nThe total is %d", total );
     printf( "\nThe average is %.2f ", (float)total/count );
     
     int c;
     while( (c=getchar())!='\n' ); /* 'flush' stdin */
     getchar();
     return(0);
}

Was This Post Helpful? 0
  • +
  • -

#4 bbarker3  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 33
  • Joined: 12-October 08

Re: Get multiple outputs under same printf()

Posted 28 October 2008 - 09:40 AM

That worked, but what would I need to change where the user could input float values and it calculate it? I entered 1.5, and it did not seem to work well. It was a jumbled mess of multiple streamings of characters going through the window. Anyone know a fix to this, where I can enter a decimal, and it calculate it right?

This post has been edited by bbarker3: 28 October 2008 - 11:42 AM

Was This Post Helpful? 0
  • +
  • -

#5 David W  Icon User is offline

  • DIC supporter
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,788
  • Joined: 20-September 08

Re: Get multiple outputs under same printf()

Posted 31 October 2008 - 01:25 AM

This show a way to validate data entry ... using a 'flag' value of -1 to indicate bad data entered ... (i.e. non 'float' data entry).

/*  
    Gets average of 'float' numbers in range 0..100
    with input number validation ...
    < Enter any number greater than 100 to end data entry. >
*/

#include <stdio.h>

int main()
{
    int c, c2, count = 0;
    float num, total = 0;
    
    for(;;)/>                     /* loop forever ... until break */ 
    {
        num = -1.0;             /* re-set on each loop */
        printf
        (
            "(%2d) Enter a number in range 0..100"
            " <greater than 100 to quit> : ", 
            count+1
        );
        scanf("%f", &num);      /* user input */
        while( (c=getchar())!='\n' ); /* 'flush' stdin */
        
        if( num < 0 ) 
        { 
            puts("\nError! Bad entry ..."); 
            continue;           /* from top of while */
        } 
        if (num >100 )
        {
            printf("\nDo you want to tally numbers now (y/n) ? ");
            c = getchar();
            if( c=='y' || c=='Y' ) break;   /* leave for loop now */
            
            /* if we reach here ... */
            while( c!='\n' ) c=getchar();   /* 'flush' stdin ... */
            
            printf("\nDo you want to count this %.2f number ... (y/n) ? ", num);
            c= getchar();
            c2 = c;
            while( c!='\n' ) c=getchar();   /* 'flush' stdin ... */
            if( c2=='n' || c2=='n' ) continue; /* from top of for loop */
        }
        
        count++; 
        total += num; 
    }
    
    printf( "\nYou keyed in %d numbers.", count );
    printf( "\nThe total is %.2f", total );
    
    if( count==0 ) count = 1;       /* so won't divide by zero on next line */
    printf( "\nThe average is %.2f ", total/count );
    
    while( c!='\n' ) c=getchar();   /* 'flush' stdin ... */
    getchar();
    return(0);
}

This post has been edited by David W: 31 October 2008 - 01:28 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1