Dudearoo
Useing Code::Blocks

Hey you guys.

ive got a problem, ive got a simple while loop being executed that loops 41 lines of code untill the user types no
for example

int repeat = 2;
while (repeat == 2)
{

// my code

//     || Example Code 
//     \/
cout << " yes or no" << endl;
cin.getline(teamnamesquestion, 5);
if (teamnamesquestion == "no" || teamnamesquestion == "No" || teamnamesquestion == "NO")
{
// BLANK or have repeat = 2; repetive though.
}
if (teamnamesquestion == "yes" || teamnamesquestion == "Yes" || teamnamesquestion == "YES")
{
repeat = 0;
break;
}
system("pause");
system("cls");
return0;
}

this seams like repeat should break the while loop when set to two,
in fact it does not. weard?
could you guys please help me fiqure out the problem, thanks.

Indent your code. It's impossible to read. Are you sure you aren't missing bracket(s)? Why is there are a "return 0" at the bottom of the while loop? Why does the answer "yes" have a "break" statement in it? I thought "no" was supposed to break the loop from your description.

You have an explicit return before the end of the loop, so there is no looping going on. All of what VernonDozier said goes double for me.

Why don't you use like this:

#include <iostream>
#include <string>
using namespace std;
int main(){
    int repeat = 2;
    string answer;
    while (repeat != 0){
        cout << "Insert yes or no: " << endl;
        cin>>answer;
        if (answer == "no" or answer == "No" or answer == "NO"){
            repeat--;
        }
        if (answer == "yes" or answer == "Yes" or answer == "YES"){
            break;
        }
    }
    cout<<"Press ENTER (multiple times if needed) to exit.";
    cin.get();
    cin.get();
    return (0);
}

This will break the while loop after the keyword yes is typed in, or if the keyword no is typed 2 times.
And for further posting, please use indentation when you write code, it's really hard to figure out where things start, or end.

Edited 4 Years Ago by Lucaci Andrew

Comments
Yes, std::string is the way to go!
Please don't post answers for people. Help them find their own answers.

sorry guys return0; is supost to be after terminating loop and i will start to indent my code :)
lucaciandrew your code is not clean enough, i want to type no to have the loop continue, yes to stop it.

and pressing enter a few times? I would like just one time :)

I still would like some help on the loop end, if i type yes repeat = 0; weird dosent work? could you please help me fiqure out the problem here thanks.

Judging by the way you use getline, teamnamesquestion must be a char array. The problem with this is that teamnamesquestion == "no" performs a pointer equality check, that will, obviously, always return false.

If you don't believe me, try this:

#include <iostream>

using namespace std;

int main()
{
    char str[10];

    cin.getline(str, 5);

    if (str !=  "no") cout << "foo" << endl;
    if (str != "yes") cout << "bar" << endl;

    std::cin.get();
}

What you can do is either use strcmp instead of the == operator...

#include <iostream>
#include <cstring>

using namespace std;

int main()
{
    char str[10];

    cin.getline(str, 5);

    if (strcmp(str,  "no") == 0) cout << "foo" << endl;
    if (strcmp(str, "yes") == 0) cout << "bar" << endl;

    std::cin.get();
}

...or use a std::string instead of a char array, as lucaciandrew suggested...

#include <iostream>
#include <string>

using namespace std;

int main()
{
    string str;

    getline(cin, str);

    if (str ==  "no") cout << "foo" << endl;
    if (str == "yes") cout << "bar" << endl;

    std::cin.get();
}

Vernon Dozier said

Indent your code. It's impossible to read.

You've ignored him. If you are going to ignore the help you have already been given why should we help you further?

Thanks lucaciandrew and m4ster_r0shi ! the std::string works perfectley! thanks !

Please can you guys also give me a short teaching of how to do keyboard input?
i have another thread Here
Regarding my inquiries about key input.
and WaltP Plus VernonDozier , I'll have my code more indented so to be easer to read.

thanks again you guys :)

You are on your way to c++ coding mastery. As for keyboard input, look at some of the massively available open source code available. In the open source universe, there is no such thing as stealing (unless you don't give credit where credit is due!)... :-)

This question has already been answered. Start a new discussion instead.