Start New Discussion within our Software Development Community

I'm trying to my friend with an assignment to turn a number from Fahrenheit to Celsius and Kalvin. All that works fine and good. The problem is that his professor wants him to "The main() function ought to use a loop to allow the user to enter a temperature repeatedly, stopping when a q or other non-numeric value is entered." as said in the directions. We're having trouble figuring out how to do this. I tried using the isdigit() function but haven't got it to work correctly. If someone could help that would be great. Thank you.

// hello.cpp : Defines the entry point for the console application.
//
#include <iostream>
#include "stdafx.h"
#include <conio.h>
#include <ctype.h>
    

void  Temperatures(double f);
    
    
int _tmain(int argc, _TCHAR* argv[])
{
	
    {
        float f;
		
		
        
	while(isdigit(f)== true)
	{
        printf("Please enter a Fahrenheit temperature: ");
        scanf("%f", &f);
        Temperatures((double) f);
        }
       
         return 0; 
        
    }
    
    
}
void Temperatures(double f)
    {
        float Celcius;
        float Kelvin; 
        Celcius=(f-32)/1.8;
        Kelvin=Celcius+273.16;
        
        printf("%ld", f);
        printf("The temperature in Fahrenheit is %.2f degrees.\n", f);
        printf("The temperature in Celcius is %.2f degrees.\n", Celcius);
        printf("The temperature in Kelvin is %.2f degrees.\n", Kelvin);
    }

Try reading your input a character at a time and checking that each one is a digit or a valid numeric symbol.

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

#define MULTI 10    

int main(int argc, char**argv)
{
	char ch[2];
	int x = 0;
	ch[1] = '\0';

	while ((ch[0] = fgetc(stdin)) != '\n')
	{
		if ( isdigit(ch[0]) )
		{
			x = (x * MULTI) + atoi(ch); 
		}
		else
		{
			/*handle none didgits*/
		}
	}

	fprintf(stdout, "x->%d\n", x);
	return 0;    
}
This article has been dead for over six months. Start a new discussion instead.