Hack Forums
[C] Struggling with loops? - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [C] Struggling with loops? (/showthread.php?tid=5828263)



Struggling with loops? - GoddessTaliyah - Apr 22, 2018

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");}
}



RE: Struggling with loops? - HFDone - Apr 22, 2018

else
cout << "pattern size incorrect try again";
^^ b4 the while loop check


RE: Struggling with loops? - Annabelle Lee - Apr 22, 2018

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.


RE: Struggling with loops? - SlowMehMo - May 3, 2018

(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.


RE: Struggling with loops? - 0xDEAD10CC - May 3, 2018

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.