Problem with a simple program

This is a discussion on Problem with a simple program within the C Programming forums, part of the General Programming Boards category; Alright, I just started teaching myself how to program C a couple days ago, so don't make fun of me ...

  1. #1
    C noobie
    Join Date
    Jun 2006
    Location
    MI
    Posts
    25

    Problem with a simple program

    Alright, I just started teaching myself how to program C a couple days ago, so don't make fun of me too much!


    Code:
    #include <stdio.h>
    
    
    //purpous is to have 4 numbers input and the average displayed
    
    
    main()
    {
    	int a = 1;  //counter
    	double b;   //numbers to be input
    	double c = 0.0;   //total of all b
    	char d;  //suffix to number
    
    	while(a < 5)  
    	{
    		printf("Input your %d%c number: ",a,d==1?'st':(a==2?'nd':(a==3?'rd':'th')));   //input numbers
    		scanf("%fl",&b);  //input numbers to be averaged
    		c = c + b;  //add all the numbers up
    		++a;  //tell program to go to next number
    		if (a<5) continue;	//if not done inputting numbers, loops	
    		printf("The average of the 4 numbers is %fl.",c/4);  //displays average of the sum of all 4 numbers inputted
    	}
        
    	return(0);
    	
    }
    My problem is the program goes goofy when displaying the final averaged number. Does it have to do with the "Double"? Im getting a result a 0.0000001. Also it displays "t" at the end of each number when displaying which number to input.
    Last edited by Salgat; 06-15-2006 at 05:31 PM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Since when are 'st', 'nd', 'rd' and 'th' single characters?
    = is an assignment.
    == is an equality test.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    C noobie
    Join Date
    Jun 2006
    Location
    MI
    Posts
    25
    I know about = and ==, not sure about how that applies here, but thanks about the char note. Also, any reason why its not reading(or outputting?) the correct double variables? Thats my main problem.

  4. #4
    Registered User
    Join Date
    Jun 2004
    Posts
    277
    avarage.c:14:57: warning: multi-character character constant
    avarage.c:14:67: warning: multi-character character constant
    avarage.c:14:77: warning: multi-character character constant
    avarage.c:14:82: warning: multi-character character constant
    avarage.c: In function `main':
    avarage.c:14: warning: overflow in implicit constant conversion
    avarage.c:15: warning: float format, double arg (arg 2)
    avarage.c:24:2: warning: no newline at end of file


    I can't grok your 14 line what a heck do you want to do?

  5. #5
    C noobie
    Join Date
    Jun 2006
    Location
    MI
    Posts
    25
    If you talking about the first output, Im trying to have it go 1st, then next time around 2nd, then next time 3rd, etc. To be honost, Im not 100% sure you can put the trinary ?: inside another one. But like I said, my main focus is the output being incorrect for the average value.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Salgat
    I know about = and ==, not sure about how that applies here
    Code:
    printf("Input your %d%c number: ",a,d=1?'st':(a=2?'nd':(a=3?'rd':'th')));   //input numbers

    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    C noobie
    Join Date
    Jun 2006
    Location
    MI
    Posts
    25
    Oops, hehe thanks Quzah, hoorah! But about the double variable, anyone know?

  8. #8
    Registered User
    Join Date
    Jun 2004
    Posts
    277
    Quote Originally Posted by Salgat
    If you talking about the first output, Im trying to have it go 1st, then next time around 2nd, then next time 3rd, etc. To be honost, Im not 100% sure you can put the trinary ?: inside another one. But like I said, my main focus is the output being incorrect for the average value.
    For gods sake take a book like K&R you are guessing the language that is never gonna work I will give you a basic hint of what you need.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void){
    ∑ double a,b,c,avarage;
    ∑ printf("Give me 3 numbers separated by spaces: ");
    ∑ scanf("%lf %lf %lf",&a,&b,&c);
    ∑ avarage = (a+b+c)/3;
    
    ∑ printf("The avarage is %.2lf\n",avarage);
    ∑ return 0;
    
    }

  9. #9
    C noobie
    Join Date
    Jun 2006
    Location
    MI
    Posts
    25
    Thanks! I was trying to apply a few things I was learning and reduce the amount of variables used, unfortunately I got a few things wrong. I know I may not be doing it all perfectly to the book, and I know C is a percise language, but was expirementing with some new operands and although that didn't work, I'm still wondering why its not outputting correctly.


    Heres a revision for debugging purpouses.

    Code:
    #include <stdio.h>
    
    
    //purpous is to have 4 numbers input and the average displayed
    
    
    int main(void)
    {
    	double b;   //numbers to be input
    	double c = 0.0;   //total of all b
    	int a = 1;  //counter
    
    	while(a < 5)  
    	{
    		printf("\nInput your %d number: ",a);   //input numbers
    		scanf("%fl",&b);  //input numbers to be averaged
    		printf("The number you input is: %fl",b);   //show your input
                    c = c + b;  //add all the numbers up
    		++a;  //tell program to go to next number
    		if (a<5) continue;	//if not done inputting numbers, loops	
    		printf("The average of the 4 numbers is %.2fl.",c/4);  //displays average of the sum of all 4 numbers inputted
    
    
    
    
    	}
        
    	return(0);
    	
    }

    I just want to know, why isnt it reading my double variable "b" correctly.
    Last edited by Salgat; 06-15-2006 at 05:48 PM.

  10. #10
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Code:
    scanf("%fl",&b);  //input numbers to be averaged
    Try spelling %lf correctly. And for printf, just use %f.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  11. #11
    C noobie
    Join Date
    Jun 2006
    Location
    MI
    Posts
    25
    Oh ya! Haha, thanks (I would use floats, and a more simple setup, but like I said, trying out different things);

    (ended up finishing it, thanks guys )

    Code:
    #include <stdio.h>
    //Title: Min, Max, and Average
    //Purpose: Is to have a given amount of numbers input and the average displayed
    //Creator: Austin Salgat
    
    
    
    main()
    {
        int a = 1;  //counter
       	int d;   //amount of #s averaged
    	double b;   //current # inputted
    	double c = 0.0;   //total of all b
    	double e = 0.0;   //max #
    	double f = 999.999;   //min #
    
    	printf("**************************************\n***   Find Min, Max, and Average   ***\n***    Created by Austin Salgat    ***\n**************************************");
    	printf("\n\nHow many numbers do you wish to put into this average?: ");
        scanf("%d",&d);  //input how many numbers to average
    
    	while(a <= d)  
    	{
      	    printf("\nInput number %d: ",a);   //input numbers
    		scanf("%lf",&b);  //input numbers to be averaged
            e = (b>e) ? b : e;   //input new max
    		f = (b<f) ? b : f;  //input new min
            c = c + b;  //add all the numbers up
    		
            printf("\nThe average of the %d number%c is %.2lf.",a,a==1?' ':'s',c/a);  //displays average of the sum of all 4 numbers inputted
    		printf("\nThe max is %.2lf and the min is %.2lf.\n",e,f);  //displays min and max
    		++a;  //tell program to go to next number
    	}
        
        printf("\n\n\nTo exit this program, simply type a letter and press enter!: ");
        scanf("%d",a);
    	return(0);
    	
    }
    Last edited by Salgat; 06-15-2006 at 08:43 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem with a simple program
    By alfredd in forum C++ Programming
    Replies: 4
    Last Post: 02-28-2009, 03:48 PM
  2. Client-server system with input from separate program
    By robot-ic in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-16-2009, 03:30 PM
  3. Replies: 1
    Last Post: 12-30-2007, 10:08 AM
  4. Running Program Problem
    By warfang in forum C++ Programming
    Replies: 10
    Last Post: 03-28-2007, 03:02 PM
  5. simple login program problem
    By suckss in forum C Programming
    Replies: 11
    Last Post: 11-11-2006, 05:02 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21