class initialization in C++ - c++

I tried to initialize an array of a class then use a loop to change the data members in each of the objects. I'm not sure how to get the values to stick, because after I changed the values, I tried to print a random object out and it's just the default values and not the changed values. Any help would be appreciated, Thanks!
#include <iostream>
#include <string>
#include <time.h>
using namespace std;
class card {
private:
int rank;
int suit;
////////////////////////////////////////////////////////////
public:
/////////////////////////////////////////////////////////////
// default constructor with initialization list
card(int userRank = (2,3,4,5,6) , int userSuit=15)
:rank(userRank), suit(userSuit){}
///////////////////////////////////////////////////////////
// function to validate user's rank choice.
int cardcheckRank(int pRank){
while(pRank<2 || pRank>14)
{
cout << "Choose a playing card rank between 2-14, where 11=Jack, "
"12=Queen, 13=King, 14=Ace"<<endl;
cin >> pRank;
}
return pRank;
}
/////////////////////////////////////////////////////////////
// function to validate user's suit choice.
int cardcheckSuit(int pSuit){
while(pSuit<15 || pSuit>18)
{
cout << "Choose a playing card suit "
"between 15-18, where 15=Diamond, 16=Club, 17=Heart, 18=Spades.";
cin >> pSuit;
}
return pSuit;
}
//////////////////////////////////////////////////////////////////
// functioin to get a card value from user.
void storeCard(int pRank, int pSuit){
card(cardcheckRank(pRank),cardcheckSuit(pSuit) );
}
/////////////////////////////////////////////////////////////////
// translates
string faceRank(int translateRank){
switch (translateRank) {
case 2:
return "Two";
case 3:
return "Three";
case 4:
return "Four";
case 5:
return "Five";
case 6:
return "Six";
case 7:
return "Seven";
case 8:
return "Eight";
case 9:
return "Nine";
case 10:
return "Ten";
case 11:
return "Jack";
case 12:
return "Queen";
case 13:
return "King";
case 14:
return "Ace";
default: return "Invalid";
}
}
///////////////////////////////////////////////////////////
// translate integer suit value into a word.
string faceSuit(int translateSuit){
switch (translateSuit){
case 15:
return "Diamonds";
break;
case 16:
return "Clubs";
break;
case 17:
return "Hearts";
break;
case 18:
return "Spades";
break;
default: return "Invalid";
}
}
///////////////////////////////////////////////////////
// Function to print the current card.
void printCard(){
cout << "The rank of the card is ";
cout << faceRank(rank);
cout << " and the suit is " << faceSuit(suit) << "." << endl;
}
}; // End of card class.
//////////////////////////////////////////////////////
// main function.
int main()
{
srand (time(NULL)); // initialize random seed.
card deck[52];
char choice = 'n';
int h = 0; // card number.
for(int i = 15; i < 19; i++)
{
int y = i;
for(int j =2; j < 15; j++)
{
int z = j;
(deck [h]).storeCard(z,y);
cout << "Card rank " << (deck [h]).faceRank(z);
cout << ", suit " << (deck [h]).faceSuit(y) <<endl;
++h;
}
}
do{
cout <<"Would user like to play?(y/n)"<<endl;
cin >> choice;
int ranNum = (rand()% 51 + 0);
if(choice == 'y')
{
cout << "User: ";
deck[7].printCard();
}
}
while(choice == 'y');
return 0;
}

Values are not sticking because your storeCard function doesn't really store the card, it creates a new card object and throws it away. If you really want to express this by invoking the card constructor, then assign the constructed object to the current one:
void storeCard(int pRank, int pSuit){
*this = card(cardcheckRank(pRank),cardcheckSuit(pSuit) );
}
A more idiomatic approach would be for storeCard to directly modify the object's attributes, much like the constructor does:
void storeCard(int pRank, int pSuit){
rank = cardcheckRank(pRank);
suit = cardcheckSuit(pSuit);
}

Related

ASCII Strength Game will not calculate "Bot" word value

