3 Replies - 3051 Views - Last Post: 13 May 2009 - 09:37 AM Rate Topic: -----

#1 iCode  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 52
  • Joined: 15-January 09

Float & Char

Posted 13 May 2009 - 09:17 AM

Does Char always have to be scanned first? I created a code that didn't work when I had scanned for float first (In the int main()). Why is this?

With float first:
#include<stdio.h>

float C2F(float C)
{
	  float F;
	  
	  F = (C * 1.8) + 32;
	  
	  return F;
}

float F2C(float F)
{
	  float C;
	  
	  C = (F - 32) / 1.8;
	  
	  return C;
}

int main()
{
	float temp;
	char unit;
	
	printf("Enter Temperature: ");
	scanf("%f", &temp);
	
	printf("Enter Temperature Unit: ");
	scanf("%c", &unit);
		  
	if( unit == 'C' || unit == 'c')
		printf("%f is equivalent to %f", temp, C2F(temp));
	else if ( unit == 'F' || unit == 'f')
		printf("%f is equivalent to %f", temp, F2C(temp));
	else
		printf("Invalid Input");
		
		
	scanf("%d");
	return 0;
}


After the user enters a number, it also prints 'Invalid Function'.

But when I have char first, it works fine:

#include<stdio.h>

float C2F(float C)
{
	  float F;
	  
	  F = (C * 1.8) + 32;
	  
	  return F;
}

float F2C(float F)
{
	  float C;
	  
	  C = (F - 32) / 1.8;
	  
	  return C;
}

int main()
{
	float temp;
	char unit;
	
	printf("Enter Temperature Unit: ");
	scanf("%c", &unit);

	printf("Enter Temperature: ");
	scanf("%f", &temp);
	
		  
	if( unit == 'C' || unit == 'c')
		printf("%f is equivalent to %f", temp, C2F(temp));
	else if ( unit == 'F' || unit == 'f')
		printf("%f is equivalent to %f", temp, F2C(temp));
	else
		printf("Invalid Input");
		
		
	scanf("%d");
	return 0;
}


Why is this?

Is This A Good Question/Topic? 0
  • +

Replies To: Float & Char

#2 AmitTheInfinity  Icon User is offline

  • C Surfing ∞
  • member icon

Reputation: 117
  • View blog
  • Posts: 1,559
  • Joined: 25-January 07

Re: Float & Char

Posted 13 May 2009 - 09:24 AM

Well that's not the rule, that's because of the undefined behaviour of input buffer. When you give float input you press enter. Some times [and that's most of the times when MS's OS] that '\n' remains in buffer and causes your character scanf to get skipped.

one of the solution for this is to flush [fflush or flushall] the input buffer or put a getch() before character scanf or simply avoid scanfs for getting inputs.

I hope this will help you. :)
Was This Post Helpful? 1
  • +
  • -

#3 iCode  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 52
  • Joined: 15-January 09

Re: Float & Char

Posted 13 May 2009 - 09:29 AM

View PostAmitTheInfinity, on 13 May, 2009 - 08:24 AM, said:

Well that's not the rule, that's because of the undefined behaviour of input buffer. When you give float input you press enter. Some times [and that's most of the times when MS's OS] that '\n' remains in buffer and causes your character scanf to get skipped.

one of the solution for this is to flush [fflush or flushall] the input buffer or put a getch() before character scanf or simply avoid scanfs for getting inputs.

I hope this will help you. :)


Do I just put getch();

Or

getch(Unit);

How is it going to recognise that I want Unit to be recognised?
Was This Post Helpful? 0
  • +
  • -

#4 AmitTheInfinity  Icon User is offline

  • C Surfing ∞
  • member icon

Reputation: 117
  • View blog
  • Posts: 1,559
  • Joined: 25-January 07

Re: Float & Char

Posted 13 May 2009 - 09:37 AM

That getch is just to make input buffer empty, it has no other meaning to that. taking input will be done by your scanf .

int main()
{
	float temp;
	char unit;
   
	printf("Enter Temperature: ");
	scanf("%f", &temp);	

	// This call won't matter, it's just to soak in that remains of previous input [most probably '\n' or '\r' etc.]
	getch();


	printf("Enter Temperature Unit: ");
	scanf("%c", &unit);
   
	// ... remaining code goes here.
}



otherwise you can try fflush at place of getch.
int main()
{
	float temp;
	char unit;
   
	printf("Enter Temperature: ");
	scanf("%f", &temp);


	// added fflush here.
	fflush(stdin);

	printf("Enter Temperature Unit: ");
	scanf("%c", &unit);
   
	// ... remaining code goes here.
}



Though both of these methods are not recommended [but so is scanf for inputs :) ] they can solve your problem right now, so you can proceed with them.

--EDIT--
Updated code to take float input before character input.

This post has been edited by AmitTheInfinity: 13 May 2009 - 09:53 AM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1