Code:
#include <stdio.h>
#include <stdlib.h>
#define LOOPNUM 42 //just a way to keep a loop going infinitely without a GOTO
/***********************************************/
/* The point of this program is to create a */
/* variety of different functions that can be */
/* called through main. It's really just going*/
/* to be every C program I make made so that I */
/* can call any one of them at a whim. */
/***********************************************/
int inputnum; //main()'s scanf input variable
/**********************/
/* Main */
/**********************/
int main()
{
printf("Okay. This is a computer program that is specifically geared toward a \ncompendium of all the useless things I can do with the C Programming language.\n");
printf("Remember. The number corresponds to a certain function. There is a quit \nfunction as well. Just type in '0'.\n");
printf("Also, remember. This works as a command line would. Once you finish, a \nnew line appears. Then, you can put something else in.\n\n");
printf("Hello, world!-----------------------------------------------------------1\n");
printf("2-way temperature converter---------------------------------------------2\n\n");
while (1)
{
printf("-->"); //this pointer looks nice, doesn't it?
inputnum = 100; //in order to reset it to a value every time, so that I don't get an infinite loop
scanf("%d", &inputnum);
skipgarb();
switch(inputnum) //first time using switch, this is probably not very good form
{
case 1 : helloworld();
break;
case 2 : tempconvert();
break;
case 0 : exit(0);
default: printf("Uhhhmmm...well, this is awkward. Please, try again.\n");
break;
}
}
return 0;
}
/********************************************************************************************************************************
****************************************************/
skipgarb() //skips user input garbage
{
while (getchar() != '\n')
{
;
}
}
/********************************************************************************************************************************
****************************************************/
helloworld() //exactly as it says
{
printf("Hello, world!\n");
}
/********************************************************************************************************************************
****************************************************/
tempconvert() //I'm having some problems with this one right now
{
int z;
int x = LOOPNUM;
printf("Okay. Start by putting in a number. Enter 1 for a fahrenheit to celsius \nconversion, and enter 2 for a celsius to fahrenheit conversion. As always, put in 0 to
exit.\n");
while (x = LOOPNUM)
{
int m;
printf("-->");
int z = 100; //see line 26
scanf("%d", &z);
skipgarb();
switch(z)
{
case 1 : fahrtocels();
x = 1;
break;
case 2 : celstofahr();
x = 1;
break;
case 0 : system("cls");
main();
x = 1; //I know i won't need this. Just a precaution in case of the (supposedly) impossible possibility of exiting from main but not the program via scanf input.
break;
default: printf("Mmmmmm...try again. Remember. 1, 2, or 0.");
break;
}
}
}
/********************************************************************************************************************************
****************************************************/
fahrtocels() //convert fahrenheit to celsius
{
float fahrenheit;
float answer;
printf("Enter your fahrenheit value.\n");
scanf("%f", &fahrenheit);
answer = 5.0 / 9.0 * (fahrenheit - 32); //don't you just love math?
printf("%f\n", answer);
}
/********************************************************************************************************************************
****************************************************/
celstofahr() //convert celsius to fahrenheit
{
float celsius;
float answer;
printf("Enter your celsius value.\n");
scanf("%f", &celsius);
answer = (9.0/5.0)*(celsius+32);
printf("%f\n", answer);
}
So, a few things:
1. Is it against so-called "standards" to call main?
2. Is there is distinguishing mark as to when to use if statements vs. a switch?
3. Anything else I might have done wrong?
4. Oh, and who makes the official programming standards?
Because, this program works. But, is it 'right' by the standards, and by what other programmers will consider 'right'? It looks nice to me, but is it well-written and easy to read?