I'm making a game that tests the ASCII strength of a user versus a bot. (There is also a 2 player mode but that's working fine.) The full description is given at the top of my .cpp file. As a basic breakdown, the bot opens a txt file with 500 common four letter words and inserts them into a size 500 array. It then randomly generates a number to pick a random one, and then goes through the process of tovalue() to recieve its ASCII value, where in tovalue() runs through chartoint() four times, one for each character of the word. My issue is that the program calculates the ASCII value perfectly fine of the user generated word, but always returns 0 (0000) for the botword, no matter what the word.
I've tried a few iterations of the generateword() function including using a vector but always get the same resutls. I've done a lot of digging about this and haven't quite found any solutions, although I suspect that the chartoint() function could be better optimized, just not sure how to impliment any better solutions for this specific case. Also, don't think the problem is with chartoint() since it works fine for user input, but I'm pretty sure the problem is with generateword(). Suggestions for making chartoint() would be helpful, but its not my main priority right now since I just need the program to 100% work first. Also, I've confirmed that all of the words in my .txt file are all caps and only four characters per line.
// Write the code for a game called “ASCII Strength” of a four-letter word selected by Player 1
// followed by a four-letter word selected by Player 2. The result would be the sum
//of the ASCII value of each of the letters of the selected words and whoever has higher sum (called ASCII strength) wins.
#include <iostream>
#include <vector>
#include <fstream>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;;
int chartoint(char a) {
switch (a) {
case 'A':
return 1;
break;
case 'B':
return 2;
break;
case 'C':
return 3;
break;
case 'D':
return 4;
break;
case 'E':
return 5;
break;
case 'F':
return 6;
break;
case 'G':
return 7;
break;
case 'H':
return 8;
break;
case 'I':
return 9;
break;
case 'J':
return 10;
break;
case 'K':
return 11;
break;
case 'L':
return 12;
break;
case 'M':
return 13;
break;
case 'N':
return 14;
break;
case 'O':
return 15;
break;
case 'P':
return 16;
break;
case 'Q':
return 17;
break;
case 'R':
return 18;
break;
case 'S':
return 19;
break;
case 'T':
return 20;
break;
case 'U':
return 21;
break;
case 'V':
return 22;
break;
case 'W':
return 23;
break;
case 'X':
return 24;
break;
case 'Y':
return 25;
break;
case 'Z':
return 26;
break;
}
return 0;
}
int tovalue(string input) {
int first = chartoint(input[0]);
int second = chartoint(input[1]);
int third = chartoint(input[2]);
int fourth = chartoint(input[3]);
cout << first << second << third << fourth; // EXISTS TO TEST CALCULATION
int value = first + second + third + fourth;
return value;
}
string generateword() {
string arr[500];
ifstream file("words.txt");
if (file.is_open())
{
for (int i = 0; i < 500; i++) {
string temp;
getline(file, temp);
arr[i] = temp;
}
file.close();
}
else
{
cout << "Error: Unable to open file.";
exit(0);
}
srand(time(0));
int random_index = rand() % 500;
string random_word = arr[random_index];
return random_word;
}
int main()
{
cout << "Welcome to ASCII strength, a game where the strongest word wins!";
cout << "\nTo play, you must enter a four letter word. The program will calculate the 'ASCII strength' of your word and compare it to your opponent.";
cout << "\nWhoever has the higher sum will win!";
char another;
another = 'y';
while (another == 'y' || another == 'Y') {
cout << "\nWould you like to play against a friend, or against a bot? (F/B)";
char mode;
cin >> mode;
if (mode == 'F' || mode == 'f') {
cout << "\nPlayer 1, please input your four letter word in all caps: ";
string answer1;
cin >> answer1;
int value1;
value1 = tovalue(answer1);
cout << "\nPlayer 2, please input your four letter word in all caps: ";
string answer2;
cin >> answer2;
int value2;
value2 = tovalue(answer2);
if (value1 > value2) {
cout << "\nPlayer 1 wins!";
}
else if (value2 > value1) {
cout << "\nPlayer 2 wins!";
}
else if (value1 == value2) {
cout << "\nTie!";
}
}
else if (mode == 'B' || mode == 'b') {
cout << "\nPlease input your four letter word in all caps: ";
string answer;
cin >> answer;
int valueanswer;
valueanswer = tovalue(answer);
string botword;
botword = generateword();
cout << "\nThe bot generates a random word based on a list of popular four letter words.";
cout << "\nThe bot has generated this word: " << botword;
int valuebot;
valuebot = tovalue("botword");
cout << valueanswer << " " << valuebot; // THIS EXISTS PURELY TO TEST WHETHER THE VALUES ARE PROPERLY CALCULATING
if (valueanswer > valuebot) {
cout << "\nYou win!";
}
else if (valuebot > valueanswer) {
cout << "\nThe bot wins!";
}
else if (valueanswer == valuebot) {
cout << "\nTie!";
}
}
cout << "\nWould you like to start a new game? (y/n)";
cin >> another;
}
}
Your problem is this line:
valuebot = tovalue("botword");
Since all characters in "botword" are lowercase, you get all 0 score. You probably meant to write
valuebot = tovalue(botword);

