To quit safely


I have been learning c/c++ for about two weeks now and am having trouble with this code.
I am not getting any errors, but it is buggy and I don't know what I am missing.

P.S. I am not a student of any school

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
#include <iostream>
#include <string>

using namespace std;

int main()
{
    string sAnswer = "";
    cout << "\n Should I say bye bye? \n";

        cout << "\n [Y]es OR [N]o \n";
        getline(cin, sAnswer);

            if (sAnswer == "Y" || sAnswer == "y")
            {   cout << "\n bye bye and come again \n";
                return 0;
            }
            else if (sAnswer == "N" || sAnswer == "n")
            {   sAnswer.erase(0,10000);
                main();
            }
            else (sAnswer != "Y" || sAnswer != "y" || sAnswer != "N" || sAnswer != "n");
            {   cout << "Please type Y for yes or N for no.\n";
                sAnswer.erase(0,10000);
                main();
            }
}


If you type "Y" it will close as it should, but if you type "N" then "Y", that is when the bugs show up. Your assistance is appreciated.

by the way is their any tutorials on per-planing for programers? That would help me wright better code.
In C++, it's an error to call the main() function. Consider learning about loops
if you type "N" then "Y", that is when the bugs show up

This is because you're calling main() recursively. Do NOT call main recursively. Looping constructs exist for what you're trying to do.

If you type "N", then you call main again. Within that call to main, you type in "Y", then zero gets returned up the stack. This does not end the program.

Then, back in main (i.e. the the first entry into main), the code that's "within" the else clause gets called. I say "within" because the code after your else clause is equivalent to this:

1
2
3
4
5
6
7
8
else
{
   (sAnswer != "Y" || sAnswer != "y" || sAnswer != "N" || sAnswer != "n");
}

cout << "Please type Y for yes or N for no.\n";
sAnswer.erase(0,10000);
main();


i.e. in your else, that boolean expression is evaluated and its result is discarded. The braces after your else simply serve as a scope and nothing more.

The else clause does not require an expression like the if or else if. You should remove the

(sAnswer != "Y" || sAnswer != "y" || sAnswer != "N" || sAnswer != "n");

from your else clause.

And always be careful about trailing semi-colons after else clauses. They make it so the else clause does nothing. Compilers should warn about that though.
Last edited on
I considered all you had to say Cubbi and shacktar and here is my revised 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
26
27
28
29
30
31
#include <iostream>
#include <string>

using namespace std;

int main()
{   int iLoop = 1;
    --iLoop;
    do
     {  cout << "\n Should I say bye bye? \n";
        cout << "\n [Y]es OR [N]o \n";
        string sAnswer = "";
        getline(cin, sAnswer);


            if (sAnswer == "Y" || sAnswer == "y")
            {   cout << "\n bye bye and come again \n";
                return 0;
            }
            else if (sAnswer == "N" || sAnswer == "n")
            {   sAnswer.erase(0,10000);
                iLoop++;
            }
            else
            {   cout << "Please type Y for yes or N for no.\n";
                sAnswer.erase(0,10000);
                iLoop++;
            }
    }
    while (iLoop >= 1);
}


It works now, Thank You.
Here's an alternate way to write it:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <string>

int main()
{ 
    for ( ; ; )
    {
        std::cout << "\n Should I say bye bye?\n\n [Y]es OR [N]o\n" ;

        std::string answer ;
        std::getline(std::cin, answer) ;

        if ( answer == "Y" || answer == "y" )
        {
            std::cout << "\n Bye bye and come again!\n" ;
            return 0 ;
        }

        if ( !(answer == "N " || answer == "n") )
            std::cout << "Please type Y for yes or N for no.\n" ;
    }    
}


There is no point in sAnswer.erase(0,10000) as sAnswer[/t] is recreated every loop. Even were it not, [tt]getline discards the former value.

There is also not much point in your iLoop variable since the loop ends when sAnswer is "Y" and not when iloop is <= 0. while(true) would've been preferable.
Topic archived. No new replies allowed.