Been working hard today! Coded a tile engine and a very poor camera but it will do for now :)
[media]http://www.youtube.com/watch?v=UrnNuTY2GtE[/media]
a C++ number guessing game.
code so far:
[cpp]
#include <iostream>
//Welcome to the jungle, babe.
//numbers - Klownox
int main(){
int a;
int b;
std::cout << "Please enter an integer value: ";
std::cin >> a;
std::cout << "\nThank you, now please get your friend to guess you number: ";
std::cin >> b;
if (b == a){
std::cout << "\nYou have guessed the correct number!";
}else{
std::cout << "\nYou have not guessed the correct number, try again: ";
std::cin >> b;
}
return 0;
}
[/cpp]
[QUOTE=Klownox;16184157]a C++ number guessing game.
code so far:
[cpp]
#include <iostream>
//Welcome to the jungle, babe.
//numbers - Klownox
int main(){
int a;
int b;
std::cout << "Please enter an integer value: ";
std::cin >> a;
std::cout << "\nThank you, now please get your friend to guess you number: ";
std::cin >> b;
if (b == a){
std::cout << "\nYou have guessed the correct number!";
}else{
std::cout << "\nYou have not guessed the correct number, try again: ";
std::cin >> b;
}
return 0;
}
[/cpp][/QUOTE]
I'm new to C++ and programming in general, but (correct me if I'm wrong, please) couldn't you just do this:
[cpp]using namespace std;[/cpp]
at the start of the code, right below #include? Then you could remove all the std:: before you make a cout or a cin statement.
[QUOTE=Benji;16184986]I'm new to C++ and programming in general, but (correct me if I'm wrong, please) couldn't you just do this:
[cpp]using namespace std;[/cpp]
at the start of the code, right below #include? Then you could remove all the std:: before you make a cout or a cin statement.[/QUOTE]
You are correct.
[QUOTE=Benjy355;16185012]You are correct.[/QUOTE]
Very funny
[QUOTE=Benjy355;16185012]You are correct.[/QUOTE]
Alright, cool. Thanks.
[editline]08:09PM[/editline]
I'm playing with that code. I want the person using the program to enter a number between 1-100 and then the program will tell you either "The number is below 50 or above 50." How would I go about doing this?
[cpp]
//Numbers - Klownox
#include <iostream>
using namespace std;
int main()
{
int a;
int b;
cout << "Please enter an integer value between 1 and 100: ";
cin >> a;
cout << "Thank you. Please have someone attempt to guess your number. \n";
cin >> b;
if (a > 50)
{
cout << "The number is greater than 50. \n";
}
if (b == a){
cout << "You have guessed the correct number! \n";
cin.get();
}
else{
cout << "You have not guessed the correct number, try again: \n ";
cin >> b;
}
return 0;
} [/cpp]
Would I go about doing that with an if statement like that?
(Hope you don't mind using your code Klownox. Just trying to learn and get some practice with some code I understand.)
just use
[cpp]
if (a > 50)
cout << "The number is greater than 50. \n";
else
cout << "The number is smaller than 50. \n";
[/cpp]
but what if the number IS 50?? figure that out yourself
less then or equal to..?
[cpp]
//Numbers - Klownox
#include <stdafx.h>
#include <iostream>
using namespace std;
int main()
{
int a;
int b;
cout << "Please enter an integer value between 1 and 100: ";
cin >> a;
{
cout << "Thanks! Please have someone attempt to guess your number";
if (a > 50)
cout << ", which is greater than 50. \n";
else
cout << ", which is less than 50. \n";
cin >> b;
}
if (b == a)
{
cout << "You have guessed the correct number! \n";
}
else
{
cout << "You have not guessed the correct number, try again: \n ";
cin >> b;
}
return 0;
}
[/cpp]
The program closes immediately when you guess the correct number. Would [cpp]cin.get();[/cpp] work to stop this? If so, where do I place this in my code?
[QUOTE=Benji;16185631][cpp]
//Numbers - Klownox
#include <stdafx.h>
#include <iostream>
using namespace std;
int main()
{
int a;
int b;
cout << "Please enter an integer value between 1 and 100: ";
cin >> a;
{
cout << "Thanks! Please have someone attempt to guess your number";
if (a > 50)
cout << ", which is greater than 50. \n";
else
cout << ", which is less than 50. \n";
cin >> b;
}
if (b == a)
{
cout << "You have guessed the correct number! \n";
}
else
{
cout << "You have not guessed the correct number, try again: \n ";
cin >> b;
}
return 0;
}
[/cpp]
The program closes immediately when you guess the correct number. Would [cpp]cin.get();[/cpp] work to stop this? If so, where do I place this in my code?[/QUOTE]
at the start
[QUOTE=danharibo;16185649]at the start[/QUOTE]
Not working. Can you be more specific?
[QUOTE=Benji;16185708]Not working. Can you be more specific?[/QUOTE]
You want it to pause the program after it's done, but before it returns? Where do you think it would go
Awesome - I've just finished the multi-player version, now to base the buggy single-player off of this!
[cpp]
#include <iostream>
//Welcome to the jungle, babe.
//numbers - Klownox
int main(){
Top:
int a;
int b;
std::cout << "\nPlease enter an integer value: ";
std::cin >> a;
if (a > 100){
goto Top;
}else{
std::cout << "\nThank you, now please get your friend to guess you number: ";
std::cin >> b;
Check:
if (b == a){
std::cout << "\nYou have guessed the correct number! Play again!";
goto Top;
}else{
std::cout << "\nYou have not guessed the correct number, try again: ";
std::cin >> b;
goto Check;
}
return 0;
}
}
[/cpp]
[url]http://xkcd.com/292/[/url]
Tis true, you know?
Read up on loops, goto indicates a flaw in your design; you should never use that.
Also, you should say that the maximal integer-value should be 100.
[editline]2:47AM[/editline]
Also, try to indent your code right, meaning one tab (or an amount of spaces) when a new scope begins, and one less when it ends.
[QUOTE=Klownox;16185738]Awesome - I've just finished the multi-player version, now to base the buggy single-player off of this!
[/QUOTE]
is.. is.. is that a goto?
[QUOTE=Benji;16185631]The program closes immediately when you guess the correct number. Would [cpp]cin.get();[/cpp] work to stop this? If so, where do I place this in my code?[/QUOTE]
std::cin.get() waits for input (thus pausing the program). So you would put it where you want it to wait.
[quote=danharibo;16185767]is.. Is.. Is that a goto?[/quote]
ruuuuuuuuuuuun the raptors are coming!
[QUOTE=danharibo;16185737]You want it to pause the program after it's done, but before it returns? Where do you think it would go[/QUOTE]
I understand what you're saying, but it's not working. Show me in the actual code I posted.
[cpp]
//Numbers - Klownox
#include <stdafx.h>
#include <iostream>
using namespace std;
int main()
{
int a;
int b;
cout << "Please enter an integer value between 1 and 100: ";
cin >> a;
{
cout << "Thanks! Please have someone attempt to guess your number";
if (a > 50)
cout << ", which is greater than 50. \n";
else
cout << ", which is less than 50. \n";
cin >> b;
}
if (b == a)
{
cout << "You have guessed the correct number! \n";
}
else
{
cout << "You have not guessed the correct number, try again: \n ";
cin >> b;
}
cin.get();
return 0;
}
[/cpp]
You won't learn well with copy & paste.
Get a decent book or at least an internet tutorial.
You might wanna look [url=http://www.facepunch.com/showthread.php?t=700269]here[/url].
What is so wrong about using [b]goto[/b]? It works, so I see no reason not to use it.
[QUOTE=Klownox;16185936]What is so wrong about using [b]goto[/b]? It works, so I see no reason not to use it.[/QUOTE]
You'll be mauled by raptors
[QUOTE=danharibo;16185890][cpp]
//Numbers - Klownox
#include <stdafx.h>
#include <iostream>
using namespace std;
int main()
{
int a;
int b;
cout << "Please enter an integer value between 1 and 100: ";
cin >> a;
{
cout << "Thanks! Please have someone attempt to guess your number";
if (a > 50)
cout << ", which is greater than 50. \n";
else
cout << ", which is less than 50. \n";
cin >> b;
}
if (b == a)
{
cout << "You have guessed the correct number! \n";
}
else
{
cout << "You have not guessed the correct number, try again: \n ";
cin >> b;
}
cin.get();
return 0;
}
[/cpp][/QUOTE]
[b]Exactly[/b] what I was doing. Program still closes as soon as you enter the correct number.
[editline]08:58PM[/editline]
[QUOTE=ZeekyHBomb;16185917]You won't learn well with copy & paste.
Get a decent book or at least an internet tutorial.
You might wanna look [url=http://www.facepunch.com/showthread.php?t=700269]here[/url].[/QUOTE]
I'm watching through the C++ Tutorial video series that someone posted. And I have the C++ Primer Plus, Third Edition by Stephen Prata.
Its probably because theres still the new line character left in the input buffer when you enter an integer. Easiest solution is to put two cin.get()s. (Awaits elitists to whip out the huge and ugly code to clear the buffer of any characters left in it)
Okay, I'm using [b]while[/b], how do I use two in one program?
[cpp]
#include <iostream>
//Welcome to the jungle, babe.
//numbers - Klownox
int main(){
while(true){
int a;
int b;
std::cout << "\nPlease enter a number from 0-100: ";
std::cin >> a;
if (a > 100){
continue;
}else{
std::cout << "\nThank you, now please get your friend to guess you number: ";
std::cin >> b;
while(1 == 1){
if (b == a){
std::cout << "\nYou have guessed the correct number! Play again!\n";
continue;
}else{
std::cout << "\nYou have not guessed the correct number, try again: ";
std::cin >> b;
continue;
}
return 0;
}
}
}
}
[/cpp]
After you get it right, it says this: "Please enter a number from 0-100:" infinitely.
[QUOTE=Klownox;16186028]Okay, I'm using [b]while[/b], how do I use two in one program?
[cpp]
#include <iostream>
//Welcome to the jungle, babe.
//numbers - Klownox
int main(){
while(true){
int a;
int b;
std::cout << "\nPlease enter a number from 0-100: ";
std::cin >> a;
if (a > 100){
continue;
}else{
std::cout << "\nThank you, now please get your friend to guess you number: ";
std::cin >> b;
while(1 == 1){
if (b == a){
std::cout << "\nYou have guessed the correct number! Play again!\n";
continue;
}else{
std::cout << "\nYou have not guessed the correct number, try again: ";
std::cin >> b;
continue;
}
return 0;
}
}
}
}
[/cpp][/QUOTE]
You need to start formatting your code correctly and it will be far easier to see the logic. Do it like this:
[code]
if( )
{
note the tab
while( )
{
another nested loop etc etc
}
}
else
{
}[/code]
Alright. I used to do a lot of Python, so I'm used to [b]}else{[/b]
[QUOTE=r4nk_;16186023](Awaits elitists to whip out the huge and ugly code to clear the buffer of any characters left in it)[/QUOTE]
The [b]proper[/b] way of doing it is:
[code]
std::cin.ignore(std::cin.rdbuf()->in_avail() + 1);
[/code]
[QUOTE=r4nk_;16186023]Its probably because theres still the new line character left in the input buffer when you enter an integer. Easiest solution is to put two cin.get()s. (Awaits elitists to whip out the huge and ugly code to clear the buffer of any characters left in it)[/QUOTE]
Perfect! Thanks!
[QUOTE=r4nk_;16186068]You need to start formatting your code correctly and it will be far easier to see the logic. Do it like this:
[code]
if( )
{
note the tab
while( )
{
another nested loop etc etc
}
}
else
{
}[/code][/QUOTE]
except don't use hungarian, use K&R bracketing:
[code]
if () {
four column space indent; more portable and attractive to the eye
while () {
four more spaces
}
}
else {
indent here too
}[/code]
Sorry, you need to Log In to post a reply to this thread.