I want the user to input the day number as the value of the parameter of the called function using cin

So, as you see in the question I want to make the user input the value of the arguement daynum down when I call the function getday, and not me who enters it. However I can't seem to get it right. I have tried cin << getday(); but it's wrong I looked in the internet to get an idea I guess and I tried getday(cin); but still it's wrong kinda clueless here about how to use cin with the function call.
#include <iostream>
using namespace std;
string getday(int daynum){
string dayname;
switch(daynum){
case 0:
dayname = "sunday";
break;
case 1:
dayname = "Monday";
break;
case 2:
dayname = "Tuesday";
break;
default:
dayname = "invalid day number";
}
return dayname;
}
int main()
{
cout << "Enter daynum" << endl;
return 0;
}
You have to create a temporary variable, fill it with user value using cin, and then pass it to your function:
string getday(int daynum)
{
string dayname;
switch (daynum)
{
case 0:
dayname = "sunday";
break;
case 1:
dayname = "Monday";
break;
case 2:
dayname = "Tuesday";
break;
default:
dayname = "invalid day number";
}
return dayname;
}
int main()
{
cout << "Enter daynum" << endl;
int daynum;
cin >> daynum;
cout << getday(daynum) << endl;
return 0;
}
Also you can avoid the switch statement using an array or other data structure. It will be more cleaner and easier to mantain.
Example:
#include <iostream>
#include <array>
#include <string>
int main()
{
std::array<std::string, 7> names = {"Monday", "Tuesday" , "Wendesday" , "Thursday", "Friday", "Saturday", "Sunday"};
unsigned int n = 0;
std::cin >> n;
if(n < names.size()) {
std::cout << names[n] << std::endl;
}
else
{
std::cout << "Invalid day number" << std::endl;
}
}
You need to add some value in daynum by using cin inside main() or getday.
#include <iostream>
using namespace std;
string getday(int daynum) {
string dayname;
switch(daynum) {
case 0:
dayname = "sunday";
break;
case 1:
dayname = "Monday";
break;
case 2:
dayname = "Tuesday";
break;
default:
dayname = "invalid day number";
}
return dayname;
}
int main() {
int num ; // Here i create variable num --> to pass argument to getday function
cout << "Enter daynum" << endl;
cin>>num;
cout<<getday(num);
return 0;
}

C++ pull a card from deck

