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.
#include <iostream>
#include <string>
usingnamespace 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)
{
}
elseif (i == 2)
{
}
elseif (i == 3)
{
}
}
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.
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;
}
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")
{
}
elseif (i == "2")
{
}
elseif (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).