| View previous topic :: View next topic |
| Author |
Message |
guernica Grandmaster Cheater Supreme
Reputation: 0
Joined: 20 Jun 2007 Posts: 1211
|
Posted: Tue Jan 29, 2008 11:37 pm Post subject: [C++]Random crashes |
|
|
| Code: | //this is a simple game where the user guesses a number in the range of 1-100
#include <iostream>
#include <windows.h>
using namespace std;
int main()
{
srand( GetTickCount() );
//allocation of vars
int guessednumber = 0;
int guessamount = 0;
int randnumber = rand()%100;
//introduction
cout << "Welcome to the Guessing Game." << endl << "The Rules are simple, I will think of a number from 1 to 100." << endl << "You will try to guess it." << endl;
cout << "----------------------------------------------";
cout << endl;
//first guess
guessamount = guessamount + 1;
cout << endl << "Guess #" << guessamount << ": ";
cin >> guessednumber;
//if guess is to low
while (guessednumber < randnumber)
{
cout << "Incorrect guess, please guess higher." << endl;
cout << "----------------------------------------------";
cout << endl;
guessamount = guessamount + 1;
cout << endl << "Guess #" << guessamount << ": ";
cin >> guessednumber;
}
//if guess is too high
while (guessednumber > randnumber)
{
cout << "Incorrect guess, please guess lower." << endl;
cout << "----------------------------------------------";
cout << endl;
guessamount = guessamount + 1;
cout << endl << "Guess #" << guessamount << ": ";
cin >> guessednumber;
}
//if guess is correct
while (guessednumber == randnumber)
{
cout << "Congratulations! You guessed the correct number in " << guessamount << " guesses!";
cout << endl << "Press the enter key to exit.";
cin.ignore();
cin.get();
return 0;
}
} |
i wrote this program for experimentation with srand but sometimes when i run it, it just closes randomly. mind pointing out my flaws?
EDIT: i figured out how to regenerate the error. enter a guess that is too high, then enter one that is too low(or vise versa), and the program closes. thought i knew what i was doing with this one... help?
_________________
| georgezilka wrote: | | im looking for experience (EXP) hacks for maple story are there any where it can give instant level up ? |
|
|
| Back to top |
|
 |
mer0x Advanced Cheater
Reputation: 0
Joined: 06 Jan 2008 Posts: 63
|
Posted: Tue Jan 29, 2008 11:51 pm Post subject: |
|
|
Maybe use if() instead of while???
| Code: |
#include <windows.h>
#include<iostream>
using namespace std;
int main(int argc, char* argv[])
{
srand( GetTickCount() );
//allocation of vars
int guessednumber = 0;
int guessamount = 0;
int randnumber = rand()%100;
//introduction
cout << "Welcome to the Guessing Game." << endl << "The Rules are simple, I will think of a number from 1 to 100." << endl << "You will try to guess it." << endl;
cout << "----------------------------------------------";
cout << endl;
//first guess
guessamount = guessamount + 1;
cout << endl << "Guess #" << guessamount << ": ";
cin >> guessednumber;
//if guess is to low
if (guessednumber < randnumber)
{
cout << "Incorrect guess, please guess higher." << endl;
cout << "----------------------------------------------";
cout << endl;
guessamount = guessamount + 1;
cout << endl << "Guess #" << guessamount << ": ";
cin >> guessednumber;
}
//if guess is too high
if (guessednumber > randnumber)
{
cout << "Incorrect guess, please guess lower." << endl;
cout << "----------------------------------------------";
cout << endl;
guessamount = guessamount + 1;
cout << endl << "Guess #" << guessamount << ": ";
cin >> guessednumber;
}
//if guess is correct
if (guessednumber == randnumber)
{
cout << "Congratulations! You guessed the correct number in " << guessamount << " guesses!";
cout << endl << "Press the enter key to exit.";
cin.ignore();
cin.get();
return 0;
}
}
|
Last edited by mer0x on Tue Jan 29, 2008 11:55 pm; edited 1 time in total |
|
| Back to top |
|
 |
guernica Grandmaster Cheater Supreme
Reputation: 0
Joined: 20 Jun 2007 Posts: 1211
|
Posted: Tue Jan 29, 2008 11:54 pm Post subject: |
|
|
still crashes
_________________
| georgezilka wrote: | | im looking for experience (EXP) hacks for maple story are there any where it can give instant level up ? |
|
|
| Back to top |
|
 |
