Hey guys, I have an assignment due soon, and I cant figure out how to read my text files into structs and then have them displayed onto the screen. The code compiles but when I try to display the cards or players it prints out a bunch of crazy squares with numbers in them. I have no clue on what to do.
/*-----------------------------------------------------------------------------------------*\
FILE NAME: project3.cpp
DESCRIPTION: A program that reads an array of characters from a file and shuffles them
EXIT CODES: None
COMPILER: Ubuntu text editor
Author Date
------------ ------------
Brian Dudkowski 09/10/14
\*----------------------------------------------------------------------------------------*/
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cstring>
usingnamespace std;
struct card{
char rank[5];
char suit[7];
int cvalue;
char location[4];
};
struct player{
int total;
char name[100];
};
void choice_one();
void choice_three();
void get_menu();
int main()
{
int choice;
do{
get_menu();//Displays the menu
cout << "Enter you choice: ";// Could not get this to work in the menu function
cin >> choice;
cout << endl << endl;
}while (choice == 1 && choice == 2 && choice == 3 && choice == 4);
switch (choice)
{
case 1:
{
choice_one();
break;
}
case 2:
{
break;
}
case 3:
{
choice_three();
break;
}
case 4:
{
cout << "Goodbye!\n\n";
break;
}
}
return 0;
}
/* -----------------------------------------------------------------------------
FUNCTION: void get_menu()
DESCRIPTION: Displays the menu
RETURNS: No return for void
NOTES: Could not figure out how to include the user input for choice
----------------------------------------------------------------------------- */
void get_menu(){
cout << endl << endl;
cout << "Choose an option\n"
<< "-------------------\n"
<< "1. Print unshuffled deck to screen\n"
<< "2. Print shuffled deck to the screen\n"
<< "3. Print players\n"
<< "4. Quit\n\n";
}
/* -----------------------------------------------------------------------------
FUNCTION: void choice_one()
DESCRIPTION: Reads the unshuffled deck and prints it on the screen
RETURNS: No return for void
NOTES: None
----------------------------------------------------------------------------- */
void choice_one()
{
fstream fin;
fin.open("unshuffled.txt");
card unshuff[52];
card *cptr;
cptr = unshuff;
for (int i = 0; i < 4; i++)
{
fin >> (*cptr).rank >> (*cptr).suit;
cout << (*cptr).rank << (*cptr).rank << endl;
}
fin.close();
cout << endl << endl;
}
void choice_three()
{
fstream fin;
fin.open("players.txt");
player pname[4];
player *nptr;
nptr = pname;
for (int i = 0; i < 4; i++)
{
fin >> (*nptr).name;
cout << (*nptr).name << endl;
}
fin.close();
cout << endl << endl;
}
Basically there are two text files; one has all of the cards in the deck (two heart, three heart, four heart...), the other text file has 4 player name(first name last name...). I have to read those two files into the arrays using pointers and display them to the screen.