I've been working on a little word scrambler game for my class and I had the job of adding words and definitions from a external file and including a changable leaderboard from an external file. I had a structure holding score and a player's name. I would input the first ten scores form the leaderboard.txt and compare them to the 11th score (current player). However, it seems I cannot compare them to resave the top ten. I posted my code, everything works except in the switch(input) case(y or Y) if(strikes >= 3) section. I cannot exchange or compare values of the scoreArray[i].score.
Any solutions or pointers in the right direction for this issue would be really appreciated!
// Lab_10.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
#include <strstream>
#include <ctime>
using namespace std;
string scramble(string, int&, string&); //scrambles the word
int randomSelection(int); //random selection of a number to use in the array
const int maxLength = 200;
struct word // words and the whole line of text from dictionary.txt
{
string line;
string wrd;
};
struct leaderboard // yet to finish
{
string line;
string playerName;
int score;
};
int _tmain(int argc, _TCHAR* argv[])
{
int randNumb; // random integer
word wordArray[maxLength];
leaderboard scoreArray[11];
string word1; // actual word
string scrambled; // scrambled word
string guess; // input to compare
int vowels = 0;
char input = 'Z';
int test;
scoreArray[10].score = 0;
cout << "Welcome To WORD SCRAMBLER!" << endl;
cout << "Would you like to play?" << endl;
cout << "Enter [Y] to play or [N] to exit: ";
cin >> input;
ifstream infile; // streams in txt
infile.open("dictionary.txt");
if(infile.fail())
{
cout << "cannot find file"; // if the file should fail
return -99;
}
while(!infile.eof())
{
for(int i = 0; i <= 50; i++) // gets fifty lines from file and saves it to the array
{
getline(infile, wordArray[i].line);
}
}
infile.close();
for(int i = 0; i <= 40; i++) // seperates the word from the line
{
int pos;
pos = wordArray[i].line.find('|');
string temp = wordArray[i].line.substr(0,pos);
wordArray[i].wrd = temp;
}
/*for(int i = 0; i <= 40; i++) This wouldve saved the definition to the wordArray[i].def but errors were occuring
{
int pos;
pos = wordArray[i].line.find('|');
wordArray[i].def = wordArray[i].line.substr(pos+2, wordArray[i].line.length());
} */
while(input != 'n' && input != 'N')
{ cout << "\n************************************New Round***********************************" << endl;
scoreArray[10].score = 0;
int strike = 0; // stub
cout << "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\nPress Enter to Continue!" << endl;
cin.get();
cin.get();
while(input != 'n' && input != 'N' && strike < 3)
{
scrambled.clear();
randNumb = randomSelection(35);
vowels = 0;
scramble(wordArray[randNumb].wrd, vowels, scrambled);
cout << "\n\n\n\n\n\n\n\n\n\n";
success = false;
int tries = 0;
while (success == false && tries < 5)
{
tries++;
cout << "********************************************************************************" << endl;
cout << "| Let's play WORD SCRAMBLER! |" << endl;;
cout << "********************************************************************************" << endl;
cout << "TRY NUMBER: " << tries << endl;
cout << "\n\nNumber of vowels removed from word: " << vowels << endl;
cout << "\nThe scrambled word is: " << scrambled << endl;
int pos = wordArray[randNumb].line.find('|');
string hint = wordArray[randNumb].line.substr(pos+2,wordArray[randNumb].line.length());
//^ This saves the line without the word and "|" to a string
cout << "\nDefinition: " << hint << endl;// hint is display
cout << "\n\n\n\n\n";
cout << "Please guess the word: ";
cin >> guess;
cout << "You guessed: " << guess << endl;
if (guess.compare(wordArray[randNumb].wrd) == 0)
{
success = true;
}
else
{
cout << "\nSorry, that is incorrect. Try again!" << endl << endl;
}
}
if (success == true)
{
cout << "\n\nCongrats! You've guessed the word in " << tries << " attempts!";
scoreArray[10].score += 5 - (tries - 1);
cout << "\nThe word was: "<< wordArray[randNumb].wrd <<"\n\n\n\n\n\n\n\n\nCurrent Score: " << scoreArray[10].score;
cout << "\n\n";
}
else
{
cout << "\nSorry! You did not guess the word in 5 tries. The word was: " << wordArray[randNumb].wrd;
cout << "\n\n\n\n\n\n\n\nCurrent score: "<< scoreArray[10].score << "\n\n\n\n";
strike++;
}
cout << "\nYou have " << strike << " strike(s).\n\n\n\n\n\n\n\n\n\n";
cout << "\nWould you like to play again? ([y]es/[n]o)";
cin >> input;
switch(input)
{
case 'Y':
case 'y':
if(strike >= 3)
{
{
cout << "\n\n\n\n\n\n\n\n\n\n\n\n\n*****************************************************************************" << endl;
cout << "You have chosen to play again.\nHowever, you have three strikes and must begin a new round!\n\nYou're score for this round was: " << scoreArray[10].score << endl;
cout << "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\nPlease Enter Your Name for The Leaderboards: ";
cin >> scoreArray[10].playerName;
ifstream inLeaderBoard;
inLeaderBoard.open("Leaderboard.txt");
while(!inLeaderBoard.eof())
{
for(int i = 0; i < 10; i++)
{
inLeaderBoard >> scoreArray[i].score;
inLeaderBoard >> scoreArray[i].playerName;
}
inLeaderBoard.close();
}
for(int i = 0; i < 10; i++)
{
scoreArray[i].score = (int)scoreArray[i].score;
}
bool ordered = false;
int n = 0, k = 0, y = 0, x = 0;
while(ordered == false)
{
for(int i = 0; i < 11; i++)
{
if(scoreArray[i].score > scoreArray[i+1].score)
x = scoreArray[i+1].score;
y = scoreArray[i].score;
scoreArray[i].score = x;
scoreArray[i+1].score = y;
k++;
}
if(k == 0)
ordered == true;
else
k = 0;
}
}
cout << "The score for this round was submitted for the leaderboards" << endl;
cout << "Press enter to Continue\n";
cin.get();
cin.get();
}
// TOOK OUT SOME CODE HERE TO KEEP THE POST SOMEWHAT SHORT
EXIT_SUCCESS;
}
string scramble(string word1, int& vowels, string& scrambled)
{
for(int i = 0; i < word1.length(); i++)
switch(word1.at(i))
{
case 'A':
case 'a':
case 'e':
case 'E':
case 'I':
case 'i':
case 'O':
case 'o':
case 'U':
case 'u': vowels = vowels+1;
break;
default:
if (bool x = rand() % 2)
{
scrambled = scrambled+word1.at(i);
}
else
{
scrambled = word1.at(i)+scrambled;
}
}
return scrambled;
}
int randomSelection(int num)
{
int random;
time_t t;
time(&t);
srand(t);
random = rand() % 35;
return random;
}

New Topic/Question
Reply



MultiQuote




|