Hey
I am trying to find a way for the user(not the programmer) to determine how many decimal places a number will print out to. I have tried pointers, precision mods and even a sub program to bypass my lack of knowledge in this area. Any help would be greatly appreciated.

Thanks!

void main(void)
{
    float total;
    float a;
    float b;
    float n;
    float SUM(float , float); /*function prototype*/

    printf("enter 2 numbers you would like to add separated by a space\n");
    scanf("%f %f", &add, &add1);
    printf("How many decimal places would you like your answer to go out to?\n");
     /*this is to determine how many decimal places user wants to print out to*/
    scanf("%f", &n); 

    total = SUM(a,b); /*subprogram that u can see below*/

    /*(this is where i have tried to put pointers and different precision mods)*/
    printf("Added together they are %f\n",n, total);
}

float SUM(float a, float b)
{
    return(a+b);
}

You can set the precision of a number using sprintf:

#include<stdio.h>
#include<stdlib.h>

int main(int argc, char** argv)
{
    float nr1 = 0.0f;
    float nr2 = 0.0f;
    float sum = 0.0f;
    int n = 0;
    char buffer[30];

    printf("Enter the two numbers:\n");
    scanf("%f %f",&nr1,&nr2);

    printf("How many decimal places would you like your answer to go out to?\n");
    scanf("%d", &n);

    /*Here you set the precision*/
    sprintf(buffer,"Added together they are %%.%df\n",n);
    printf("The printf format string will be: %s",buffer);

    sum = nr1+nr2;

    printf(buffer,(sum));
    return 0;
}
/*Output
Enter the two numbers:
32.3 83.2
How many decimal places would you like your answer to go out to?
6
The printf format string will be: Added together they are %.6f
Added together they are 115.500000
 */

main is not void and you have not defined add and add1, a and b are uninitialized... you have not enough formats for your parameters in printf.

Dheaven-thank you. I didnt know about sprintf and now my knowledge of c programming is growing. I will read up on this some but would you mind explaining line 19 of your code

sprintf(buffer,"Added together they are %%.%df\n",n);

because i do not quite understand the purpose what the sprintf does and the function it plays with the buffer preceding the print statement.

pyTony-thank you as well. What do you mean by i do not have enough formats for my parameters, i dont quite understand what you mean

I said you have too many paramaters in printf compared to the format string.

Good, but little excessive code for my taste dheaven. As you gave solution, here is my fixed code:

#include<stdio.h>
#include<stdlib.h>

int main(int argc, char** argv)
{
    float nr1 = 0.0f;
    float nr2 = 0.0f;
    int n = 0;

    printf("How many decimal places would you like your answer to go out to?\n");
    scanf("%i", &n);

    printf("Enter the two numbers:\n");
    scanf("%f %f",&nr1,&nr2);

    printf("Added together they are %.*f\n", n, nr1+nr2);
    return 0;
}
/*Output:
How many decimal places would you like your answer to go out to?
3
Enter the two numbers:
1.345 7.51212
Added together they are 8.857
*/

Edited 4 Years Ago by pyTony

Comments
yes!

pyTony- I understand your code better due to the lack of strings and buffers which i am not yet familiar. Thank you
Would you be willing to further explain line 19 and 20 of dheavens code

sprintf(buffer,"Added together they are %%.%df\n",n);
printf("The printf format string will be: %s",buffer);

so that i can completely understand all of the points you guys have made

Hello sebass123

pyTony variant is more elegant (I forgot about * option)

sprintf is used for formatting a string. I practically inserted the number representing the precision of the floating point number into the format string used by printf.

You can read here to find more about sprintf and format specifiers:
http://www.cplusplus.com/reference/clibrary/cstdio/sprintf/

On the other hand, pyTony eliminated the need for sprintf by using * width specifier.

According the cplusplus.com, the width specifier is used when:

The width is not specified in the format string, but as an additional integer value argument preceding the argument that has to be formatted.

This article has been dead for over six months. Start a new discussion instead.