Okay, so I made a console application that - since I'm a beginner - took me a total of 8 hours to make so that it is easily expansible and easily read. I think that this lives up to the standards. But, please, I'm not concerned about the standards right now. My programs are sometimes like duct tape. They aren't pretty, but they work. So, don't go rambling on about the standards and making my code look pretty. I'm currently concerned about functionality. Now, with that rant out of the way...
Here is the code. I'm using the Code::Blocks in conjunction with the GNU GCC compiler. No errors from the compiler. Just from Windows. One of those 'send error report sorry for the inconvenience' things.
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");
int x = LOOPNUM; //I had to assign something to x, so that my loop would just go over and over. cool, huh?
while (x = LOOPNUM)
{
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);
}
Okay. There are two problems I'm having. When I run the program and type in 2 to go to tempconvert(), I then type in 1, 2, or 0 and it gives me the previously described error. However, the default works fine. By commenting out most of fahrtocels() and adding a printf() to it, I was able to determine that the problem is not within the functions fahrtocels() or celstofahr(). This can also be determined by the fact that my 0 case also comes up with an error.
Now. On to the other problem. By commenting out the contents of main() and replacing them with nothing but calls to fahrtocels() and celstofahr(), and, while the scanf() did come up, I was able to determine by putting a printf() after it that it didn't complete. Also, they return the same value every time. fahrtocels() returns one value and celstofahr() returns a different one.
And, if I wasn't clear enough on what I just said, you can always just run it yourself. I've been trying for a long time to fix it up, but just can't. Oh, and everything that I didn't discuss works fine.
Sorry for the long post! (-: