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.

The while (contReset=='n'||contReset=='N');//and this part comes in the end:

int main(void)
{
    do { // this is where you had typed [I]this is the problem loop[/I]
      
      // code here

    } while (contReset=='n'||contReset=='N'); // here is where it needs to be.

    return 0;

}

The if statement and do-while statements are mixed up.
Basically, you wrote

do {
	if (/*expression*/)
	{
		/* some code */
	} while ( /*expression*/ );
}

Thanks guys.

I've corrected the initial fault and now its working fine, and I've finished the rest of the program. I have hit another problem though, I was told by my lecturer that we should try and put in some form of error checking which will ask the user for another input if they enter something that isn't one of the valid choices.

I have had a look through a few examples but don't really get it. I mean, there is a method that made sense but would result in having to add a huge amount of code to this, which I wanted to try and avoid.

Below is the code up to the end of the 2nd request for user input, would somebody please be able to show me how I would go about getting some error checking on the inputs.

Thanks very much.

#include <math.h>
#define PI 3.14159265358979323846264338327

int main() {

int initialChoice,tempChoice;
float tempCelsius,tempCtoF,tempFahrenheit,tempFtoC,radius,volume,height;
float sideA,sideB,sideH,sideHSquared;
char contTemp,contReset,contLength,contVolume;
int errorFlag=0;

    do    {
        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");
        errorFlag=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 you want to check if the user's input is valid, you can just put a do-while loop around the code that gets the input from the user.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.