Hi everyone, I am still having a few small problems understanding some of the rules of C. I've successfully converted a program from C++ to C with the help of a book. Unfortunately there is one part that I do not understand. i understand the concept somewhat but does not make much sense. For example
[code]
if ((*Group).Packs.Climb.Num > 4)
{
(*Group).Packs.Climb.Discount = (*Group).Packs.Climb.Charges * .10;
(*Group).Packs.Climb.Charges -= (*Group).Packs.Climb.Discount;
}
else
(*Group).Packs.Climb.Discount = 0;
[\code]
My question is why do I need the paranthesis around pointer group. In C++ I would not need the paranthesis or the pointer but I tried to write it in c using -> but received an error. The way I saw it written in the book was written with the paranthesis but I do not want to complete the program before I completely understand why? I receive an illegal inderection error when I use *Group->Packs.Climb.Charges * .10; or if I remove the paranthesis. Please let me know if it is something simple of if there is a book that goes deep into C programming so that I can understand this better
**I've included more code below just in case you need to see it**
Code:
#include<iostream.h>
#include <iomanip.h>
#include<stdio.h>
//The data structures
struct Package1 //Climbing package
{
int Num; //Number in party
int Beginners; //Those needing instructions
int Advanced; //Those not needing instructions
int NeedEquip; //Those renting camping equipments
float BaseCharges; //Base charges
float Charges; //Total charges
float Instruction; //Cost of instruction
float Equipment; //Cost of equipment rental
float Discount; //Discount
float Deposit; //Required deposit
};
struct Package2 //Scuba package
{
int Num; //Number in party
int Beginners; //Those needing instructions
int Advanced; //Those not needing instructions
float BaseCharges; //Base charges
float Charges; //Total charges
float Instruction; //Cost of instruction
float Discount; //Discount
float Deposit; //Required deposit
};
struct Package3 //Sky Diving Package
{
int Num; //Number in party
int Lodge1; //Number at 1st lodging choice
int Lodge2; //Number at 2nd lodging choice
float BaseCharges; //Base charges
float Charges; //Total charges
float Discount; //Discount
float Lodging; //Cost of lodging
float Deposit; //Required deposit
};
struct Package4 //Spelunking Package
{
int Num; //Number in party
int NeedEquip; //Those renting camping equipments
float BaseCharges; //Base charges
float Charges; //Total charges
float Equipment; //Cost of equipment rental
float Discount; //Discount
float Deposit; //Required deposit
};
union Pack //Combines all four structs
{
struct Package1 Climb;
struct Package2 Scuba;
struct Package3 Sky;
struct Package4 Spel;
};
struct Reservation
{
int PackNum; //Indicates which package is stored
union Pack Packs; //All Structs are combined into union Pack and passed on to Packs
};
//Constants for the charges
const float ClimbRate = 350.0; //Base rate - Devil's Courthouse
const float ScubaRate = 1000.0; //Base Rate - Bahamas
const float SkyDiveRate = 400.0; //Base Rate - Sky diving
const float CaveRate = 700.0; //Base Rate - Spelunking
const float ClimbInstruct = 100.0; //Climbing instructions
const float ScubaInstruct = 100.0; //Scuba instructions
const float DailyCampRental = 40.0; //Daily camping equip. rental
const float DayLodge1 = 65.0f; //Lodging option (sky diving)
const float DayLodge2 = 120.0f; //Lodging option (sky diving)
//Function prototypes
void Climbing(struct Reservation*);
void Scuba(struct Reservation*);
void SkyDive(struct Reservation*);
void Spelunk(struct Reservation*);
int Menu(void);
void DisplayInfo(const struct Reservation*);
void DisplayPack1(const struct Reservation*);
void DisplayPack2(const struct Reservation*);
void DisplayPack3(const struct Reservation*);
void DisplayPack4(const struct Reservation*);
//Main function
main()
{
int Selection;
struct Reservation Group;
do
{
Selection = Menu();
switch (Selection)
{
case 1: Climbing(& Group);
break;
case 2: Scuba(& Group);
break;
case 3: SkyDive(& Group);
break;
case 4: Spelunk(& Group);
break;
case 5: printf("Exiting program.\n\n"); //(cout had been changed to printf for c conversion)
}
if (Selection <5)
DisplayInfo(& Group);
} while (Selection != 5);
return 0;
}
//Definition of function Menu.
//Displays the main menu and asks the user to select an option.
//Returns an integer in the range 1 - 5.
int Menu(void)
{
int Choice;
do
{
printf("\nHigh Adventure Travel Agency\n");
printf("------------------------------\n");
printf("1) Devil's Courthouse Adventure Weekend\n");
printf("2) Scuba Bahama\n");
printf("3) Sky Dive Colorado\n");
printf("4) Barron Cliff Spelunk\n");
printf("5) Exit program\n\n");
printf("Enter 1, 2, 3, 4, or 5: ");
scanf("%d", &Choice); //(cin has been changed to scanf for c conversion)
if (Choice < 1 || Choice > 5) //If choice is less than 1 or greater than 5
printf("Invalid Selection\n"); //Cout/printf "Invalid Selection
} while (Choice < 1 || Choice > 5); //?????????????
return Choice;
}
//Definition of Climbing function.
// Uses a Reservation reference parameter to hold the vacation
// package information. This function calculates the charges for
// the Devil's Courthouse Adventure Weekend package.
void Climbing(struct Reservation *Group)
{
(*Group).PackNum= 1;
printf("\nDevil's Courthouse Adventure Weekend\n");
printf("--------------------------------------\n");
printf("How many will be going who need an instructor? ");
scanf("%d", &(*Group).Packs.Climb.Beginners);
printf("How many advanced climbers will be going? ");
scanf("%d", &(*Group).Packs.Climb.Advanced);
(*Group).Packs.Climb.Num =(*Group).Packs.Climb.Beginners + (*Group).Packs.Climb.Advanced;
printf("How many will be rent camping equipment? ");
scanf("%d", &(*Group).Packs.Climb.NeedEquip);
//Calculate base charges
(*Group).Packs.Climb.BaseCharges = (*Group).Packs.Climb.Num * ClimbRate;
(*Group).Packs.Climb.Charges = (*Group).Packs.Climb.BaseCharges;
//Calculate 10% discount for 5 or more
if ((*Group).Packs.Climb.Num > 4)
{
(*Group).Packs.Climb.Discount = (*Group).Packs.Climb.Charges * .10;
(*Group).Packs.Climb.Charges -= (*Group).Packs.Climb.Discount;
}
else
(*Group).Packs.Climb.Discount = 0;
}