I'm not sure I'm using for loops correctly

Hi! I’m working on my very text-adventure project in C++. It’s very simple, with no inventory system or character stats; just choose A, B, or C! Here’s the specific assignment. The problem I have is with for loops, and getting them to do what I want them to (which is to check if the user input matches my variables, and print text based on their decision). Here’s what I have so far:

int a = 1, b = 1, c = 1;

        std::cout << "Take it nice and slow? Or kick off the party loud.\n";
        std::cout << "             a) Stealth it\n";
        std::cout << "               b) Go loud\n";
        std::cout << "        c) Let your crew handle it\n";

        for (a != 1 && b != 2 && c != 3; int++;;) {

            if (a == 1 && b != 1 && c != 1;;) {
               
                std::cin >> a;
                std::cout << "There's an alley around the corner that brings\n";
                std::cout << "   you to a wall on the side of the building. \n";
                std::cout << "There's a crack in the wall big enough to fit\n";
                std::cout << "through. But the wall itself is short enough to\n";
                std::cout << "        rappel up over. What to do...\n\n";
                std::cout << "  a) Crawl through the crack in the wall\n";
                std::cout << "  b) Throw your grappling hook\n";
                
                if (a == 1 && b != 1;;) {

                    std::cout << "Looks like the coast is clear, move up\n";

                } else if (a != 1 && b == 1;;) {

                    std::cin >> b
                    std::cout << "You throw the hook over the wall.\n";
                    std::cout << "As it gets a firm hold on the top\n";
                    std::cout << "it lets out an audible CLANG.\n";
                    std::cout << "Nothing happens so you proceed up the wall.\n";
                    std::cout << "You reach the top of the wall and climb over.\n";
                    std::cout << "Upon landing, you turn around to find you're surrounded.\n";
                    std::cout << "You think to yourself how you knew you should have\n";
                    std::cout << "bought the wireless rappel before you're beaten.\n\n";
                    std::cout << "            EPIC FAIL END\n";
                    return 0;

                } else {
                    
                    std::cout << "You sit there, waiting for what feels like days.\n";
                    std::cout << "Eventually, you die from starvation. Good job.\n";
                    return 0;
                }

Attempting to compile this gives me:

text-adventure.cpp: In function 'int main()':
text-adventure.cpp:32:45: error: expected unqualified-id before '++' token
   32 |         for (a != 1 && b != 2 && c != 3; int++;;) {
      |                                             ^~
text-adventure.cpp:32:48: error: expected primary-expression before ';' token
   32 |         for (a != 1 && b != 2 && c != 3; int++;;) {
      |                                                ^
text-adventure.cpp:32:48: error: expected ')' before ';' token
   32 |         for (a != 1 && b != 2 && c != 3; int++;;) {
      |             ~                                  ^
      |                                                )
text-adventure.cpp:32:49: error: expected primary-expression before ')' token
   32 |         for (a != 1 && b != 2 && c != 3; int++;;) {

The extra semi-colon in the conditions is what the terminal told me to do originally.
What am I doing wrong? If you actually read through all this, thanks. It’s my first time posting in a forum so I’m not well versed in proper etiquette.

1 Like

I’d take those extra semicolons out …
the ;; and the int++;; will probably just cause errors when you try to compile it.

Where you declare that loop:

for (a != 1 && b != 2 && c != 3; int++;;) {

think about what should be different each time the loop runs (meaning each iteration)
and think about in what situation do you want the loop to stop running (so that it’s not an infinite loop)

You’ll use code for that instead of int++;;

Or maybe it shouldn’t be a for loop at all … and it should just be an if-statement?

1 Like

The semicolons are used in for loops to separate the three possible items (initialisation, conditional expression, statement), for (;;) becomes for(init; condition; statement). They aren’t just a random piece of syntax to stick and the end of the for loop.

The classic version of this of initialise an integer, test it on each iteration to see if the condition has been met and increment in the statement.
for (int i = 0; i < 10; i++)

It is possible to skip some these items. For example-

int i = 0;
for (; i < 10;) {
    std::cout << i;
    ++i;
}

is perfectly valid (C++ specific rules apply: for loop - cppreference.com) but you may as well write a while loop at that point.

Since you seem to be using a conditional and a statement to increment you’d want one semicolon to indicate the initialisation should be skipped so it would read more like-
for (; a != 1 && b != 2 && c != 3; int++)

The issue then is that int++ is not a valid statement. Perhaps you meant to increment one or more integer variables, it’s not 100% clear but the excess ;; and int++ are both going to confuse the compiler as will random semicolons in if (a == 1 && b != 1;;) for example. The semicolons have a meaning, treat them nicely eh. :stuck_out_tongue_closed_eyes:

Thanks for your reply! I’ll definitely remove those extra semi-colons. Whenever I try to compile what I already had, the error message would give me something along the lines of “expected qualifier before }” and thought it meant I needed to add something. There’s still something wrong with my logic here, so I’ll need to figure out what that is. I do need to use at least 1 for loops, but it might not be necessary here.

Again, thanks for the clarification!

I’m going to try using an if-statement here and seeing if it works. I do need to use at least 1 for loop, so I guess it’s up to me to figure out where. Also, I’m going to read up some more on for loops so I can truly grasp what it is.

Thank you!

The error messages in C++ can be a bit cryptic sometimes. Some days you can interpret what it’s getting at, at other times you’re better off just taking the line number and hoping you can find the issue yourself.

There were three issues I noted: the for loop(s) with extra ;, some if and else if with a similar issue and the int++ which probably should’ve been a variable name. If that’s cleared up and you’d still struggling to get it to compile try commenting out sections with // or /* */ to try and narrow down which bits are causing the issue so at least you know which bit needs editing.

1 Like