Program Repeating, How to end? Reply Pls

Sorry guys, I've just entered this field of programming..No good knowledge i got on it...

My question is how do i end my program?
Once the addition is done And after pressing 'y' The addition program repeats and then I'm not able to get any y/n to shut down the program... sorry for my bad English ...Please tell me what error i've done and what should i do to fix it..
My code goes here below

#include<iostream.h>
#include<conio.h>

void addition()
{
clrscr();
int a,b;
cout<<"Input two numbers to add them: \n";
cin>>a>>b;
cout<<a+b;
getch();
}

void endprog()
{
char ans;
cout<<"\nEnter y to continue adding\n";
cin>>ans;

while(ans == 'y'|| ans == 'Y')
{
addition();
}
}

void main()
{
addition();
endprog();
}


Using turbo c++ ... Please reply on this ASAP.. waiting for good replies and fixes..
Why do you have getch(); inside void addition? Remove it.

If the user enters y or Y, they will be stuck in an infinite loop that calls void addition over and over.
Look at this and see the differences from your code:
1
2
3
4
5
6
7
8
9
10
    char ans;
    cout<<"\nEnter y to continue adding\n";
    cin >> ans;

    while(ans == 'y'|| ans == 'Y')
    {
        addition();
        cout<<"\nEnter y to continue adding\n";  //ask again after every additional addition
        cin >> ans;  //this way the user can quit by entering something other than y or Y
    }


And please use something other than Turbo C... Please! Turbo C is very old and outdated. You will be learning non-standard C++! I recommend using CodeBlocks with a gcc compiler instead.
@kevinkjt2000
thanxx for your reply
I've corrected my code and Its working Perfect... Thanks And Please Check the code below..
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include<iostream.h>
#include<conio.h>
void main();
void endProg()
{
clrscr();
char ans;
cout<<"\nQuit? Enter Y/N\n";
cin>>ans;
if(ans=='n'|| ans=='N')
main();
else if(ans=='y'|| ans=='Y')
cout<<"Program Ended";
else
cout<<"Invalid Key, Press Y/N";
getch();
}

void addition()

{
clrscr();
int a,b;
cout<<"Input two numbers to add them\n";
cin>>a>>b;
cout<<a+b;
getch();
endProg();
}

void main()
{
addition();
endProg();
}


I was using gcc compiler and codeblocks stuff in my vacations... but now due to my college and classes uses Turbo C .. we need to use the software.. This is INDIAN Education System bro .. Its hell haha.. Thanks bro and bye
Topic archived. No new replies allowed.