mer0x Advanced Cheater
Reputation: 0
Joined: 06 Jan 2008 Posts: 63
|
Posted: Tue Jan 29, 2008 11:56 pm Post subject: |
|
|
| Code: |
#include <windows.h>
#include<iostream>
using namespace std;
int main(int argc, char* argv[])
{
srand( GetTickCount() );
//allocation of vars
int guessednumber = 0;
int guessamount = 0;
int randnumber = rand()%100;
//introduction
cout << "Welcome to the Guessing Game." << endl << "The Rules are simple, I will think of a number from 1 to 100." << endl << "You will try to guess it." << endl;
cout << "----------------------------------------------";
cout << endl;
//first guess
guessamount = guessamount + 1;
cout << endl << "Guess #" << guessamount << ": ";
cin >> guessednumber;
//if guess is to low
if (guessednumber < randnumber)
{
cout << "Incorrect guess, please guess higher." << endl;
cout << "----------------------------------------------";
cout << endl;
guessamount = guessamount + 1;
cout << endl << "Guess #" << guessamount << ": ";
cin >> guessednumber;
}
//if guess is too high
if (guessednumber > randnumber)
{
cout << "Incorrect guess, please guess lower." << endl;
cout << "----------------------------------------------";
cout << endl;
guessamount = guessamount + 1;
cout << endl << "Guess #" << guessamount << ": ";
cin >> guessednumber;
}
//if guess is correct
if (guessednumber == randnumber)
{
cout << "Congratulations! You guessed the correct number in " << guessamount << " guesses!";
cout << endl << "Press the enter key to exit.";
cin.ignore();
cin.get();
return 0;
}
}
|
Well check it out. If it doesn't work then its probley your system or something since it works perfectly on mines.
|
|
| Back to top |
|
 |
guernica Grandmaster Cheater Supreme
Reputation: 0
Joined: 20 Jun 2007 Posts: 1211
|
Posted: Wed Jan 30, 2008 12:02 am Post subject: |
|
|
did you even try compiling it and running it? its buggy as hell! try guessing the same number twice in a row... CRASH! why would it be a problem with my system anyway?
_________________
| georgezilka wrote: | | im looking for experience (EXP) hacks for maple story are there any where it can give instant level up ? |
|
|
| Back to top |
|
 |
Losplagos Expert Cheater
Reputation: 0
Joined: 21 Mar 2006 Posts: 172 Location: Knee deep in a c++ book
|
Posted: Wed Jan 30, 2008 9:47 am Post subject: |
|
|
| Code: | //this is a simple game where the user guesses a number in the range of 1-100
#include <iostream>
#include <windows.h>
using std::cout;
using std::cin;
using std::endl;
int main()
{
srand( GetTickCount() );
//allocation of vars
bool loop = true;
int guessednumber = 0, guessamount = 0, randnumber = rand()%10, choice, i;
//introduction
cout << "Welcome to the Guessing Game." << endl;
cout << "----------------------------------------------" << endl
<< "I will think of a number from 1 to 10." << endl;
cout << "----------------------------------------------";
while(loop == true)
{
//first guess
guessamount = guessamount + 1;
cout << endl << "Enter a guess or -1 to quit ";
cin >> i;
switch(i)
{
case -1:
cout << "WHAT A CRUEL WORLD!!!";
loop = false;
break;
default:
guessednumber = i;
while(guessednumber != randnumber)
{
cout << "WRONG!!\n Take another try!: ";
cin >> guessednumber;
cout << endl;
}
break;
}
cout << "Good job!!!\n";
}
} |
Here it is with no input checking very bare. I will add another when i am to the input section of my header file.
_________________
Earthbound = 31337 |
|
| Back to top |
|
 |
hcavolsdsadgadsg I'm a spammer
Reputation: 26
Joined: 11 Jun 2007 Posts: 5801
|
Posted: Wed Jan 30, 2008 9:58 am Post subject: |
|
|
| I hear rumors that there is a new thing called debugging.
|
|
| Back to top |
|
 |
