I’m taking an intro cs course in c++ at uni and we were learning about structs and pointers, so I’d thought of trying to make tripleX using what I’ve learned instead of directly following the tutorial. I made this function which takes four pointers to the values of the code’s products, sums, and the users guess’s products and sums and compare them.
bool CheckGuess(int *a, int *b, int *c, int *d, bool &complete)
{
bool check = ((*a == *b)&&(*c == *d));
std::cin.clear();
std::cin.ignore();
if (check == complete)
{
std::cout << "Wrong! The hacker is one step closer to collecting your intel.\n";
return false;
}
else
{
std::cout << "You finally did it!\n";
complete = true;
return complete;
}
}
It mostly works except that if the user inputs characters and words, my code prints out the line that results from a wrong guess several times:
There are three numbers in the code.
The digits of the code add up to 22
The digits multiply to 378
f a aaa <--- (my input)
Wrong! The hacker is one step closer to collecting your intel.
Wrong! The hacker is one step closer to collecting your intel.
Wrong! The hacker is one step closer to collecting your intel.
Wrong! The hacker is one step closer to collecting your intel.
Wrong! The hacker is one step closer to collecting your intel.
How can I get this to only print once, no matter what the user inputs?
Edit:
Here is my entire code. I changed the variable names to make it easier to read, but I may have messed something else up because now I get these errors:
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.24.28314\include\istream(484): warning C4530: C++ exception handler used, but unwind semantics are not enabled. Specify /EHsc
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.24.28314\include\istream(478): note: while compiling class template member function 'std::basic_istream<char,std::char_traits<char>> &std::basic_istream<char,std::char_traits<char>>::ignore(std::streamsize,int)'
tripleX.cpp(93): note: see reference to function template instantiation 'std::basic_istream<char,std::char_traits<char>> &std::basic_istream<char,std::char_traits<char>>::ignore(std::streamsize,int)' being compiled
tripleX.cpp(83): note: see reference to class template instantiation 'std::basic_istream<char,std::char_traits<char>>' being compiled
#include <iostream>
struct Passcode
{
int A;
int B;
int C;
};
void PrintIntro(int level);
bool CheckGuess(int *pGuessSum, int *pCodeSum, int *pGuessProd, int *pCodeProd, bool &complete);
bool GameStart();
void StoreGuess(Passcode *GuessDigits, int *pGuessProd, int *pGuessSum);
int main()
{
int LevelDifficulty = 1;
int const MaxDifficulty = 10;
while (LevelDifficulty <= MaxDifficulty)
{
PrintIntro(LevelDifficulty);
GameStart();
++LevelDifficulty;
}
return 0;
}
//Functions
void PrintIntro(int level)
{
if (level == 1)
{
std::cout << "\n\nCrime is on the high. Hacking has never been easier than before.\n";
std::cout << "With numerous hackers aiming to gain access to the secret government database, \n";
std::cout << "You must quickly enter the correct code, or else risk leaking valuable intel: \n";
}
else
{
std::cout << "\n\nBUT WAIT! \n Your computer is now infected with more malware! \n";
std::cout << "This hacker's malware looks tougher than before. Quickly enter the correct code! \n";
}
}
bool GameStart()
{
bool bLevelComplete = false;
//Declare 3 number code
Passcode Code1 = {7 , 6, 9};
//Print sum and product to terminal
int CodeSum = Code1.A + Code1.B + Code1.C;
int CodeProduct = Code1.A * Code1.B * Code1.C;
int *pCS = &CodeSum;
int *pCP = &CodeProduct;
std::cout << "\nThere are three numbers in the code.";
std::cout << "\nThe digits of the code add up to " << CodeSum;
std::cout << "\nThe digits multiply to " << CodeProduct << "\n";
//Create Guess Variables
Passcode Guess;
Passcode *pGuess = &Guess;
int GuessSum, GuessProduct;
int *pGS = &GuessSum;
int *pGP = &GuessProduct;
while (bLevelComplete == false)
{
StoreGuess(pGuess, pGS, pGP);
CheckGuess(pGS, pCS, pGP, pCP, bLevelComplete);
}
return bLevelComplete;
}
void StoreGuess(Passcode *GuessDigits, int *pGuessProd, int *pGuessSum)
{
std::cin >> (*GuessDigits).A >> (*GuessDigits).B >> (*GuessDigits).C;
*pGuessSum = (*GuessDigits).A + (*GuessDigits).B + (*GuessDigits).C;
*pGuessProd = (*GuessDigits).A * (*GuessDigits).B * (*GuessDigits).C;
}
bool CheckGuess(int *pGuessSum, int *pCodeSum, int *pGuessProd, int *pCodeProd, bool &complete)
{
bool check = ((*pGuessSum == *pCodeSum)&&(*pGuessProd == *pCodeProd));
std::cin.clear();
std::cin.ignore();
if (check == complete)
{
std::cout << "Wrong! The hacker is one step closer to collecting your intel.\n";
return false;
}
else
{
std::cout << "You finally did it!\n";
complete = true;
return complete;
}
}