I'm trying to create a program that will pull a card from a deck of 52 regular playing cards.
Suits: Heart, Spad, Diamond, Club.
Rank: A,2,3,4,5,6,7,8,9,10,J,Q,K.
This should be the output:
Let's pull a card!
This time we got AH
Wanna pull a card again?
y
This time we got 3J
Wanna pull a card again?
n
My output is:
Let's pull a card!
DKThis time we got 00
Wanna pull a card again?
n
This is my code:
#include <iostream>
#include <ctime>
using namespace std;
// Function Declaration
int rankCard(), suitCard();
int main()
{
srand(time(0));
char answer;
cout << "Let's pull a card!" << endl;
do {
cout << "This time we got " << rankCard() << suitCard() << endl;
cout << "Wanna pull a card again?" << endl;
cin >> answer;
} while ((answer == 'y') || (answer == 'Y'));
return 0;
}
int rankCard() {
int rank = (rand() % 13) + 1;
switch (rank) {
case 1: cout << "A";
break;
case 10: cout << "T";
break;
case 11: cout << "J";
break;
case 12: cout << "Q";
break;
case 13: cout << "K";
break;
default: cout << rank;
break;
}
return 0;
}
int suitCard() {
int suit = (rand() % 4) + 1;
switch (suit) {
case 1: cout << "H";
break;
case 2: cout << "D";
break;
case 3: cout << "C";
break;
case 4: cout << "S";
break;
}
return 0;
}
I can't figure out why the cards pulled (DK) are in that position and why I also get the 00. What am I doing wrong? Thanks
Your calls to rankCard() and suitCard() always return 0.
That 0 value is what's passed to cout in your main function.
The weird 'DK' is caused by the calls to cout inside rankCard and suitCard.
You could modify your functions to avoid the confusion:
#include <string>
std::string rankCard() {
int rank = (rand() % 13) + 1;
switch (rank) {
case 1: return "A";
case 10: return "T";
case 11: return "J";
case 12: return "Q";
case 13: return "K";
default: return std::to_string( rank );
}
return "";
}
std::string suitCard() {
int suit = (rand() % 4) + 1;
switch (suit) {
case 1: return "H";
case 2: return "D";
case 3: return "C";
case 4: return "S";
}
return "";
}
This line:
cout << "This time we got " << rankCard() << suitCard() << endl;
So those functions print the card, and then they return 0, so if you call them in cout, they will do their thing witch is printing the card and then print the return value witch is 0.
What you can do is to call them outside the cout, just do:
//...
cout << "This time we got ";
rankCard();
suitCard();
cout << endl;
cout << "Wanna pull a card again?" << endl;
//...
Personally I would refactor the functions to return the respective card char:
Live sample
const char rankCard() {
int rank = (rand() % 13) + 1;
switch (rank) {
case 1: return 'A';
case 10: return 'T';
case 11: return 'J';
case 12: return 'Q';
case 13: return 'K';
default: return rank + 48; // convert to decimal digit
}
}
const char suitCard() {
int suit = (rand() % 4) + 1;
switch (suit) {
case 1: return 'H';
case 2: return 'D';
case 3: return 'C';
case 4: return 'S';
default: return 0; //ASCII code for null character
}
}

Guessing Game with functions c++

I am writing a guessing game program using functions for each thing, I keep getting errors saying function isn't set so when I try to call it, it isn't working. I can't figure out what I am doing wrong.
I know I have arguments for the functions that aren't being used but I cant seem to figure out where or how I should include those in the function themself.
I am fairly new to programming/c++ so please no negative comments I am just trying to get as much help as I can.
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int getGuess(string prompt);
string getRank(int guessCount);
bool getPlayAgain(string prompt);
void playOneGame();
int main(){
srand(time(0));
int number = rand() % 100 + 1;
string prompt = getGuess();
do(
playOneGame();
)while(getPlayAgain())
return EXIT_SUCCESS;
}
int getGuess(string prompt){
int num;
int guessCount = 0;
prompt = cout << "Please enter a number between 1-100: ";
cin >> num;
if(num > 100){
cout << "Please enter a number between 1-100: " << endl;
}
if(num < 1){
cout << "Please enter a number between 1-100: " << endl;
}
if(num <= 100){
cout << "The number you guessed is: " << num << endl;
guessCount++;
}
}
string getRank(int guessCount){
switch(guessCount){
case 1:
case 2:
case 3: cout << "Lucky!" << endl;
break;
case 4:
case 5:
case 6: cout << "Awesome";
break;
case 7:
case 8:
case 9: cout << "Good";
break;
case 10:
case 11:
case 12: cout << "Meh";
break;
case 13:
case 14:
case 15: cout <<"Poor";
break;
default: cout << "Pathetic";
}
}
bool getPlayAgain(string prompt){
bool done = false;
int num1;
while(!done){
cout << "Enter 1 to play again or 2 to quit: ";
cin >> num1;
if(num1 == 2){
break;
}
else(
getGuess();
)
}
}
void playOneGame(){
getGuess();
getRank();
getPlayAgain();
}
No return statement in getguess() function but function signature is int return type.
Getguess() accepts prompt parameter as input but not used inside the function.

c++ : Deck not showing first card

