May 22, 2017 at 3:08pm UTC
Hello, I am wondering if anyone could help me out on what the error is for the following program, I feel stupid asking because it seems simple. This is some of the code for my EOC final project. Thanks!
#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <stdlib.h>
#include <time.h>
#include <windows.h>
#include <cstdlib>
#include <cctype>
#include <ctime>
#include <algorithm>
using namespace std;
double choice, cardTotal, dealerTotal,hitstay;
int card1,card2,card3,card4;
void successLoginMenu(int choice);
void HangMan();
void Blackjack(double hitstay);
void MissileCommand();
void dealThirdCard();
void dealFourthCard();
void playerDealer(double dealerTotal);
void youBust();
void youWin();
void youLose();
int main(){
successLoginMenu(choice);
return 0;
void successLoginMenu(int choice)
{
cout << "Welcome To The Main Menu Of The Program! Please Choose From The Following\n\n- Blackjack[1]\n- Hangman[2]\n- Missile Command[3]\n- Exit" << endl;
cin >> choice;
if (choice == 1)
{
cout << "Please wait while we transfer you to the game....";
Sleep(2000);
cout << string (50, '\n');
Blackjack(hitstay);
}
if (choice == 2)
{
cout << "Please wait while we transfer you to the game....";
Sleep(2000);
cout << string (50, '\n');
HangMan();
}
if (choice == 3)
{
cout << "Please wait while we transfer you to the game....";
Sleep(2000);
cout << string (50, '\n');
MissileCommand();
}
if (choice == 4)
{
exit(0);
}
}
void Blackjack(double hitstay){
card1 = rand() % 10 + 1;
card2 = rand() % 10 + 1;
cout << "Welcome To BlackJack!\n";
cout << "Card 1: " << card1 << endl;
cout << "Card 2: " << card2 << endl;
cardTotal = card1 + card2;
cin.ignore();
cout << "Card Total: " << cardTotal;
cout << "\nWould you like to 'Hit[1]' or 'Stay[2]'" << endl;
cin >> hitstay;
if (hitstay == 1)
{
dealThirdCard();
}
if (hitstay == 2)
{
playerDealer(dealerTotal);
}
}
void dealThirdCard(){
srand(time(0));
card3 = rand() % 10 + 1;
cout << "\nCard 1: " << card1 << endl;
cout << "Card 2: " << card2 << endl;
cout << "Card 3: " << card3 << endl;
cardTotal = card1 + card2 + card3;
cin.ignore();
cout << "Card Total: " << cardTotal;
cout << "\nWould you like to 'Hit[1]' or 'Stay[2]' " << endl;
cin >> hitstay;
if (hitstay == 1)
{
dealFourthCard();
}
if (hitstay == 2)
{
playerDealer(dealerTotal);
}
}
void dealFourthCard(){
srand(time(0));
card4 = rand() % 11 + 1;
dealerTotal = rand() % 29 + 10;
cout << "\nCard 1: " << card1 << endl;
cout << "Card 2: " << card2 << endl;
cout << "Card 3: " << card3 << endl;
cout << "Card 4: " << card4 << endl;
cardTotal = card1 + card2 + card3 + card4;
cin.ignore();
cout << "Card Total: " << cardTotal << endl;
cout << "Dealer Total: " << dealerTotal;
Sleep(3000);
if (cardTotal > 21 && cardTotal > dealerTotal);
{
youBust();
}
if (cardTotal < 21 && cardTotal < dealerTotal && dealerTotal < 21);
{
youLose();
}
if (cardTotal < 21 && cardTotal > dealerTotal);
{
youWin();
}
}
void playerDealer(double dealerTotal){
srand(time(0));
dealerTotal = rand() % 11 + 1;
cout << "Dealer Total: " << dealerTotal;
if (cardTotal < dealerTotal)
{
youLose();
}
if (cardTotal > dealerTotal && cardTotal < 21)
{
youWin();
}
}
void youBust(){
Sleep(2000);
cout << endl << "\nYou Busted, \2\n1 - Play Again\2\n2 - Return To Lobby\n";
cin >> choice;
if (choice == 1)
{
cout << string (50, '\n');
Sleep(2000);
Blackjack(hitstay);
}
if (choice == 2)
{
cout << string (50, '\n');
main();
}
}
void youLose(){
Sleep(2000);
cout << endl << "\nYou lost! \2\n1 - Play Again\2\n2 - Return To Lobby\2";
cin >> choice;
if (choice == 1)
{
cout << string (50, '\n');
Sleep(2000);
Blackjack(hitstay);
}
if (choice == 2)
{
cout << string (50, '\n');
main();
}
}
void youWin(){
Sleep(2000);
cout << endl << "\nYou Won! \2\n1 - Play Again\2\n2 - Return To Lobby\2";
cin >> choice;
if (choice == 1)
{
cout << string (50, '\n');
Sleep(2000);
Blackjack(hitstay);
}
if (choice == 2)
{
cout << string (50, '\n');
main();
}
}
void HangMan(){
const int MAX_WRONG = 5;
vector<string> words;
words.push_back ("COMPUTER");
words.push_back("SCIENCE");
words.push_back("ANATOMY");
words.push_back("ARBOR");
words.push_back("ENGLISH");
words.push_back("WIFI");
words.push_back("CRYPT");
words.push_back("FIXABLE");
words.push_back("DELUXE");
words.push_back("BUFFON");
words.push_back("BUFFALO");
words.push_back("AWKWARD");
words.push_back("CRUSH");
words.push_back("FIZZY");
words.push_back("DRINK");
words.push_back("DUI");
words.push_back("PARTNERS");
words.push_back("PLACENTA");
words.push_back("EMBRYO");
words.push_back("DINOSAUR");
words.push_back("GROCERIES");
words.push_back("POTATO");
words.push_back("TOMATO");
words.push_back("CHIPS");
words.push_back("FRIES");
words.push_back("COBWEB");
words.push_back("GOSSIP");
words.push_back("EXODUS");
words.push_back("IVY");
words.push_back("IVORY");
words.push_back("HYPHEN");
words.push_back("MATRIX");
words.push_back("ENCRYPTED");
words.push_back("HACKER");
words.push_back("TROJANHORSE");
words.push_back("VIRUS");
words.push_back("TEACHER");
words.push_back("INSTRUCTOR");
words.push_back("USA");
words.push_back("UK");
words.push_back("CHINA");
words.push_back("EUROPE");
words.push_back("GERMANY");
words.push_back("CHOCOLATE");
words.push_back("MARSHMALLOW");
words.push_back("FRIENDS");
words.push_back("ENEMIES");
words.push_back("BAD");
words.push_back("GOOD");
srand(static_cast<unsigned int>(time(0)));
random_shuffle(words.begin(), words.end());
const string WORD_GUESS = words[0];
string soFar(WORD_GUESS.size(), '-');
string usedLetters = "";
int wrong = 0;
cout << "THIS IS HANGMAN!\2\n\n";
while ((wrong < MAX_WRONG) && (soFar != WORD_GUESS))
{
cout << "\n\nRemaining Guesses: " << (MAX_WRONG - wrong) << "\n";
cout << "\nYou have used this letter " << usedLetters << endl;
cout << "\nThe word so far looks like this: " << soFar << endl;
}
char guess;
cout << "\n\nEnter your guess: ";
cin >> guess;
guess = toupper(guess);
while (usedLetters.find(guess) != string::npos)
{
cout << "\nYou have already guess: " << guess << endl;
cout << "Enter your: ";
cin >> guess;
guess = toupper(guess);
}
usedLetters += guess;
if (WORD_GUESS.find(guess) != string::npos)
{
cout << guess << " is in the word\n";
for (int i = 0; i < WORD_GUESS.length(), i++)
{
if (WORD_GUESS[1] == guess){
soFar[i] = guess;
}
}
else
{
cout << guess << " is not in the word\n";
++wrong;
}
}
if (wrong == MAX_WRONG)
{
cout << "Game Over! You’ve Been Hanged\n";
}else{
cout << "The word was: " << WORD_GUESS << endl;
cout << "\n\n";
}
return EXIT_SUCCESS;
}
void MissileCommand(){
}
Last edited on May 22, 2017 at 3:09pm UTC
May 23, 2017 at 3:40pm UTC
#include <iostream>
#include <cctype>
#include <ctime>
#include <algorithm>
#include <string>
#include <iomanip>
#include <cstdlib>
#include <vector>
#include <windows.h>
using namespace std;
int main(){
const int MAX_WRONG = 5;
vector<string> words;
words.push_back("COMPUTER");
words.push_back("SCIENCE");
words.push_back("ANATOMY");
words.push_back("ARBOR");
words.push_back("ENGLISH");
words.push_back("WIFI");
words.push_back("CRYPT");
words.push_back("FIXABLE");
words.push_back("DELUXE");
words.push_back("BUFFON");
words.push_back("BUFFALO");
words.push_back("AWKWARD");
words.push_back("CRUSH");
words.push_back("FIZZY");
words.push_back("DRINK");
words.push_back("DUI");
words.push_back("PARTNERS");
words.push_back("PLACENTA");
words.push_back("EMBRYO");
words.push_back("DINOSAUR");
words.push_back("GROCERIES");
words.push_back("POTATO");
words.push_back("TOMATO");
words.push_back("CHIPS");
words.push_back("FRIES");
words.push_back("COBWEB");
words.push_back("GOSSIP");
words.push_back("EXODUS");
words.push_back("IVY");
words.push_back("IVORY");
words.push_back("HYPHEN");
words.push_back("MATRIX");
words.push_back("ENCRYPTED");
words.push_back("HACKER");
words.push_back("TROJANHORSE");
words.push_back("VIRUS");
words.push_back("TEACHER");
words.push_back("INSTRUCTOR");
words.push_back("USA");
words.push_back("UK");
words.push_back("CHINA");
words.push_back("EUROPE");
words.push_back("GERMANY");
words.push_back("CHOCOLATE");
words.push_back("MARSHMALLOW");
words.push_back("FRIENDS");
words.push_back("ENEMIES");
words.push_back("BAD");
words.push_back("GOOD");
srand(static_cast<unsigned int>(time(0)));
random_shuffle(words.begin(), words.end());
const string WORD_GUESS = words[0];
string soFar(WORD_GUESS.size(), '-');
string usedLetters = "";
int wrong = 0;
cout << "THIS IS HANGMAN!\2\n\n";
char guess;
cout << "\n\nEnter your guess: ";
cin >> guess;
guess = toupper(guess);
while ((wrong < MAX_WRONG) && (soFar != WORD_GUESS))
{
cout << "\n\nRemaining Guesses: " << (MAX_WRONG - wrong) << "\n";
cout << "\nYou have used this letter " << usedLetters << endl;
cout << "\nThe word so far looks like this: " << soFar << endl;
}
while (usedLetters.find(guess) != string::npos)
{
cout << "\nYou have already guess: " << guess << endl;
cout << "Enter your: ";
cin >> guess;
guess = toupper(guess);
}
usedLetters += guess;
if (WORD_GUESS.find(guess) != string::npos)
{
cout << guess << " is in the word\n";
for (int i = 0; i < WORD_GUESS.length(); i++)
{
if (WORD_GUESS[i] == guess)
{
soFar[i] = guess;
}
else
{
cout << guess << " is not in the word\n";
++wrong;
}
}
}
if (wrong == MAX_WRONG)
{
cout << "Game Over! You’ve Been Hanged\n";
}
else
{
cout << "The word was: " << WORD_GUESS << endl;
cout << "\n\n";
}
Sleep(5000);
main();
}
Last edited on May 23, 2017 at 3:41pm UTC
May 25, 2017 at 5:33pm UTC
I think it's pretty hard guessing what the errors is. you should specify it.. Looks pretty good to me anyway..
If you need any help detecting bugs you can try using some programs as help. There are amny, such as checkmarx or others.
Good luck!
Ben.
May 25, 2017 at 8:39pm UTC
You have been asked to use code tags. PLEASE DO SO.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.
If you're not going to make the slightest bit of effort to make your posts readable, why should we spend the slightest bit of effort helping you?
You've ignored
wildblue 's advice not to call main() recursively. Not only is it prohibited by the C++ standard, but the way you are doing it is guaranteed to cause a stack overflow eventually. You have no other exit from main(), other than to call main() recursively. Your program will never exit.
Line 95: The type of i should be size_t to prevent a compiler warning due to a type mismatch.
Last edited on May 25, 2017 at 8:39pm UTC