IM HAVING PROBLEM WITH THIS PROGRAM, GOING BACK TO THE MAIN PAGE AND SELECTED PAGE,
IF THE USER ANSWER NO, THE PROGRAM WILL GO BACK TO MAIN MENU
IF THE USER ANSWER YES, THE PROGRAM WILL COMPUTE THE SAME PROBLEM.

help PLS...
THE PROGRAM BELOW WONT GO BACK TO COMPUTE THE SELECTED PAGE IF YOU ANSWER "YES".

#include <stdio.h>
#include <conio.h>

main()

{
float choice,A,CUBE;
char temp;
{
clrscr();
textcolor(BLUE);
gotoxy (24,5);
cprintf("SOLVE FOR THE FOLLOWING VOLUMES\n");
textcolor(345);
gotoxy (26,7);
cprintf("[1]   -    CUBE");
gotoxy (26,8);
cprintf("[2]   -    CONE");
gotoxy (26,9);
cprintf("[3]   -    SPHERE");
gotoxy (26,10);
cprintf("[4]   -    CYLINDER");
gotoxy (26,11);
cprintf("[5]   -    EXIT");
textcolor(RED);
gotoxy (24,14);
cprintf("Enter your choice:   ");
scanf("%f",&choice);

switch(choice)
{
case 1:
  {
    clrscr();
    textcolor (BLUE);
    gotoxy (30,5);
    cprintf("Volume of CUBE");

    textcolor (345);
    gotoxy (24,7);
    cprintf("Enter the side of the cube: ");
    scanf("%f",&A);

    CUBE=  (A*A*A);
    scanf("%2.0f,&CUBE");

    textcolor (345);
    gotoxy (24,10);
    cprintf("The Volume of the CUBE is %2.0f", CUBE);

{
while (temp != 'n' && temp != 'N' && temp != 'y' && temp != 'Y')
 {
textcolor (RED);
gotoxy(24,14);
cprintf("Would you like to compute again(Y/N)? ");
scanf("%c", &temp);
}
if (temp == 'y' || temp == 'Y')

return ;

else
return main();

   break;
  }
  }
  }
  }
 getche();
 }

Recommended Answers

All 4 Replies

This can be easily done with a loop:

int main()
{
    bool exit=false;
    while (!exit)
    {
        //CODE HERE!
        //somewhere have:
        /* if (user_want_to_quit)
                exit=true;
        */
    }
    return 0;
}

i really dont get it, im new to turbo C, but i really appreciate you for the response. im trying to fix this syntax, but i dont know how.

the syntax you made, makes it tough,

i really appriciate if you fix the syntax i made.

tnx in advance,,

The thing is that you cannot (as far as I know) call main recursively (from within itself) and even if you can it is likely a bad idea (if the user wants to do 100 problems you will outrun your stack) The key is to use a loop. Here is an example for your code:

#include <stdio.h>
#include <conio.h>

main()

{
float choice,A,CUBE;
char temp;
bool exit=false;
while (!exit)//loop until we want to exit
{//I do not know why you put a block here... but I am using it :P
clrscr();
textcolor(BLUE);
gotoxy (24,5);
cprintf("SOLVE FOR THE FOLLOWING VOLUMES\n");
textcolor(345);
gotoxy (26,7);
cprintf("[1]   -    CUBE");
gotoxy (26,8);
cprintf("[2]   -    CONE");
gotoxy (26,9);
cprintf("[3]   -    SPHERE");
gotoxy (26,10);
cprintf("[4]   -    CYLINDER");
gotoxy (26,11);
cprintf("[5]   -    EXIT");
textcolor(RED);
gotoxy (24,14);
cprintf("Enter your choice:   ");
scanf("%f",&choice);

switch(choice)
{
case 1:
  {
    clrscr();
    textcolor (BLUE);
    gotoxy (30,5);
    cprintf("Volume of CUBE");

    textcolor (345);
    gotoxy (24,7);
    cprintf("Enter the side of the cube: ");
    scanf("%f",&A);

    CUBE=  (A*A*A);
    scanf("%2.0f,&CUBE");

    textcolor (345);
    gotoxy (24,10);
    cprintf("The Volume of the CUBE is %2.0f", CUBE);
  }
case 5://you do not NEED blocks after a case statement, but they can make your code look nicer
textcolor (RED);
gotoxy(24,14);
cprintf("Would you like to compute again(Y/N)? ");
scanf("%c", &temp);

exit = (!(temp == 'y' || temp == 'Y'));//set the exit variable
  }//end switch
 }//end while
return 0;
}//indentation would have told you that this bracket ends main

The thing is that you cannot (as far as I know) call main recursively (from within itself) and even if you can it is likely a bad idea (if the user wants to do 100 problems you will outrun your stack) The key is to use a loop. Here is an example for your code:

#include <stdio.h>
#include <conio.h>

main()

{
float choice,A,CUBE;
char temp;
bool exit=false;
while (!exit)//loop until we want to exit
{//I do not know why you put a block here... but I am using it :P
clrscr();
textcolor(BLUE);
gotoxy (24,5);
cprintf("SOLVE FOR THE FOLLOWING VOLUMES\n");
textcolor(345);
gotoxy (26,7);
cprintf("[1]   -    CUBE");
gotoxy (26,8);
cprintf("[2]   -    CONE");
gotoxy (26,9);
cprintf("[3]   -    SPHERE");
gotoxy (26,10);
cprintf("[4]   -    CYLINDER");
gotoxy (26,11);
cprintf("[5]   -    EXIT");
textcolor(RED);
gotoxy (24,14);
cprintf("Enter your choice:   ");
scanf("%f",&choice);

switch(choice)
{
case 1:
  {
    clrscr();
    textcolor (BLUE);
    gotoxy (30,5);
    cprintf("Volume of CUBE");

    textcolor (345);
    gotoxy (24,7);
    cprintf("Enter the side of the cube: ");
    scanf("%f",&A);

    CUBE=  (A*A*A);
    scanf("%2.0f,&CUBE");

    textcolor (345);
    gotoxy (24,10);
    cprintf("The Volume of the CUBE is %2.0f", CUBE);
  }
case 5://you do not NEED blocks after a case statement, but they can make your code look nicer
textcolor (RED);
gotoxy(24,14);
cprintf("Would you like to compute again(Y/N)? ");
scanf("%c", &temp);

exit = (!(temp == 'y' || temp == 'Y'));//set the exit variable
  }//end switch
 }//end while
return 0;
}//indentation would have told you that this bracket ends main

IT WORKS PERFECT! Thanks a slot sir, i learn much...

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.