Hey guys I am thinking about mastering C and C++ this year. I have now 3 years of experience in C, C++ but I still think I need to learn a ton. What do you guys think, can you recommend me a good book?
I know all the fundamentals, basics, graphics in C basics, data structures, all the OOP concepts, quite much of inbuilt ASM. Just want to know a book where I can master these all.
#include <stdio.h>
#include <stdlib.h>
#include "modifyPerson.h"
int main(int argc, char *argv[])
{
char name[] = "Name";
createPerson (name, 21, 180.0, 23, 2, 1991);
return 0;
}
void createPerson(char *name, int age, float height, int day, int month, int year);
void printPerson();
#include <stdio.h>
#include <stdlib.h>
#include "person.h"
void createPerson(char *name, int age, float height, int day, int month, int year)
{
Person newPerson = (Person) malloc (sizeof (Person));
newPerson.name = name;
newPerson.age = age;
newPerson.height = height;
newPerson.dob.day = day;
newPerson.dob.month = month;
newPerson.dob.year = year;
return newPerson;
}
void printPerson(Person person)
{
puts (person.name);
printf ("%d\n%.2f\n", person.age);
printf ("%d/%d/%d", person.dob.day, person.dob.month, person.dob.year);
}
typedef struct {
int day;
int month;
int year;
} DoB;
typedef struct {
char name[20];
float height;
DoB birthDate;
} Person;
Person createPerson(char *name, int age, float height, int day, int month, int year);
void printPerson();
However, not sure if this is related to the undefined reference errors, but you're returning a value from the createPerson method which you've declared as a void. Also, you're trying to assign a pointer value into the struct - you'll either need to just get rid of the malloc (this seems like what you want to do and you can safely do this unless your assignment requires you to allocate memory dynamically) or change newPerson to a pointer type (declare it as a *newPerson rather than newPerson) and then change all you assignments to dereference the pointer (i.e. newPerson.name = name becomes newPerson->name = name).
One thing though, what if I pass more than 20 characters in as a name?
Well first, functions defined as void do not return a value, but you already fixed that. The other point is the malloc. When you doAlso, could you explain that a little better? I feel this is the source to several of my other errors.
int i;
Person p;
type identifier;
type identifier = malloc(num);
type *identifier = malloc(num);
Did you remember to put in any include guards? All header files are generally wrapped in something likeAnd one last thing, am I including my headers the proper way? I get errors if I include a header in more than one place, but I'm not sure where to include them so they are properly linked.
#ifndef HEADER_NAME_H_
#define HEADER_NAME_H_
/* all header contents go here */
#endif
A lot of Java programmers new to a language like C or C++ often forget that non-primitive types (objects and structs) don't always need to be allocated dynamically using new... seeing someone trying to dynamically allocate memory where unnecessary/wrong is almost dead giveaway of a programmer who started off using Java (or some other language with similar reference semantics, but it's almost always Java) ^^And....yeah, I did. What gave it away?
A lot of Java programmers new to a language like C or C++ often forget that non-primitive types (objects and structs) don't always need to be allocated dynamically using new... seeing someone trying to dynamically allocate memory where unnecessary/wrong is almost dead giveaway of a programmer who started off using Java (or some other language with similar reference semantics, but it's almost always Java) ^^
typedef struct {
void (*func)(instruction);
union {
double db;
char ch;
};
} instruction;
int i;
for (i = 0; i <= instr_count; i++) {
(*instr[i].func) (instr[i]);
}
I'm guessing that it's getting confused on what to do, and hopefully changing the brackets will help. I try not to use structs and function pointers in general
Haha. If that was an option, I'd definitely take that route. Not that C isn't a useful language, but again, given the option, I would not use it at all.
I've been fighting with this off and on all day, I still can't get it to work.
I'm going to keep trying but my god is it annoying.
void func(int instruct) {
printf("Instruction: %d", instruct);
}
typedef struct {
void (* func)();
union {
double db;
char ch;
};
} instruction;
int main() {
instruction *ins = malloc(sizeof(instruction));
ins->db = 2.5;
ins->ch = 'a';
ins->func = &func;
printf("db: %f ch: %c",ins->db, ins->ch);
ins->func(5);
return 0;
}
[B]struct instruction {[/B]
void (*func)(instruction);
union {
double db;
char ch;
};
};
typedef struct {
[I]void (*func)(instruction);[/I]
union {
double db;
char ch;
};
[B]} instruction;[/B]
I do mean your problem but I've got it now. I was thinking about what was wrong instead of working back from the error.
The gist of it is, you've made a pointer to a void function. But you haven't set that pointer equal to anything. This works as expected:
Code:void func(int instruct) { printf("Instruction: %d", instruct); } typedef struct { void (* func)(); union { double db; char ch; }; } instruction; int main() { instruction *ins = malloc(sizeof(instruction)); ins->db = 2.5; ins->ch = 'a'; ins->func = &func; printf("db: %f ch: %c",ins->db, ins->ch); ins->func(5); return 0; }
// main.cpp
#include <iostream>
#include <Windows.h>
#include <stdio.h>
#include <cstdlib>
#include <ctime> //Headers
#include <string>
#include <iomanip>
using namespace std;
void PrintHeader();
void ShuffleDeck (int[]);
int GetCard(int[]); //Function Prototype
char GetSuit(int);
void DealCard(int[]);
//This function will go to a specific location on the screen with (x,y) coordinants.
void gotoxy(int x, int y)
{
HANDLE hConsole = GetStdHandle ( STD_OUTPUT_HANDLE );
if ( INVALID_HANDLE_VALUE != hConsole )
{
COORD pos = {x, y};
SetConsoleCursorPosition ( hConsole, pos );
}
return;
}
//This function will take the deck and add a number to it so it's not empty.
void ShuffleDeck (int deck[])
{
int temp = 0, n = 0;
for (int i = 0; i < 52; i++)
deck[i] = i;
return;
}
//This function will return the suit picture of the card when called.
char getsuit(int SuitNum)
{
switch(SuitNum)
{
case 1: return char(3);
case 2: return char(4);
case 3: return char(5);
case 4: return char(6);
}
}
//Main function
int main()
{
int y, z;
//int PlayerSum[8]; <-- I need to figure out how to keep count of the sum for each player and dealer.
int deck[52],
char HorS;
bool test[7];
srand(time(NULL)); // Variable Declarations
PrintHeader(); // Print the "BlackJack" header.
ShuffleDeck(deck); // Gives each card a value so the array is not just empty.
DealCard(deck); // Deals two cards for each person but doesn't show 1 card for the dealer.
// This for loop will set each value for test[] array == 'true'.
for(int i = 0; i < 8; i++)
{
test[i] = true;
}
//Checks if Player 1 wants to hit or stay
z = 16;
y = 8;
while(test[1] == true)
{
gotoxy(0,z);
cout << "Player 1: Hit(h) or Stay(s)? ";
cin >> HorS;
cout << endl;
if(tolower(HorS) == 'h')
{
gotoxy(9,y);
GetCard(deck);
y++;
z++;
}
else if(tolower(HorS) == 's')
{
gotoxy(11,y);
cout << "XX";
z++;
test[1] = false;
}
}
//Checks if Player 2 wants to hit or stay
y = 8;
while(test[2] == true)
{
gotoxy(0,z);
cout << "Player 2: Hit(h) or Stay(s)? ";
cin >> HorS;
cout << endl;
if(tolower(HorS) == 'h')
{
gotoxy(19,y);
GetCard(deck);
y++;
z++;
}
else if(tolower(HorS) == 's')
{
gotoxy(21,y);
cout << "XX";
z++;
test[2] = false;
}
}
//Checks if Player 3 wants to hit or stay
y = 8;
while(test[3] == true)
{
gotoxy(0,z);
cout << "Player 3: Hit(h) or Stay(s)? ";
cin >> HorS;
cout << endl;
if(tolower(HorS) == 'h')
{
gotoxy(29,y);
GetCard(deck);
y++;
z++;
}
else if(tolower(HorS) == 's')
{
gotoxy(31,y);
cout << "XX";
z++;
test[3] = false;
}
}
//Checks if Player 4 wants to hit or stay
y = 8;
while(test[4] == true)
{
gotoxy(0,z);
cout << "Player 4: Hit(h) or Stay(s)? ";
cin >> HorS;
cout << endl;
if(tolower(HorS) == 'h')
{
gotoxy(39,y);
GetCard(deck);
y++;
z++;
}
else if(tolower(HorS) == 's')
{
gotoxy(41,y);
cout << "XX";
z++;
test[4] = false;
}
}
//Checks if Player 5 wants to hit or stay
y = 8;
while(test[5] == true)
{
gotoxy(0,z);
cout << "Player 5: Hit(h) or Stay(s)? ";
cin >> HorS;
cout << endl;
if(tolower(HorS) == 'h')
{
gotoxy(49,y);
GetCard(deck);
y++;
z++;
}
else if(tolower(HorS) == 's')
{
gotoxy(51,y);
cout << "XX";
z++;
test[5] = false;
}
}
//Checks if Player 6 wants to hit or stay
y = 8;
while(test[6] == true)
{
gotoxy(0,z);
cout << "Player 6: Hit(h) or Stay(s)? ";
cin >> HorS;
cout << endl;
if(tolower(HorS) == 'h')
{
gotoxy(59,y);
GetCard(deck);
y++;
z++;
}
else if(tolower(HorS) == 's')
{
gotoxy(61,y);
cout << "XX";
z++;
test[6] = false;
}
}
//Checks if Player 7 wants to hit or stay
y = 8;
while(test[7] == true)
{
gotoxy(0,z);
cout << "Player 7: Hit(h) or Stay(s)? ";
cin >> HorS;
cout << endl;
if(tolower(HorS) == 'h')
{
gotoxy(69,y);
GetCard(deck);
y++;
z++;
}
else if(tolower(HorS) == 's')
{
gotoxy(71,y);
cout << "XX";
z++;
test[7] = false;
}
}
//Dealer reveals card and hits if < 17 or stays if > 17.
gotoxy(0,z);
cout << endl << "Dealer reveals second card.";
gotoxy(0,6);
GetCard(deck);
getchar();
getchar();
return 0;
}
void PrintHeader()
{
cout << "============================================================================" << endl
<< "| "
<< char(3) << " " << char(4) << " BlackJack " << char(5) << " " << char(6)
<< " |" << endl
<< "============================================================================" << endl << endl;
cout << "Dealer Player 1 Player 2 Player 3 Player 4 Player 5 Player 6 Player 7" << endl
<< "------ -------- -------- -------- -------- -------- -------- --------" << endl;
return;
}
int GetCard(int deck[])
{
bool test = true;
char suit;
int CardNum, value;
CardNum = rand() % 52; // Gets a random number from 52 (deck).
while(test == true) // The while loop will make sure no cards are repeated.
{
if (deck[CardNum] == -1)
CardNum = rand() % 52;
else
{
value = deck[CardNum] %13;
test = false;
}
}
deck[CardNum] = -1;
if (value == 0)
{
cout << setw(3) << "A";
//sum += 1;
}
else if (value > 0 && value < 10)
{
cout << setw(3) << (value + 1);
}
else if (value == 10)
{
cout << setw(3) << "J"; //Prints out the value on the screen.
}
else if(value == 11)
{
cout << setw(3) << "Q";
}
else if(value == 12)
{
cout << setw(3) << "K";
}
suit = GetSuit(CardNum); // Gets the suit for the card.
cout << suit;
return value;
}
//Gets suit for the card.
char GetSuit(int CardNum)
{
int value;
char suit = 0;
value = CardNum / 13;
switch(value)
{
case 0: suit = char(3);
break;
case 1: suit = char(4);
break;
case 2: suit = char(5);
break;
case 3: suit = char(6);
break;
}
return suit;
}
//Deals 2 cards for each person. Dealer only shows 1 card.
void DealCard(int deck[])
{
gotoxy(0,6);
cout << ">";
gotoxy(0,7); // Dealer
GetCard(deck);
gotoxy(9,6);
GetCard(deck); //Player 1
gotoxy(9,7);
GetCard(deck);
gotoxy(19,6);
GetCard(deck); //Player2
gotoxy(19,7);
GetCard(deck);
gotoxy(29,6);
GetCard(deck); //Player 3
gotoxy(29,7);
GetCard(deck);
gotoxy(39,6);
GetCard(deck); //Player 4
gotoxy(39,7);
GetCard(deck);
gotoxy(49,6);
GetCard(deck); //Player 5
gotoxy(49,7);
GetCard(deck);
gotoxy(59,6);
GetCard(deck); //Player 6
gotoxy(59,7);
GetCard(deck);
gotoxy(69,6);
GetCard(deck); // Player 7
gotoxy(69,7);
GetCard(deck);
return;
}