I've been set in a lab session at University the task of creating a program which can perform one of a variety of calculations, with it performing the one specified by the user, and with it looping back after every calculation however it is the looping which is causing me problems.
Shown below is my current code and under it is what is going wrong.
#include <stdio.h>
#include <math.h>
int main(void) {
int initialChoice;
int tempChoice;
float tempCelsius;
float tempCtoF;
char contTemp;
char contReset;
float tempFahrenheit;
float tempFtoC;
do//this is the problem loop
{
printf("\nPlease choose one of the following options:\n");
printf("1:\tTemperature Conversion\n");
printf("2:\tCylinder Volume\n");
printf("3:\tPythagoras's Theorem\n");
printf("\nPlease enter your choice, 1, 2 or 3\n");
scanf("%d",&initialChoice);
if (initialChoice==1)
{
do
{
printf("\nYou have chosen temperature conversion\n");
printf("This will convert a given temperature from Fahrenheit to Celsius, or Celsius to Fahrenheit\n");
printf("\nPlease choose what you would like to do from the following options:\n");
printf("1:\tCelsius to Fahrenheit\n");
printf("2:\tFahrenheit to Celsius\n");
printf("\nPlease enter your choice, 1 or 2\n");
scanf("%d",&tempChoice);
if (tempChoice==1)
{
printf("\nPlease enter the inital temperature in Celsius\n");
scanf("%f",&tempCelsius);
tempCtoF=(tempCelsius*1.8)+32;
printf("\n%0.2f Celsius is equal to %0.2f Fahrenheit\n",tempCelsius,tempCtoF);
}
else if (tempChoice==2)
{
printf("\nPlease enter the inital temperature in Fahrenheit\n");
scanf("%f",&tempFahrenheit);
tempFtoC=(tempFahrenheit-32)/1.8;
printf("\n%0.2f Fahrenheit is equal to %0.2f Celsius\n",tempFahrenheit,tempFtoC);
}
printf("\nWould you like to perform another temperature conversion?\n");
printf("Y/N\n");
fflush (stdin);
scanf("%c",&contTemp);
}while (contTemp=='y'||contTemp=='Y');
printf("\nWould you like to exit the program?\n");
printf("Y/N\n");
fflush(stdin);
scanf("%c",&contReset);
}while (contReset=='n'||contReset=='N');//and this
}
return 0;
}
Essentially the loops are working fine till I get onto the one that asks if they want to exit the program.
What I want to happen is that if they say no then the program returns to the initial choice (from line 19 to 24), however when I added this loop in the program won't compile.
The error I'm getting is:
1>c:\users\alex\documents\visual studio 2010\projects\week 1\week 1\work1.c(72): error C2059: syntax error : 'return'
I've got absolutely no idea what has gone wrong with this loop. If I remove it then the program works fine, but I need the loop to be there.
Please can somebody take a look over the code and tell me where I'm going wrong. Also, I apologise for how messy it all is, this is what happens when you have never used C before!
Thanks very much.