Good day! (Yes to all interpretations thereof.)

I am a beginner at C, studying on my own because it’s fun and my goal is to “simply” gain a better understanding of how machines work under the hood. I just successfully wrote a bidirectional Celcius/Fahrenheit converter that also takes some user input. As a next step, I would like to save conversion history. Do I have to save to a file outside the program or can I temporarily save history in a variable or an array and give the user the choice to check history by displaying the contents of that variable/array? If the answer is saving to an outside file, then I digress, as this is next on my “curriculum” (still self studies). If the answer is that I can save previous input into variables or an array, how do I make a - for instance - for loop go through the elements of that array, assigning one user input (temperature conversion) in one element at a time?

Also, if this is more advanced than what I am make it sound like, please let me know, and I’ll let it be for the time being.

#include <stdio.h>  

//Function declarations  
float toCelcius(float fahrenheit);  
float toFahrenheit(float celcius);  

//Bidirectional temperature converter  

int main() {  

	int f_value;  
	int c_value;  
	int choice;  

	printf("\nWelcome to this Fantasic Bidirectional Temperature Converter!\n");  
	printf("\nWhat would you like to do? Press f + enter to convert Fahrenheit to Celcius, c + enter to convert Celcius to Fahrenheit or ctrl + c to exit: ");  
			
	while ((choice = getchar()) != EOF) {  
		if (choice != 'f' && choice != 'c') {  
			printf("Goodbye!\n"); //ctrl + c does not allow for this to be displayed.  
		}	
		if (choice == 'f') {  
			printf("\nEnter the temperature in Fahrenheit: ");  
			scanf("%d", &f_value);  
			getchar();  
			printf("\n%d degrees Fahrenheit is %.1f degrees Celcius.\n", f_value, toCelcius(f_value));  
		}  
		if (choice == 'c') {  
			printf("\nEnter the temperature in Celcius: ");  
			scanf("%d", &c_value);  
			getchar();  
			printf("\n%d degrees Celcius is %.1f degrees Fahrenheit.\n", c_value, toFahrenheit(c_value));  
		}  
		printf("\nWhat would you like to do next? Press f + enter to convert Fahrenheit to Celcius, c + enter to convert Celcius to Fahrenheit or ctrl + c to exit: ");  
	}  
	return 0;  
}  

//Function definitions  
float toCelcius(float fahrenheit) {  
	return (5.0 / 9.0) * (fahrenheit - 32.0);  
}  

float toFahrenheit(float celcius) {  
	return (celcius * 1.8) + 32;  
}  
  • printf("%s", name);@piefed.blahaj.zoneOP
    link
    fedilink
    English
    arrow-up
    2
    ·
    8 hours ago

    Thanks! Ultimately, down the road, I want to try Assembly and then those languages that give CPUs instruction sets. I am interested in the borders where readable code turns into binary. I just chose C to begin with because I was influenced by Linux and a colleague who used to work with C many years ago.

    • lime!@feddit.nu
      link
      fedilink
      arrow-up
      3
      ·
      7 hours ago

      ah fun! have you checked out ben eater’s ongoing youtube series of building a breadboard computer? he’s selling kits that allow you to follow along from hardware through binary, assembly, to BASIC and C. well worth a look imo.