1.11M Members

Ask user yes or no loop back to main page

 
0
 

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();
 }
 
-1
 

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;
}
 
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,,

 
1
 

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
 
0
 

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...

You
This article has been dead for over six months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: