You are here:

C/restart program

Advertisement


Question
Hi,

I was wondering if you could help me. I have a simple C program that consists of only one function. I'd like to add some lines to it so that when the prgram is done, it is going to ask the user if the user wants to re-run the same code without exiting. If the user says "yes" the program will run again and then ask the question agian. I don't want to use GOTO statement.
Thanks,
        Eric


Answer
Hi Eric

This code will solve your problem

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

void main()
{
 char ch;
 do {

 /*
 This part will contain you program
 */

 fflush(stdin);
 printf("\nDo you wish to continue(y/n): ");
 ch = getche();
 }while(ch=='y' || ch=='Y');
}

In case if you need any explanation please feel free to ask me.

regards
Joydeep

C

All Answers


Answers by Expert:


Ask Experts

Volunteer


Joydeep Bhattacharya

Expertise

Ability to solve C and Data Structure problems and puzzles with simple and easy to understand logic.

Experience

C, C++, Java, Data Structure, PHP, Web Designing

Organizations
http://www.scodz.com Designation: webmaster

Publications
http://www.scodz.com

Education/Credentials
Master of Computer Applications

Past/Present Clients
http://analysingc.50webs.com http://www.funforu.com

©2012 About.com, a part of The New York Times Company. All rights reserved.