Hey everyone! Just joined today and am loving cprogramming.com ! Learned everything so far for C at this website (and some C++ but choosing to learn C first for various reasons )
I am programming on a Windows machine and trying to make a DOS calculator while I learn new techniques (I haven't programmed in a long time and am coming back, again).
Here is the source code to a recent project I a have been working on. I got a few problems though. I am having trouble with my exit function. You'll see what I mean once you run it. I don't know if that if loop is wrong or what, but it ain't working properly.
Also, could I use anything else besides a goto loop because I heard those 'never ending' loops are killer. If anything you spot seems a little weary, help me! I'm learning!
Code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
/////////////////////////////////////////
// Functions for mathematical equations //
/////////////////////////////////////////
int addition(int x, int y);
int subtraction(int x, int y);
int multiplication(int x, int y);
int division(int x, int y);
int main(void)
{
int choice;
int num1;
int num2;
int answer;
int exit;
/////////////////////////////////////////
// Ints for function prototypes //////////
/////////////////////////////////////////
int x;
int y;
int yes = 0;
int no = 1;
Start:
system("CLS"); //clear screen cache
system("COLOR 4F"); //red bkgd, white text
printf(" Welcome to Drew's Calculator 1.0\n"); /*Title, this will be centered as best as possible*/
printf("\n\nSelect which type of operation you want to execute:\n");
printf("1 - Addition\n");
printf("2 - Subtraction\n");
printf("3 - Multiplication\n");
printf("4 - Division\n");
printf("5 - Exit\n\n");
scanf("%d\n", &choice , "\n");
switch(choice){
case 1: //addition\\
printf("\n");
scanf("%d%d", &num1, &num2);
printf("\n\n%d", addition(num1, num2));
getch();
goto Start;
break;
case 2: //subtraction\\
printf("\n");
scanf("%d%d", &num1, &num2);
printf("\n\n%d", subtraction(num1, num2));
getch();
goto Start;
break;
case 3: //multiplication\\
printf("\n");
scanf("%d%d", &num1, &num2);
printf("\n\n%d", multiplication(num1, num2));
getch();
goto Start;
break;
case 4: //division\\
printf("\n");
scanf("%d%d", &num1, &num2);
printf("\n\n%d", division(num1, num2));
getch();
goto Start;
break;
case 5: //exit\\
printf("\n\nAre you sure you wish to exit? \n");
printf("\n0 - yes \n1 - no");
scanf("%d", &exit);
if(exit == yes)
{
system("EXIT");
}
else if (exit == no)
{
goto Start;
}
else
{
goto Start;
}
break;
default:
goto Start;
break;
}
getch();
return 0;
}
/////////////////////////////////////////
// Prototypes for functions //////////////
/////////////////////////////////////////
int addition(int x, int y)
{
return x + y;
}
int subtraction(int x, int y)
{
return x - y;
}
int multiplication(int x, int y)
{
return x * y;
}
int division(int x, int y)
{
return x / y;
}
I'm gonna constantly keep changing and making this code better (using arrays in the future or something). This is just what I have learned so far.