Fedora Linux Support Community & Resources Center
  #1  
Old 24th September 2012, 11:44 AM
JoeNapoleon Offline
Registered User
 
Join Date: Oct 2011
Location: Wuhan , China
Posts: 22
windows_xp_2003chrome
a bug in a small exercise program.how do i correct it. thank you.

/* exercise5-8.c -- convert fahrenheit temperature to celsius and kelvin temperature */


#include <stdio.h>
void Temperatures(double f);
const float ct1 = 1.8;
const float ct2 = 32.0;
const float ct3 = 273.16;
int main(void)
{
double temp;
printf("enter a temperature: ");
scanf("%lf", &temp);
while(temp >= 0 || temp < 0 ) // there is a bug
{
Temperatures(temp);
printf("enter a temperature(q to quit): ");
scanf("%lf", &temp);
}
printf("bye.\n");
return 0;
}

void Temperatures(double f)
{
double cel, kel;
cel = ct1 * f + ct2;
kel = cel + ct3;
printf("Fahrenheit temperature: %.2f, Celsius temperature: %.2f, \
Kelvin temperature: %.2f\n", f, cel, kel);
}


/* i need to input a number, if it is a character, then quit. but there is a bug in this program, when i input a char, it did not result correctly. how to write code in while condition? thank you. */
Reply With Quote
  #2  
Old 24th September 2012, 01:03 PM
ikamusume Offline
Registered User
 
Join Date: Sep 2012
Location: UK
Posts: 6
linuxfirefox
Re: a bug in a small exercise program.how do i correct it. thank you.

From man scanf

Code:
       These  functions  return  the  number  of input items successfully
       matched and assigned, which can be fewer  than  provided  for,  or
       even zero in the event of an early matching failure.
So you could check if scanf returns 1 do the conversion, else just quit.
Reply With Quote
Reply

Tags
bug, correct, exercise, program, programhow, small

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Exercise 1-11 , how to run the program to test the word count program? JoeNapoleon Programming & Packaging 2 17th November 2011 04:37 AM
I need an XP emulator for one small program tweakedlogic Using Fedora 4 4th December 2008 06:40 AM
Correct way to 'run' a program glennzo Using Fedora 17 20th October 2007 11:41 PM
Small program I'm working on... Flounder Wibble 8 30th January 2007 06:28 PM


Current GMT-time: 02:39 (Thursday, 23-05-2013)

TopSubscribe to XML RSS for all Threads in all ForumsFedoraForumDotOrg Archive
logo

All trademarks, and forum posts in this site are property of their respective owner(s).
FedoraForum.org is privately owned and is not directly sponsored by the Fedora Project or Red Hat, Inc.

Privacy Policy | Term of Use | Posting Guidelines | Archive | Contact Us | Founding Members

Powered by vBulletin® Copyright ©2000 - 2012, vBulletin Solutions, Inc.

FedoraForum is Powered by RedHat