Click here to Skip to main content
15,887,596 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
No matter what input I give this code, the format is messed up and the values do not match what they are supposed to:
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <float.h>
#include <string.h>

double input1 = 0;
int main()
{
    double  celsius1, kelvin1, rankine1;
    scanf ("%g", &input1);
    celsius1 = ((5 *(input1 - 32))/9);
    rankine1 = (input1 + 459.67);
    kelvin1 = ((5*rankine1)/9);
    printf("%g degrees Fahrenheit\n%g degrees Celsius\n%g degrees Kelvin\n%g degrees Rankine", input1, celsius1, kelvin1, rankine1);
    return 0;
}


Here is the output for an input of -460.67
-460.67
1.62382e-314 degrees Fahrenheit
-17.7778 degrees Celsius
255.372 degrees Kelvin
459.67 degrees Rankine
C:\Users\rthom\source\repos\ConsoleApplication1\Debug\ConsoleApplication1.exe (process 20956) exited with code 0.


I am required to use %g on this assignment for all variables as well, so there is no way I'd be able to change that.

What I have tried:

I've tried initializing input1 outside and inside of main. I've also tried declaring the variable in main, but regardless of what I've done it hasn't worked. When I've initialized input1 inside main and removed the &, I receive a null pointer != result exception.
Posted
Updated 14-Jul-20 14:39pm
v2

1 solution

scanf - C++ Reference[^]

Specifier chart is near the bottom. It's because '%g' only handles floats (4 bytes) not doubles (8 bytes). You'd need to use '%lg' for doubles. Changing the variables to float worked for me.
 
Share this answer
 
Comments
Ryan Thomsen 14-Jul-20 21:12pm    
Unfortunately my assignment specifies I must use type double for all variables and that I must use printers %g conversion specifier to display all numeric values. Is there a workaround to this?
Jon McKee 15-Jul-20 0:46am    
I hope you figured it out. If the instructions actually require you to read doubles as if they were floats like this I would ask the teacher about it.

This code will show you what I'm talking about if you enter the same value twice. Floats and doubles have different memory formats.
int main()
{
	double input1 = 0;
	
	//Different precision reads
	scanf("%g", &input1);
	printf("G (float) read: %g\n", input1);
	scanf("%lg", &input1);
	printf("LG (double) read: %g\n", input1);

	//Examining the hex of double vs float
	printf("\nRaw double: %-g\t\t0x", input1);
	for (int i = 0; i < sizeof(double); i++)
		printf("%02X", *(((unsigned char *)&input1) + i));
	printf("\n");
	float input2 = (float)input1;
	printf("Casted float: %-g\t\t0x", input2);
	for (int i = 0; i < sizeof(float); i++)
		printf("%02X", *(((unsigned char *)&input2) + i));
	printf("\n");
	return 0;
}

You can technically pull this off but it's super hacky, may not work on all systems, and never recommended:
scanf("%g", &input1);
printf("%g\n", *((float *)&input1));

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900