C++ noob needs help!

Thanatos

Active Member
I'm just learning the basics of C++ and programming in general. I need a little help on a 99 bottles of beer on the wall program. Here is what i have so far:
PHP:
#include <iostream>
#include <Windows.h>
#include <stdio.h>
using namespace std;

int main()
{
	int bottles = 99;


		while (bottles > 1);
		{
			cout << bottles << " bottles of beer on the wall!" << endl;			
			cout << bottles << " bottles of beer!" << endl;	
			cout << "take one down, pass it around," << endl;	
			cout << endl;
			bottles - 1;
			Sleep (750);
			system ("CLS");
		}

		cout << bottles << " bottle of beer on the wall!" << endl;			
		cout << bottles << " bottle of beer!" << endl;	
		cout << "take it down, pass it around," << endl;
		cout << "Zero bottles of beer on the wall!" << endl;
		cout << endl;
		Sleep (750);
		system ("CLS");


	return 0;
}

And it isn't working! I get the prompt to come up but it displays nothing. Sorry, this is probably a really easy fix, but i don't know it. Maybe a 'for' loop? :confused:
 

Troncoso

VIP Member
Well I wasn't sure if the sleep() argument was in milliseconds or seconds. I looked it up and everyone is saying seconds. So each iteration will be waiting 12.5 minutes to continue. Also, apparently you a dos.h header file to even use the sleep function. I don't remember that but it's been a while since I've used c or c++. That one I won't say for sure but you can try it.

Now, on the chance that the sleep function does in fact use milliseconds than the reason you can see anything is because 750 ms is less than 1 second so it's going to fast for you to see it.

To verify that, after the while loop, you have that second block of statements. Take out the "system("cls")" and instead use something like cin.get() to pause the program til you hit a key. There is no reason to clear the screen if that is the last bit of output. That way you can see if something is showing up, which it should. The rest of your code looks fine.

Also use the cin.get() function in your while loop instead of the system("clr") function. That way, each time it runs through an iteration of the loop, it'll stop til you hit a key.
 
Last edited:

kobaj

VIP Member
Not sure if this is causing your problem, but a few issues I noticed.

No semi-colons after a while statement. it should be
Code:
        while (bottles > 1)
        {

You decrement bottles, but never set it back to the variable. It should be.
Code:
bottles -= 1;

And generally its bad practice to use system ("CLS"); but thats an argument for another day.
 

Troncoso

VIP Member
Not sure if this is causing your problem, but a few issues I noticed.

No semi-colons after a while statement. it should be
Code:
        while (bottles > 1)
        {

.

Wow, I'm an idiot. Haha. I didn't notice that. That is definitely a problem. Fix that first then refer back to what I said if you still have issues.
 

Thanatos

Active Member
OHHH! yep, getting rid of that semicolon fixed it. i changed bottles-1; to --bottles; and it still works. thanks guys. now wanna see my blackjack program? :D

PHP:
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <Windows.h>
using namespace std;

int main()
{
	int p1;
	int p2;
	int p3;
	int p4;
	int p5;

	int d1;
	int d2;
	int d3;
	int d4;
	int d5;

	char hit;

	srand( time(NULL) );

	p1 = rand() % 11+1;
	p2 = rand() % 11+1;
	p3 = rand() % 11+1;
	p4 = rand() % 11+1;
	p5 = rand() % 11+1;

	d1 = rand() % 11+1;
	d2 = rand() % 11+1;
	d3 = rand() % 11+1;
	d4 = rand() % 11+1;
	d5 = rand() % 11+1;

	
	
	cout << "Lets play black jack!" << endl;

	if (d1 + d2 == 21)
	{
		cout << "The dealer got black jack before you! you lose!" << endl;
	}
	if (d1 + d2 > 21)
	{
		cout << "The dealer went over 21! You win!" << endl;
	}
	else
	{
		cout << "Here are your two cards" << endl;

		cout << p1 << endl;
		cout << p2 << endl;

		if (p1 + p2 == 21 )
		{ 
			cout << "You got Black Jack! you win!" << endl;
		}
		if (p1 + p2 > 21)
		{
			cout << "Too much! you lose! You got " << (p1 + p2) << endl;
		}

		else
		{
			cout << "Want to get another card? y/n" << endl;
			if (d1 + d2 + d3 == 21)
			{
				cout << "The dealer just got 21 before you! You lose!" << endl;
			}
			if (d1 + d2 + d3 > 21)
			{
				cout << "The dealer went over 21! You win!" << endl;
			}
			cin >> hit;
				if (hit == 'y')
				{
						cout << p3 << endl;
						if (p1 + p2 + p3 == 21)
						{ 
							 cout << "You got Black Jack! you win!" << endl;
						}
						if (p1 + p2 + p3 > 21)
						{
							cout << "Too much! you lose! You got " << (p1 + p2 + p3) << endl;
						}
				}
		}
	}

				
	 
	return 0;
}

I know its still VERY sketchy, what do you guys think?
 

ScottALot

Active Member
Hey Green, how'd you get started on C++? I'd love to learn that language, but I can't find any book that seems to be the "C++ Bible" like I've heard about other subjects.
 

ScottALot

Active Member
That's not online, I'm assuming? I wish I could've known about that earlier this summer, I would've tried to go.
 

Stretch1414

New Member
I am taking a C++ programming class at my high school, I got it pushed through as Independent Study, and our technology coordinator is helping us out a little. It's pretty cool, especially since I noticed the semicolon in the while statement line, I have to admit I accidentally did that myself one time. And I was freaking out because my program would not work right, then my friend came over and *clicked behind semicolon* *hit backspace key* and then laughed his ass off.
 

Darren

Moderator
Staff member
Yeah I'm currently learning C++ in my public high school. Had to learn Visual Basic first though.

Even if you don't aren't going to use it later any type of computer programming is good to know just so you get the gist of how stuff works in a computer.
 

Thanatos

Active Member
definitely. I wish i had classes like that in my high school. I think we have 150 kids total. In the whole high school. 16 kids in 9th grade. suffice to say, we are underfunded and have like 0 electives.
 
Top