Not declared in this scope error, anyone can help?

It says int i is not declared in this scope (do while loop) and I can't figure out how it can't be as I'm declaring it in the while condition, can anyone help me? Thanks!
( please keep in mind I can't use any other statements or anything unless it's in this code as I haven't learned it and I'm trying to build this with what if learned so far, this is a test for chapter 5: loops in JumpingintoC++ (I also tryd with bool but seems to make no difference) )

This is the full code, the part that's giving the "i was not declared in this scope" is in the do while loop.

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
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>
#include <string>
using namespace std;

int main()
{
    int a;
    cout << "Press enter to start the 99 bottles song.\nPress enter everytime to continue the song. (or hold enter)\n";
    cin.ignore();
    for (a = 99; a > 0; -a) // Counts down until 0
    {
    cout << "" << a << " bottle(s) of beer on the wall," " " << a << " bottle(s) of beer.\n";
    cout << "Take one down, pass it around," " " << --a << " bottle(s) of beer on the wall.\n";
    cin.ignore ();
    if (a < 1)
    {
        cin.ignore();
        cout << "I hope you sang along!\n";
        cin.ignore ();
        break;
    }
    }
    do
    {
    int i;
    string stop;
    stop = "quit";
    cout << "What would you like to do? Type the option number to iniate it. (ex. 1)\nType quit to stop the program\n";
    cout << "Option 1: Login\nOption 2: Math\nOption3: Bottlesong ";
    cin >> i;
    }while (i != 1 || != 2 || != 3);
    if (i == 1)
    {

    }
    else if (i == 2)
    {

    }
    else if (i == 3)
    {

    }
}

Last edited on
You're getting the errors on the following statement:
1
2
 
    } while (i != 1 || != 2 || != 3);

i is only defined within the braces of your do-while loop. Your conditionals are outside the braces and therefore out of scope

edit: Also your conditionals are defective. Your loop should look like this:
1
2
3
4
5
6
7
8
9
10
 
    int i;
    do
    {   string stop;
        
        stop = "quit";
        cout << "What would you like to do? Type the option number to iniate it. (ex. 1)\nType quit to stop the program\n";
        cout << "Option 1: Login\nOption 2: Math\nOption3: Bottlesong ";
        cin >> i;
    } while (i < 1 || i > 3);

Note that i has been moved outside the loop and that i is included in each term of the while condition.

PLEASE YOU CODE TAGS (the <> formatting button) when posting code.
Last edited on
It seems I've fixed it so far by putting int i next to my a ontop, and changing the while code to:
}while (!(i=1) || !(i=2) || !(i=3));

I'm going to test if it works now, atleast it compiles now, is this the correct way?

UPDATE: It compiles now, but I know ! equals not, so how could I make it work so it loops aslong as int i is not 1,2 or 3?
Last edited on
Your while loop isn't going to work as you expect it to.
 
(i=1)

is an assignment statement, not a comparison.
The result of that will always be true.
Thanks for the replies, I've fixed it myself by doing this:
while ((i!=1) && (i!=2) && (i!=3));
but you're right Abstraction, you're statement makes more sense and is logical and short, thanks for the advice!
It seems to be working now, but the problem I'm having now is that because I'm using integers and using while (i < 1 || i > 3) the moment I type characters it goes into rage-loop and it won't stop unless I stop it trough my Windows task manager, how could I fix this?
A classic issue with cin >> intval;
Two alternatives:
1) Use getline with a string and test the string is numeric, then convert it to an integer.
2) Use a char value for your input, then test against the char value.
1
2
3
4
5
6
7
8
char choice;
  cin >> choice;
  switch (choice)
  { case '1': // do choice 1
                 break;
     //  other cases
     default: cout << Not a valid input' << endl;
   } 

Note the use of the character value in the cases.



Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
    int a;
    string i;
    do
    {
    cout << "What would you like to do? Type the option number to iniate it. (ex. 1)\n";
    cout << "Option 1: Login\nOption 2: Math\nOption 3: 99 Bottles song\nStart option: ";
    getline (cin, i);
    }while (i !="1" || i!="2" || i!="3");        // ((i!=1) && (i!=2) && (i!=3)) same thing, longer
    if (i == "1")
    {

    }
    else if (i == "2")
    {

    }
    else if (i == "3")

it works this way, and it doesnt crash/rageloop when pressing chars, but now it won't accept any numeric inputs, even tryed "3"
The problem is your while condition again.
Your loop is going to repeat while any of the conditions is true.
if i is "1", then (i!="2") and (i!="3") will both be true, therefore your loop will repeat.

Use && instead of ||
 
while (i !="1" && i!="2" && i!="3");        

or the while condition I showed you earlier (using quoted values).
 
while (i < "1" || i > "3");
Thanks, that helped! I dind't use and because I thought it checks && before anything else, or atleast I read it that way. Thanks!
Topic archived. No new replies allowed.