I am trying to make a function that rounds number..
I declared the function above the main and voided it
Then I recalled the function in the program and listed the variables needed in brackets
and then I wrote the function

I can't work out what is wrong.

#include<stdio.h>
#include<math.h>

void round2int(float,float);
int main (void)
{

	float roundednum,x;
	do
	{
	printf("Enter a number (Enter zero (0) to quit program)");
	scanf("%f",&x);
	round2int(x,roundednum);
	}
	while(x!=0);
   fflush(stdin);
   getchar();
   return 0;
}

void round2int(float test, float number)
{
  float number,test;
  number=floor(test+05);
  printf("%f",number);
}

Recommended Answers

All 8 Replies

in your function you are declaring twice the variables needed. 1st time as arguments and 2nd time when you say

float number, test;

this cleans up the variables and you get zero or memory gabadge.

Since your function is called round2Int, you could have it just "return" an int and print it from main().

int round2int(float test, float number)
{
  return floor(test+.5);
}
commented: Solved my question and helped in most difficulties even by messaging (in Daniweb) +2

in your function you are declaring twice the variables needed. 1st time as arguments and 2nd time when you say

Oh, I get it. Actually that was the mistake, and there was no need for 2 floats. One was enough.

#include<stdio.h>
#include<math.h>

void round2int(float);
int main (void)
{

   float x;
   printf("Enter a number (Enter zero (0) to quit program)\n");
   while(x!=0)
   {
	printf("\n\nNumber: ");
	scanf("%f",&x);
	round2int(x);
   }
   fflush(stdin);
   getchar();
   return 0;
}


void round2int(float test)
{
  test=floor(test+0.5);
  printf("Rounded Number: %f",test);
}

This worked well.

@thines01:

print it from main().

How can I print it from the main? I know it sounds stupid, but I am still a beginner to the language.
I've tried this but it didn't work

printf("%f",*round2int);

If your function returns an int, you coud do this:

if(0 != x)
{
   printf("I rounded it to (%d)\n\n", round2int(x,roundednum));
}

How can I print it from the main? I know it sounds stupid, but I am still a beginner to the language.

There are three conventional options for accessing the calculated result in a function from the calling function:

  1. Return the result from your function:
    #include <stdio.h>
    #include <math.h>
    
    float round2int(float test)
    {
        return floor(test + 0.5);
    }
    
    int main (void)
    {
        float x;
        
        while (1) {
            printf("Enter a number (Enter zero (0) to quit program): ");
            scanf("%f", &x);
            
            if (x == 0)
                break;
                
            printf("%f\n", round2int(x));
        }
        
        return 0;
    }
  2. Pass a pointer to the destination object as an argument:
    #include <stdio.h>
    #include <math.h>
    
    void round2int(float test, float *result)
    {
        *result = floor(test + 0.5);
    }
    
    int main (void)
    {
        float x, result;
        
        while (1) {
            printf("Enter a number (Enter zero (0) to quit program): ");
            scanf("%f", &x);
            
            if (x == 0)
                break;
            
            round2int(x, &result);
            printf("%f\n", result);
        }
        
        return 0;
    }
  3. (not recommended) Use a global variable:
    #include <stdio.h>
    #include <math.h>
    
    float result;
    
    void round2int(float test)
    {
        result = floor(test + 0.5);
    }
    
    int main (void)
    {
        float x;
        
        while (1) {
            printf("Enter a number (Enter zero (0) to quit program): ");
            scanf("%f", &x);
            
            if (x == 0)
                break;
            
            round2int(x);
            printf("%f\n", result);
        }
        
        return 0;
    }
commented: Well given examples +2

Thanks Narue .. Is there any reason why the 3rd is not recommended?.. since it was the one I learned in my notes..

Anyways the 'Return Method' will always work and I understood that now(for the current problems lol :P

Is there any reason why the 3rd is not recommended?.. since it was the one I learned in my notes..

Global variables are difficult to work with in larger programs because they can be accessed from anywhere. Bugs where you find yourself asking "where did that variable get changed?" are greatly complicated by variables with unrestricted access.

Anyways the 'Return Method' will always work

Not always, but it should be your go to method.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.