Losplagos Expert Cheater
Reputation: 0
Joined: 21 Mar 2006 Posts: 172 Location: Knee deep in a c++ book
|
Posted: Wed Jan 30, 2008 10:39 am Post subject: |
|
|
Edit: Still some bugs sorry i will post up the source again.
_________________
Earthbound = 31337 |
|
| Back to top |
|
 |
killersamurai Expert Cheater
Reputation: 0
Joined: 10 Sep 2007 Posts: 197 Location: Colorado
|
Posted: Wed Jan 30, 2008 11:10 am Post subject: |
|
|
Your main problem is that your logic is off. If the first condition is not met it will go to the second condition. Say that was met, but then out of no where the user enters another number lower than the generated number. You can't go back to the first condition and you will pass the third condition because it wasn't met. In the end, the program will terminate.
I modified your code a lot. To the point that only a few things that were yours is there. It also introduces pointers and functions. It was not necessary, but you're going to learn it anyways. Might as well see some of the ways you can use pointers. As for the functions, it is easier to make changes if needed and also reduces repetition a lot. Also, rand() % 100 will not give you the range of 1 - 100. It is the range of 0 - 99. You have to shift it by one. 1 + rand() % 100 gives you the range of 1 - 100.
| Code: |
#include <cstdlib>
#include <ctime>
#include <iostream>
int guessamount = 0;
void Guess(int*);
void ShowLine();
void a(short);
int main(int argc, char* argv[])
{
srand(time(0));
int guessednumber = 0;
void (*ptrGuess)(int*) = Guess;
void (*ptrA)(short) = a;
int randnumber = 1 + rand()%100;
bool keepGoing = true;
std::cout << "Welcome to the Guessing Game.\nThe Rules are simple, I will think of a number from 1 to 100.\nYou will try to guess it.\n";
ShowLine();
while (keepGoing)
{
ptrGuess(&guessednumber);
if (guessednumber < randnumber)
ptrA(1);
else if (guessednumber > randnumber)
ptrA(2);
else
{
ptrA(3);
keepGoing = false;
}
}
return 0;
}
void Guess(int* guessedNumber)
{
std::cout << "\nGuess #" << guessamount << ": ";
std::cin >> *guessedNumber;
guessamount++;
}
void ShowLine()
{
std::cout << "----------------------------------------------\n";
}
void a(short index)
{
if (index == 1)
std::cout << "Incorrect guess, please guess higher.\n";
else if (index == 2)
std::cout << "Incorrect guess, please guess lower.\n";
else if (index == 3)
{
std::cout << "Congratulations! You guessed the correct number in ";
std::cout << guessamount << " guesses!\n";
}
}
|
|
|
| Back to top |
|
 |
