Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

C 

Struggling with loops?

Hey, my code is here: .The issue is, when a user enters 5, it DOES take all desired inputs, but no matter what, when "Run" is entered, it loops "Incorrect Pattern Size, try again.. any help?
I think my problem is with the while loop i made, so i'll show that here:

Code
int main()
{
    int n, s;
    string EndString;
    cout << "1. pattern one" << endl;
    cout << "2. pattern two" << endl;
    cout << "3. pattern three" << endl;
    cout << "4. pattern four" << endl;
    cout <<"5. Unlimited Entries!" << endl;
    cout << "6. quit" << endl;
    cout << "enter your choice" << endl;
    cin >> n;
    if(n == 5){
        cout << "Entries will now run forever. When Finished, Type 'Run' to Display" << endl;
        do{cout << "enter pattern size";
    cin >> s;
    if (s >= 2 && s <= 9)
    {
        switch (n)
        {
        case 1:
            pattern1();
            break;
        case 2:
            pattern2();
            break;
        case 3:
            pattern3();
            break;
        case 4:
            pattern4();
            break;
        default:
            cout << "quit";
        }
    }
    else
        cout << "pattern size incorrect try again";





    if( n >= 6){
        cout << "Terminating Program..." << endl;
        return 0;
    }
    cout << "enter pattern size";
    cin >> s;
    if (s >= 2 && s <= 9)
    {
        switch (n)
        {
        case 1:
            pattern1();
            break;
        case 2:
            pattern2();
            break;
        case 3:
            pattern3();
            break;
        case 4:
            pattern4();
            break;
        default:
            cout << "quit";
        }
    }
    else
        cout << "pattern size incorrect try again";
}while(EndString != "Run" || "run");}
}
else
cout << "pattern size incorrect try again";
^^ b4 the while loop check
[Image: bitcoin.png] Be Nice For Once 15hVUVz7xA4kazHdk2BuErENGCskVwjYXP
Your indentation makes this very hard to read. Can you try fixing the indentation first please? Perhaps that might make your error obvious to yourself as well.
(Apr 22, 2018 01:12 PM)GoddessTaliyah Wrote:  Hey, my code is here: .The issue is, when a user enters 5, it DOES take all desired inputs, but no matter what, when "Run" is entered, it loops "Incorrect Pattern Size, try again.. any help?
I think my problem is with the while loop i made, so i'll show that here:

Code
int main()
{
int n, s;
string EndString;
cout << "1. pattern one" << endl;
cout << "2. pattern two" << endl;
cout << "3. pattern three" << endl;
cout << "4. pattern four" << endl;
cout <<"5. Unlimited Entries!" << endl;
cout << "6. quit" << endl;
cout << "enter your choice" << endl;
cin >> n;
if(n == 5){
       cout << "Entries will now run forever. When Finished, Type 'Run' to Display" << endl;
       do{cout << "enter pattern size";
cin >> s;
if (s >= 2 && s <= 9)
{
switch (n)
{
case 1:
pattern1();
break;
case 2:
pattern2();
break;
case 3:
pattern3();
break;
case 4:
pattern4();
break;
default:
cout << "quit";
}
}
else
cout << "pattern size incorrect try again";





if( n >= 6){
       cout << "Terminating Program..." << endl;
       return 0;
}
cout << "enter pattern size";
cin >> s;
if (s >= 2 && s <= 9)
{
switch (n)
{
case 1:
pattern1();
break;
case 2:
pattern2();
break;
case 3:
pattern3();
break;
case 4:
pattern4();
break;
default:
cout << "quit";
}
}
else
cout << "pattern size incorrect try again";
}while(EndString != "Run" || "run");}
}

Well yeah your lopping that statement, just cut it off with a strategically placed } by your if statement. as previously stated the indentation burns my eyes.
"You can pick your friends, you can pick your nose, but you can't pick your friend's nose, because they usually get mad, butt we all know there hiding secrets in there" -James Bond
Do none of you have formatting tools? Takes me 2 seconds to format this code into my own format in Sublime.

It's because you don't assign EndString by anything, and your input is to an int type. Strings obviously do not work with that and when you enter a string all int extractions from std::cin will continue to fail because you haven't cleared the state flag nor have you junked the data from the stream. If you want string and int, you'll have to take input strictly as a string, then unless the input was "run" (and your case sensitive checking logic is also flawed), if it isn't convertible to a value that can be represented by int, then fail.

You can get case-insensitive string comparison by creating your own string type with different char traits.
Getting Started: C/C++
[C/C++/.NET/x86 ASM - Automation Programmer]


Possibly Related Threads...
Thread / Author Replies Views Last Post
[Python] by n00b.caApr 4, 2020 12:39 PM 3 201 Apr 4, 2020 12:39 PM
Last Post: n00b.ca
[Java] by YukinoNov 12, 2019 05:50 PM 6 426 Nov 12, 2019 05:50 PM
Last Post: Accord
[Java] by ComputroniumMar 3, 2018 11:38 AM 2 228 Mar 3, 2018 11:38 AM
Last Post: Computronium