Hello Daniweb! I'm a beginner and C, and trying to make a program that asks a question in the main and takes an input from the user which jumps to the appropriate function. Here is the source.

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


void cm(){
	float a;
	double b;

	printf("Enter a number in Inches to be converted to CM:");
	scanf("%f", &a);

	b = a * 2.54;

	printf("%f", b);
}

int main()
{
	float inch, restart;
do {

	printf("Enter 1 to convert inches to cm, anything else to quit");
	scanf("%f", &cm);

	if ( inch == 1 )
	{
		cm();
	}
	else 
	{
		system("exit");
	}
	printf("Press 1 to restart, anything else to quit");
	scanf("%f", restart);}
while (restart == 1);
}

It compiles, but doesn't give the results I need, instead it gives me one warning,

Warning	1	warning C4700: uninitialized local variable 'restart' used.

Can anyone help?

scanf("%f", restart);

should be

scanf("%f", &restart);}

line 24 is passing the address of a function to scanf?
I think you meant scanf("%f", &inch); line 35 you need to pass the address of restart scanf("%f", &restart);

Thank you and gerard4143 , both of you. I made the corrections and the program seems to work.

I have a question though, when is it okay to use int, double or float?

Another question, how do I take an input string from a user?

#include string.h

char[10]//example

printf("%s",);

I'm completly lost on that?

int variables only hold integer values (-12, 0, 5, 9)
float and double variables hold floating point values (-3.6, 0.1, 5.3, 9.0)

doubles have more precision as to the number of significant digits, but consume more memory.

I prefer to use int (or long) values when an integer value is appropriate as operations using integer values are much faster than their floating point equivalents. I only use doubles for floating point values unless I have a need to use less memory, disk space or bandwidth.

For string input look at gets() or fgets(). I prefer fgets because I can protect my string from being overrun.

Oh, okay sthank you for clearing up float and double.

I'll look into the fgets();

Thanks.

Aurorian, here's an important idea to remember about floats and doubles: they can't perfectly represent every number.

Integers can represent every (whole) number, but non-integer data types can't. They'll be *very* close, but not quite perfect. So making comparisons of float1 == float2 type, are a bad idea, overall.

You want to use integers for comparisons like that.

Adak, I don't quite get what you mean, for instance if I was going to take an input then do something to that input for instance multiply it by 2.718, I should set the input to an int and the result to a float?

I'm sorry I get confused, I wish I could understand better.

I think what he was trying to point out is that while it is perfectly permissible to compare two integer values using the equality operator "==" that you can't rely on it for double or floating point values.

When comparing floating point values, you usually compare the absolute value of their difference to see if it is less than a threshold. Something like if (fabs(dblA - dblB) < 0.001) which is true if the two values are less than 1/1000 apart. The value 0.001 can be modified to increase or decrease the required precision.

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.