Losplagos Expert Cheater
Reputation: 0
Joined: 21 Mar 2006 Posts: 172 Location: Knee deep in a c++ book
|
Posted: Wed Jan 30, 2008 12:15 pm Post subject: |
|
|
Here is mine with a header file that you can use for input checking so no excuse now for not checking inputs
| Code: |
//this is a simple game where the user guesses a number in the range of 1-100
#include "rev0"
#define WIN32_LEAN_AND_MEAN // Takes out some rarely used stuff from windows.h
#include <windows.h>
using std::cin;
using std::cout;
using std::endl;
using losbasic::inputmm;
int main()
{
srand( GetTickCount() );
//allocation of vars
bool loop = true;
int guessednumber = 0, guessamount = 0, trys = 1, i;
//introduction
cout << "Welcome to the Guessing Game." << endl;
cout << "----------------------------------------------" << endl
<< "I will think of a number from 1 to 100." << endl;
cout << "----------------------------------------------";
while(loop == true)
{
int randnumber = 1 + rand()%100;
//first guess
cout << endl << "Enter a guess or -1 to quit: ";
i = inputmm<int>(-2,101);
switch(i)
{
case -1:
cout << "WHAT A CRUEL WORLD!!!";
loop = false;
break;
default:
guessednumber = i;
while (guessednumber != randnumber)
{
cout << "WRONG!!\n Take another try!: ";
guessednumber = inputmm<int>(-1,101);
cout << endl;
trys++;
}
break;
}
cout << "Took you " << trys << " Attempts to find the number";
}
} |
| Code: |
#include <iostream>
namespace losbasic
{
template <typename T>
T input()
{
T returnvalue;
std::cin >> returnvalue;
std::cin.clear();
while (std::cin.fail())
{
std::cout << "Bad input Try again: ";
std::cin.clear(); // Clear failbit
std::cin.sync(); // Sync the stream
std::cin >> returnvalue; // Try again
}
}
template <typename T>
T inputmm(T min, T max) //Always use this before a switch No execeptions
{
T returnvalue;
std::cin >> returnvalue;
while(std::cin.fail()||returnvalue < min||returnvalue > max)
{
std::cout << "Bad input Try again: ";
std::cin.clear(); // Clear failbit
std::cin.sync(); // Sync the stream
std::cin >> returnvalue; // Try again
}
}
template <typename T>
T inputmn(T min)
{
T returnvalue;
std::cin >> returnvalue;
while(std::cin.fail()||returnvalue < min)
{
std::cout << "Bad input Try again: ";
std::cin.clear(); // Clear failbit
std::cin.sync(); // Sync the stream
std::cin >> returnvalue; // Try again
}
}
template <typename T>
T inputm(T max)
{
T returnvalue;
std::cin >> returnvalue;
while(std::cin.fail()||returnvalue > max)
{
std::cout << "Bad input Try again: ";
std::cin.clear(); // Clear failbit
std::cin.sync(); // Sync the stream
std::cin >> returnvalue; // Try again
}
}
}
|
Ok now let me explain the functions in the header.
losbasic:: function <Value type> ()
So this will say
losbasic::inputmm<int>(0,11)
int inputmm (int,int)
There are these functions in the header file.
| Code: |
input<type>()
inputmm<type>(min,max)
inputmn<type>(min)
inputm<type>(max)
|
input<type>() Gets input and checks if the input is invalid.
inputmm<type>(min, max) Gets input checks if input is invalid or under the min or over the max.
inputmn<type>(min) Gets input checks if it is under the min
inputm<type>(max) Gets input and checks if it is over the max.
_________________
Earthbound = 31337 |
|
| Back to top |
|
 |
guernica Grandmaster Cheater Supreme
Reputation: 0
Joined: 20 Jun 2007 Posts: 1211
|
Posted: Wed Jan 30, 2008 5:52 pm Post subject: |
|
|
ill abandon this project for a while. i dont understand lots of your guys codes. ill go back and relearn some stuff before i continue.
_________________
| georgezilka wrote: | | im looking for experience (EXP) hacks for maple story are there any where it can give instant level up ? |
|
|
| Back to top |
|
 |
Losplagos Expert Cheater
Reputation: 0
Joined: 21 Mar 2006 Posts: 172 Location: Knee deep in a c++ book
|
Posted: Wed Jan 30, 2008 9:26 pm Post subject: |
|
|
I forgot a few lines of code in that header file too im sorry.
| Code: |
#include <iostream>
namespace losbasic
{
template <typename T>
T input()
{
T returnvalue;
std::cin >> returnvalue;
while (std::cin.fail())
{
std::cout << "Bad input Try again: ";
std::cin.clear(); // Clear failbit
std::cin.sync(); // Sync the stream
std::cin >> returnvalue; // Try again
}
return returnvalue;
}
template <typename T>
T inputmm(T min, T max) //Always use this before a switch No execeptions
{
T returnvalue;
std::cin >> returnvalue;
while(std::cin.fail()||returnvalue < min||returnvalue > max)
{
std::cout << "Bad input Try again: ";
std::cin.clear(); // Clear failbit
std::cin.sync(); // Sync the stream
std::cin >> returnvalue; // Try again
}
return returnvalue;
}
template <typename T>
T inputmn(T min)
{
T returnvalue;
std::cin >> returnvalue;
while(std::cin.fail()||returnvalue < min)
{
std::cout << "Bad input Try again: ";
std::cin.clear(); // Clear failbit
std::cin.sync(); // Sync the stream
std::cin >> returnvalue; // Try again
}
return returnvalue;
}
template <typename T>
T inputm(T max)
{
T returnvalue;
std::cin >> returnvalue;
while(std::cin.fail()||returnvalue > max)
{
std::cout << "Bad input Try again: ";
std::cin.clear(); // Clear failbit
std::cin.sync(); // Sync the stream
std::cin >> returnvalue; // Try again
}
return returnvalue;
}
}
|
[/code]
_________________
Earthbound = 31337 |
|
| Back to top |
|
 |
|