continue a program ?

Pages: 12
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# include <iostream> 
using namespace std;

int main ()

{
    
int a,b,c ;
cout <<" enter ur first no. " << endl;
cin >> a; 
cout  <<" enter ur second no. " << endl ;
cin >> b ;
cout <<" ur ans is " << (a+b) <<endl;
scanf ("%d%",&c);
return 0 ;
}


umm ok if i want to continue this program like after d addition gets over i want the program to ask the digits for addition again ? how can i do so ?
To keep the program going keep adding more code... Am I misunderstanding your request? You could also use a loop of some kind, in that case there are a few choices like a for() loop or a while() loop or a do...while loop. How many interations do you want?
Try using a do while loop.

1
2
3
4
5
6
7
8
9
do 
{
      char input;// variable that stores user's choice

      // enter program here

     cout << "Do you want to quit?\n[Y]es  [N]o/n";
     cin >> input;
} while ((input ==  'N') || (input == 'n'))
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// Way one:

int main ()
{
    
    // Code here...
    
    // When we return we call the main function
    return main ();
}

// Way two:

int main ()
{
    // Label to go to later
    StartOfMain:
    
    // Code here...
    
    // Go to the label
    goto StartOfMain;
    
    return 0;
}

// Way three:

int main ()
{
    // A loop
    // Do while:
    do
    {
        // Code here...
    } while (0 == 0);
    // Or a while:
    while (0 == 0)
    {
        // Code here...
    }
    // Or a for:
    for (;;)
    {
        // Code here...
    }
    
    return 0;
}
Way one:
You can't do this because you aren't allowed to call main() (and even if you could, you could crash the system due to infinite recursion)

Way two:
That isn't what goto is for, don't use it like that.

Way three:
I personally would use while(true) because it's shorter, but...
Way one works fine for me, its what I use when I need to do this.

Way two was just an example, and if it works for him, I doubt he really cares if it is used for what it is for or not.

Way three, lol, shorter by two spaces =).
I personally would use while(true) because it's shorter, but...


Even shorter would be while (1).

I still prefer my method above because it allows the user the choice to quit.
An infinite while or for loop would still allow the user to choose to quit:

1
2
3
4
5
6
while(true)
{
    // ...
    if(foo == "quit")
        break;
}

Or you could use a bool as condition:

1
2
3
4
5
6
while(!done)
{
    // ...
    if(foo == "quit")
        done = true;
}
Last edited on
the while stuff worked pal ..thanks :)
umm how can i make the user decide whether he wants to continue or not ?

something like
cout<<"do u want to continue (y/n)";

and plz show it by entering the "stuff" that is required to do so in my code

thanks
Browni3141 did exactly what you are asking in the 3rd post.
yea but i kinda dont know where to put it ...plz show it by putting it in my code
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> 
using namespace std;

int main ()

{
    do 
    {
        char input;// variable that stores user's choice
    
        // enter program here
        int a,b,c ;
        cout <<" enter ur first no. " << endl;
        cin >> a; 
        cout  <<" enter ur second no. " << endl ;
        cin >> b ;
        cout <<" ur ans is " << (a+b) <<endl;
        scanf ("%d%",&c);
   
        cout << "Do you want to quit?\n[Y]es  [N]o/n";
        cin >> input;
    } while ((input ==  'N') || (input == 'n'))

    return 0 ;
}
it says 22 `input' undeclared (first use this function)

EDIT: made some changes to the program and it compiles fine..but kinda dont understand how it works..ran it after the line says do u want to continue (y/n)

if i press y nothing happens if i press n nothing happens ?
Last edited on
If you use the code we've posted exacty, you're program should work fine. You probably didn't put char input at line 9, and the code is written to work if you ask "Do you want to quit?" not "Do you want to continue?"
nope doesn't work and by the way when the "do u want to quit " comes what should we press "Y"??

or what should happen if we select no..

coz nothing is happening in me
ricomoss, to use the scanf () function you need more includes like stdio.h or conio.h, not just iostream.h.
Yes, I know what is there won't compile. I just did a quick copy/paste so he could see what we were talking about as far as placement.

I probably won't address this anymore until I can see complete questions being asked. (I hate "coz", "ur", etc)
i dont think so CPProgrammer4l because i have become using scanf() from the start for pausing a program only with the <iostream> library
sorry ricomoss but please answer my question :(
Pages: 12