I am actually trying to create a bluff game of cards in c++. When I call the function showAllCards the card at the of the deck ( Ace of Hearts ) does not show on output, the only way to show it is to add both deck structures in main body.And when program runs (without top card), dev generates some warnings too. What am I doing wrong?
Also can you please refer some improvements/changes in program.
Here is the code:
#include <iostream>
#include<Strings.h>
#include<cstdlib>
using namespace std;
struct link{
int data;
link *link;
};
struct deck
{
string suit[32];
string value[32];
}card;
struct cardsDeck{
string nxt[20];
string suitlist[4] = {"hearts","spades","clubs","diamonds"};
string vals[8] = {"ace","two","three","four","five","jack","queen","king"};
}cdeck;
class Game
{
private:
link *first;
public:
Game();
void check();
void mask();
void pass();
void enterCard();
void showAllCards();
void shuffle();
void rearrangeCards();
};
Game::Game(){
first=NULL;
}
void Game::rearrangeCards(){
short int x = 0, y = 0, z = 0;
while(x < 32)
{
card.suit[x] = cdeck.suitlist[y];
card.value[x] = cdeck.vals[z];
++x;
++z;
if(x % 8 == 0)
{
++y;
}
if(z % 8 == 0)
{
z = 0;
}
}
}
void Game::showAllCards(){
int x;
while(x < 32)
{
if(card.suit[x]!=card.suit[x-1]){
cout<<"\n";
}
cout << card.value[x] << " of " << card.suit[x] << "\n";
++x;
}
}
int main(){
Game game;
int op;
game.rearrangeCards();
cout<<"Welcome to Bluff Mashter 20/12/15::6:46\nEnter the Operation please:\n\n";
cin>>op;
while(op!=0){
switch(op){
case 1:
game.showAllCards();
break;
case 2:
break;
case 3:
break;
case 4:
break;
case 5:
break;
case 6:
break;
case 7:
break;
case 8:
break;
}
}
return 1;
}
and here are the warnings
20 63 E:\Projjects\mainbluuf.cpp [Warning] non-static data member initializers only available with -std=c++11 or -std=gnu++11
21 78 E:\Projjects\mainbluuf.cpp [Warning] non-static data member initializers only available with -std=c++11 or -std=gnu++11
20 63 E:\Projjects\mainbluuf.cpp [Warning] extended initializer lists only available with -std=c++11 or -std=gnu++11
21 78 E:\Projjects\mainbluuf.cpp [Warning] extended initializer lists only available with -std=c++11 or -std=gnu++11
assuming your method is this:
void Game::showAllCards(){
int x = 0;
while(x < 32)
{
if(card.suit[x]!=card.suit[x-1]){
cout<<"\n";
}
cout << card.value[x] << " of " << card.suit[x] << "\n";
++x;
}
}
in your first iteration you're doing this: card.suit[0]!=card.suit[-1] which is wrong.
void Game::showAllCards(){
int x = 1;
while(x < 32)
{
if(card.suit[x]!=card.suit[x-1]){
cout<<"\n";
}
cout << card.value[x] << " of " << card.suit[x] << "\n";
++x;
}
}
I didn't test the code though, but I guess that fixes the issue.
Note: I set x to 1 that's the only change. Since you're new to C++ by default uninitialized variables can contain any unpredicted values which is stored in that memory address, so the good practice is to always initialize your variables.
Update:
Try this:
void Game::showAllCards(){
for(x = 0; x < 32; ++x)
{
cout << card.value[x] << " of " << card.suit[x] << "\n";
}
}
Update 2:
int main(){
Game game;
int op;
game.rearrangeCards();
cout<<"Welcome to Bluff Mashter 20/12/15::6:46\nEnter the Operation please:\n\n";
cin >> op;
while(op!=0){
switch(op){
case 1:
game.showAllCards();
break;
case 2:
break;
case 3:
break;
case 4:
break;
case 5:
break;
case 6:
break;
case 7:
break;
case 8:
break;
}
cin >> op;
}
return 0;
}
Update 3:
void Game::showAllCards(){
string lastSuit = "";
for(x = 0; x < 32; ++x)
{
if(lastSuit != card.suit[x])
cout << "\n";
cout << card.value[x] << " of " << card.suit[x] << "\n";
lastSuit = card.suit[x];
}
}
After some logical torture and tips from Boynux I figured out both prblems.
for(int j=0;j<32;j++){
cout<<card.value[j] << " of " << card.suit[j]<<"\n";
if(j==7 || j==15 || j==23 || j==31)
{
cout<<"\n";
}
}