continue a program ?

Pages: 12
Oh, my bad. Never mind, I tried using scanf () with only iostream.h and it works.
<iostream.h> is the old, pre-standard header. Always use <iostream> instead.
I'm not sure what you are trying to accomplish. This is how I did it...

This will add two numbers together and output the answer. Then it will ask if you would want to repeat the process.
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
#include <iostream>
#include <string>

int main ()

{
	std::string input;
    do
    {
        // enter program here
        int a;
        int b;
        std::cout <<"Enter the first number."   << std::endl;
        std::cin  >> a;
        std::cout << "Enter the second number." << std::endl;
        std::cin  >> b;
        std::cout << "The answer is: " << a+b   << std::endl;

        std::cout << "Do you want to quit?" << std::endl
                       << "[Y]es  [N]o" << std::endl;
        std::cin   >> input;
    } while ((input ==  "N") || (input == "n"));

    return 0 ;
}
yea that works thanks but what if i want to quit ..if i press y nothing happens

and if you have got time could you please explain

while (input == "N")|| (input == "n"));

by doing this how did the program jump to the start

thanks
I've compiled this and run it on several platforms (Kubuntu (gcc), XP (cygwin) and Solaris (gcc)) and everything seems fine to me.

If I press 'n' the program continues. If I press 'y' the program exits. I'm not sure what's going wrong for you. :(

The reason the program jumps to the start is because of the mechanics of a do-while loop. Where it says do is where the loop begins. The program enters the loop and executes the instructions until it gets to the while part.

You'll notice instructions next to while. Those are the conditions of the loop. If those conditions are true the program jumps back up to do and repeats the process until the while conditions are false.

So, when input is 'N' or 'n' the condition is true and the do-while loop repeats. Otherwise the do-while loop ends and the next instruction is executed. In this case the next instruction is return 0, which exits the program.
yup i got my problem i write scanf ("%d%",&c);

which makes the program to pause.but my problem is my program ends if i dont put the pause command

"not happened in this one "
Topic archived. No new replies allowed.
Pages: 12