Just curious

This is a discussion on Just curious within the C Programming forums, part of the General Programming Boards category; Once again this is another one of Sams quizes, first here is the question: 10. Write a program that uses ...

  1. #1
    CAP
    CAP is offline
    Registered User CAP's Avatar
    Join Date
    May 2002
    Posts
    179

    Just curious

    Once again this is another one of Sams quizes, first here is the question: 10. Write a program that uses a function to find the average of five type float values entered by the user.

    Now this is just a very simple example because last time it didn't work so this is like a "start simple and add on to it" kinda things.
    Anyways this is my code:

    Code:
    #include <stdio.h>
    
    float a,b,c,d,e=0;
    double f=0;
    
    float average(float,float,float,float,float);
    
    int main(void)
    {
    	printf("Please enter 5 values: ");
    	scanf("%f",&a);
    	scanf("%f",&b);
    	scanf("%f",&c);
    	scanf("%f",&d);
    	scanf("%f",&e);
    
    	f=float average(a,b,c,d,e);
    
    
    
    	printf("Your values are: %f %f %f %f %f\n\n",a,b,c,d,e);
    
    	return 0;
    }
    Simple enough right nothing big, no errors, that isn't my question. My questions is why do you have to put spaces to enter your 5 values instead of enter a value hit enter and do that over and over again, I thought that would be wrong so I tried to spread out all of the scaf's into seperate ones but I still have to hit space instead of enter or else the whole thing dies, why is this and how can I change it?

    I tried to continue on but this came up: (17) : error C2059: syntax error : 'type'

    Now I just updated the code up above to fit my question now, how do I get rid of this type thing and for that matter what is it. My "index" describes it as a syntax error and that I may be missing a closing brace or that the error is in the line above but I simply cannot find it. I thought it would have been simple but I have now tried a few variations of the line that takes the variables(that is the error line)but I just can't get it...any help?
    Last edited by CAP; 09-06-2002 at 06:31 PM.
    -Microsofts Visual C++ Introductory Kit-
    Current Projects: Learning Everything C.

    Everyone has a photographic memory, some people just don't have any film.
    ______________________________

    When was the last time you went for a colon cleansing? Because quite frankly, you're so backed up with crap that it's spilling out your mouth

  2. #2
    Registered User
    Join Date
    Aug 2002
    Posts
    21
    >f=float average(a,b,c,d,e);

    your function returns a float but f is a double, and you have 'float' in front of the call to the function, can you do that?

    not sure about the scanf thing,
    does
    scanf("%f%f%f%f%f", &a, &b, &c, &d, &e);
    not do what you want?

    hope this is helpful.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    >f=float average(a,b,c,d,e);

    your function returns a float but f is a double, and you have 'float' in front of the call to the function, can you do that?
    I assume they are trying to typecast this. If so, this is what you want:

    f = (float) avarage( a, b, c, d, e );

    On a side note, you, the original poster, never actually use the variable 'f', so what's the point?

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

  4. #4
    CAP
    CAP is offline
    Registered User CAP's Avatar
    Join Date
    May 2002
    Posts
    179
    Hobo:
    your function returns a float but f is a double, and you have 'float' in front of the call to the function, can you do that?not sure about the scanf thing,
    does
    scanf("%f%f%f%f%f", &a, &b, &c, &d, &e);
    not do what you want?hope this is helpful.
    I tried to put them all in one scanf but I think I had commas between the %f's...would that be a problem? I changed the f to a double just to see if that would fix the 'type' error.



    Quzah:
    I assume they are trying to typecast this. If so, this is what you want:f = (float) avarage( a, b, c, d, e );

    On a side note, you, the original poster, never actually use the variable 'f', so what's the point?

    Quzah.
    Thanks Quzah, I just tried to typecast it your way and it works, I didn't know about having the float in brackets and I realize that I have yet to use f, sorry about that but I posted this before supper and checked after the movie so I am going to go and try everything now but I just wanted to post the latest stuff I had. But now I have used f, and it returns an average and works just fine, thanks and I guess I am on to the next question
    Last edited by CAP; 09-06-2002 at 10:27 PM.
    -Microsofts Visual C++ Introductory Kit-
    Current Projects: Learning Everything C.

    Everyone has a photographic memory, some people just don't have any film.
    ______________________________

    When was the last time you went for a colon cleansing? Because quite frankly, you're so backed up with crap that it's spilling out your mouth

  5. #5
    Registered User
    Join Date
    Aug 2002
    Posts
    21
    I tried to put them all in one scanf but I think I had commas between the %f's...would that be a problem?
    I don't think it would cause a problem but I had to type:
    value,value,value,value,value<enter>

    without the commas I typed:
    value<enter>
    value<enter>
    value<enter>
    value<enter>
    value<enter>

    hobo

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,439
    scanf("%f",&a);
    scanf("%f",&b);
    scanf("%f",&c);
    scanf("%f",&d);
    scanf("%f",&e);
    or
    scanf("%f%f%f%f%f", &a, &b, &c, &d, &e);
    or
    scanf("%f %f %f %f %f", &a, &b, &c, &d, &e);

    These 3 versions should all work in the same way.

    You can either enter one value per line
    value<enter>
    value<enter>
    value<enter>
    value<enter>
    value<enter>

    or like this
    value value value value value<enter>

    Basically, any white space you type on input (space, tab, newline) is ignored, and any embedded white space in the scanf control string is also ignored. The only time this "ignore all whitespace" rule is ignored is when you have %c or %[ as the conversion format.

    But if you have commas in the control string (or anything else for that matter), then you've got to have the same characters in your input

    Eg.
    scanf( "%f hello %f", &a, &b );

    would need input of
    123hello456<enter>
    or
    123 hello<enter>
    456<enter>
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    CAP
    CAP is offline
    Registered User CAP's Avatar
    Join Date
    May 2002
    Posts
    179
    Ok I am going to try a couple of different ways of scanning so that I can check on the input and see how everything works but thanks for all your help
    -Microsofts Visual C++ Introductory Kit-
    Current Projects: Learning Everything C.

    Everyone has a photographic memory, some people just don't have any film.
    ______________________________

    When was the last time you went for a colon cleansing? Because quite frankly, you're so backed up with crap that it's spilling out your mouth

  8. #8
    Registered User datainjector's Avatar
    Join Date
    Mar 2002
    Posts
    356
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    void average ( float , float ,float ,float ,float );
    
    int main(){
        float num, num1, num2, num3, num4;
    
        printf( "Enter five numbers:" );
        scanf("%f%f%f%f%f", &num, &num1, &num2, &num3, &num4 );
    
        average ( num, num1, num2, num3, num4 );
    
        system ("pause");
        return 0;
    }
    void average ( float n, float n1,float n2,float n3,float n4 )
    {
     float sum, avg;
         sum = n+n1+n2+n3+n4;
         avg = sum/5;
     printf( "The average is %f", avg );
    }
    Global varaibles are bad programming technique they are faster but they voilate the principles of least privilage its poor programming practice
    "I wish i could wish my wishs away"

    "By indirections find directions out" -- William Shakespears

    "Do what thou wilt shall be the whole of the law" -- Crowley "THE BEAST 666"

    Mizra -> love = Death...
    RDB(Rocks yooo)..

    http://www.cbeginnersunited.com

    Are you ready for the Trix ???

  9. #9
    CAP
    CAP is offline
    Registered User CAP's Avatar
    Join Date
    May 2002
    Posts
    179
    Global varaibles are bad programming technique they are faster but they voilate the principles of least privilage its poor programming practice
    What are the principles of least privilage? And in every program I have seen they all use global variables unless of course the variables are in a function...why do so many books have that and what is a better way to do it?
    -Microsofts Visual C++ Introductory Kit-
    Current Projects: Learning Everything C.

    Everyone has a photographic memory, some people just don't have any film.
    ______________________________

    When was the last time you went for a colon cleansing? Because quite frankly, you're so backed up with crap that it's spilling out your mouth

  10. #10
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>scanf("%f%f%f%f%f", &num, &num1, &num2, &num3, &num4 );
    No matter how you enter the data, always check the return code from scanf() to ensure it did what you asked it too.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Curious GCC error messages
    By Mario F. in forum Tech Board
    Replies: 1
    Last Post: 11-22-2006, 03:57 PM
  2. Just curious
    By Chaplin27 in forum C++ Programming
    Replies: 3
    Last Post: 09-10-2004, 10:57 AM
  3. Curious about thumdnails
    By Gravedigga in forum Windows Programming
    Replies: 1
    Last Post: 03-08-2004, 09:02 AM
  4. A curious question
    By sbongo in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 01-02-2004, 08:55 PM
  5. Curious About Master Boot Record
    By civix in forum Tech Board
    Replies: 1
    Last Post: 01-26-2003, 01:19 AM

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