We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,085 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Ask user yes or no loop back to main page

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();
 }
2
Contributors
4
Replies
3 Days
Discussion Span
1 Year Ago
Last Updated
6
Views
PIXXAR
Newbie Poster
3 posts since Mar 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

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;
}
Labdabeta
Practically a Master Poster
615 posts since Feb 2011
Reputation Points: 27
Solved Threads: 31
Skill Endorsements: 1

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

PIXXAR
Newbie Poster
3 posts since Mar 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 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
Labdabeta
Practically a Master Poster
615 posts since Feb 2011
Reputation Points: 27
Solved Threads: 31
Skill Endorsements: 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

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

PIXXAR
Newbie Poster
3 posts since Mar 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.1081 seconds using 2.75MB