2017-05-03 19:37:17

Hello,

Visual C++ tells me that there are some undeclared identifiers in eleventh and fourteenths lines of this particular code, which I'm sure they have been declared:

//99 Bottles of Beer

//includes

#include <iostream> //for std::cout and std::endl

int main ()
{
for (int beer = 99; beer == 1; beer--) // The only declared var
std::cout << beer << " bottles of beer on the wall" << std::endl;
std::cout << beer << " bottles of beer" << std::endl; // The first supposedly undeclared variable
std::cout << "Take one down" << std::endl;
std::cout << "Pass it around" << std::endl;
std::cout << beer - 1 << " bottles of beer on the wall" << std::endl << std::endl; // The second one is here
return 0;
}

Thanks for any help in advance.

Standing by the window, eyes upon the moon,
Hoping that the memory will leave her spirit soon.

2017-05-03 20:32:11

Hello,
the reason why c++ considers variable beer as undeclared is, that you:
a. missed { and } in for cycle.
b. Declared variable in cycle that means it can be used only in it's block of code, which makes in this case that one std::cout command, which is not showing error.

Depends on what you want code to do, I readed it only quickly and do not understand it's goal, hovever using variable out of it's range do not require to know goal. So as I sayd, add block marks to include more commands, or declare beer as global or local for main variable.
Hope this helps.
Best regards

Rastislav

2017-05-03 23:17:56

Hi there,

Surrounding the for-loop by bracket signs resolves the error, but the code now does not do anything at all. What I'm trying to achieve is to print all of the lyrics of the song called "99 Bottles of Beer." The program is still far from being complete, but without making it work, I don't think if I can make any more progress.

Standing by the window, eyes upon the moon,
Hoping that the memory will leave her spirit soon.

2017-05-03 23:44:15 (edited by Rastislav Kish 2017-05-03 23:58:46)

Hello again,
yeah, I understand now. Next problem in your code is, that you wrote for cycle with never true condition. Your code:

for (int beer = 99; beer == 1; beer--)

set variable beer to 99 and start cycle. Hovever in process where program checks if cycle can run, condition will return false, because 99 is not equal to 1, so cycle ends and program do nothing. You can easyly fix it by changing condition to beer>=1 or beer>0, in that case it will work, because 99 is greater than 0, 98 too etc. Full code will look as follow:

//99 Bottles of Beer

//includes

#include <iostream>

int main ()
{
for (int beer = 99; beer >= 1; beer--) {
std::cout << beer << " bottles of beer on the wall" << std::endl;
std::cout << beer << " bottles of beer" << std::endl;
std::cout << "Take one down" << std::endl;
std::cout << "Pass it around" << std::endl;
std::cout << beer - 1 << " bottles of beer on the wall" << std::endl << std::endl; //if (beer>1) std::cout << beer - 1 << " bottles of beer on the wall" << std::endl << std::endl; else std::cout<<"No bottles of beer on the wall"<<std::endl; can be also used.
}

return 0; //Is not needed, in C it was, many programmers are using it also in c++ but there is no reason to do it, so if you want to have it you can, but it is not necessary.
}

Best regards

Rastislav

2017-05-03 23:53:10

Hello,

It finally did work... Thank you very much for your great help. wink

Standing by the window, eyes upon the moon,
Hoping that the memory will leave her spirit soon.

2017-05-04 00:04:15

Great, I completed my last post just now, audiogames successfully breaked my lines and deleted half of message, but now it's fixed, hovever it is not important when you have what you need.
Happy coding:

Rastislav

2017-05-04 00:12:40

The broken lines in your message seem to be fixed, and you are an awesome tutor. Once again, thanks for making my day.

Standing by the window, eyes upon the moon,
Hoping that the memory will leave her spirit soon.