Thread: Possible Loss of data

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    48

    Smile Possible Loss of data

    Hi everyone, I've posted codes for C++ in the past but am now trying to learn c. I successfully converted the following program into C from c++. It was a pretty easy conversion, the only problem I had were with the warnings. I could not figure out (using only the book) why I kept receiving the warnings. The message runs successfully but I just wanted to understand the program a little bit better and see how I could change it to remove those warnings. All 8 warnings state

    warning C4244: '=' : conversion from 'double ' to 'float ', possible loss of data

    If you can steer me in the right direction I would greatly appreciate it..especially since I'm used to programming in C++ and C although is basically the same, confuses me every once in a while.

    Thanks

    Code:
        //
    
    //This program will assist 
    //in calculating the costs of 4 major vacation packages
    
    
    #include<iostream.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;
    };
    
    //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.0;			//Lodging option (sky diving)
    const float DayLodge2 = 120.0;			//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;
    
    	//Add cost of instruction 
    	 (*Group).Packs.Climb.Instruction = (*Group).Packs.Climb.Beginners * ClimbInstruct;
    	 (*Group).Packs.Climb.Charges += (*Group).Packs.Climb.Instruction;
    
    	//Add cost of camping equipment rental 
    	 (*Group).Packs.Climb.Equipment = (*Group).Packs.Climb.NeedEquip * DailyCampRental * 4;
    	 (*Group).Packs.Climb.Charges += (*Group).Packs.Climb.Equipment;
    
    	//Calculate required deposit 
    	 (*Group).Packs.Climb.Deposit = (*Group).Packs.Climb.Charges / 2.0;
    }
    
    //Definition of Scuba function.
    //Uses a Reservation reference parameter to hold
    //the vacation package information.
    //This function calculates the charges for the Scuba Bahama package. 
    
    void Scuba(struct Reservation *Group)
    {
    	(*Group).PackNum = 2;
    
       printf("\nScuba Bahama\n");
    	printf("--------------\n");
    	printf("How many will be going who need an instructor? ");
    	scanf("%d", &(*Group).Packs.Scuba.Beginners);
    	printf("How many advanced scuba divers will be going? ");
    	scanf("%d", &(*Group).Packs.Scuba.Advanced);
    
    	(*Group).Packs.Scuba.Num = (*Group).Packs.Scuba.Beginners + (*Group).Packs.Scuba.Advanced;
    
    	//Calculate base charges 
    	(*Group).Packs.Scuba.BaseCharges = (*Group).Packs.Scuba.Num * ScubaRate;
    	(*Group).Packs.Scuba.Charges = (*Group).Packs.Scuba.BaseCharges;
    
    	//Calculate 10% discount for 5 or more 
    	if ((*Group).Packs.Scuba.Num > 4)
    	{
    		(*Group).Packs.Scuba.Discount = (*Group).Packs.Scuba.Charges * .10;
    		(*Group).Packs.Scuba.Charges -= (*Group).Packs.Scuba.Discount;
    	}
    	else
    		(*Group).Packs.Scuba.Discount = 0;
    
    	//Add cost of instruction 
    	(*Group).Packs.Scuba.Instruction = (*Group).Packs.Scuba.Beginners * ScubaInstruct;
    	(*Group).Packs.Scuba.Charges += (*Group).Packs.Scuba.Instruction;
    
    	//Calculate required deposit 
    	(*Group).Packs.Scuba.Deposit = (*Group).Packs.Scuba.Charges / 2.0;
    }
    
    //Definition of Sky Dive function.
    //Uses a Reservation reference parameter to hold
    //the vacation package information.
    //This function calculates the charges for the Sky Dive Colorado package. */
    
    void SkyDive(struct Reservation *Group)
    {
    	(*Group).PackNum = 3;
    
       printf("\nSky Dive Colorado\n");
    	printf("-------------------\n");
    	printf("How many will be going? ");
    	scanf("%d", &(*Group).Packs.Sky.Num);
    
    	//Calculate base charges 
    	(*Group).Packs.Sky.BaseCharges = (*Group).Packs.Sky.Num * SkyDiveRate;
    	(*Group).Packs.Sky.Charges = (*Group).Packs.Sky.BaseCharges;
    
    	//Calculate 10% discount for 5 or more 
    	if ((*Group).Packs.Sky.Num > 4)
    	{
    		(*Group).Packs.Sky.Discount = (*Group).Packs.Sky.Charges * .10;
    		(*Group).Packs.Sky.Charges -= (*Group).Packs.Sky.Discount;
    	}
    	else
    		(*Group).Packs.Sky.Discount = 0;
    
    	//Calculate lodging costs 
       printf("How many will stay at Wilderness Lodge? ");
    	scanf("%d", &(*Group).Packs.Sky.Lodge1);
    	printf("How many will stay at Luxury Inn? ");
    	scanf("%d", &(*Group).Packs.Sky.Lodge2);
    
       (*Group).Packs.Sky.Lodging = ((*Group).Packs.Sky.Lodge1 * DayLodge1) + ((*Group).Packs.Sky.Lodge2 * DayLodge2);
    
    	//Calculate required deposit 
    	(*Group).Packs.Sky.Deposit = (*Group).Packs.Sky.Charges / 2.0;
    }
    
    //Definition of Spelunk function.
    //Uses a Reservation reference parameter to hold
    //the vacation package information.
    //This function calculates the charges for the Barron Cliff Spelunk package. 
    
    void Spelunk(struct Reservation *Group)
    {
    	(*Group).PackNum = 4;
    
       printf("\nBarron Cliff Spelunk Weekend\n");
    	printf("----------------------\n");
    	printf("How many will be going? ");
    	scanf("%d", &(*Group).Packs.Spel.Num);
    	printf("How many will rent camping equipment? ");
    	scanf("%d", &(*Group).Packs.Spel.NeedEquip);
    
    	//Calculate base charges 
    	(*Group).Packs.Spel.BaseCharges = (*Group).Packs.Spel.Num * CaveRate;
    	(*Group).Packs.Spel.Charges = (*Group).Packs.Spel.BaseCharges;
    
    	//Calculate 10% discount for 5 or more */
    	if ((*Group).Packs.Spel.Num > 4)
    	{
    		(*Group).Packs.Spel.Discount = (*Group).Packs.Spel.Charges * .10;
    		(*Group).Packs.Spel.Charges -= (*Group).Packs.Spel.Discount;
    	}
    	else
    		(*Group).Packs.Spel.Discount = 0;
    
    	//Add cost of camping equipment rental */
    	(*Group).Packs.Spel.Equipment = (*Group).Packs.Spel.NeedEquip * DailyCampRental * 4;
    	(*Group).Packs.Spel.Charges += (*Group).Packs.Spel.Equipment;
    
    	//Calculate required deposit 
    	(*Group).Packs.Spel.Deposit = (*Group).Packs.Spel.Charges / 2.0;
    }
    
    //Definition of function DisplayInfo.
    //Uses a constant Reservation reference parameter to hold the vacation package information.
    //This function looks in the Group.Packnum member to determine which
    //function to call to display the vacation package information.
    
    void DisplayInfo(const struct  Reservation *Group)
    {
    	switch ((*Group).PackNum)
    	{
    		case 1: DisplayPack1(& (*Group));
    				break;
    		case 2: DisplayPack2(& (*Group));
    				break;
    		case 3: DisplayPack3(& (*Group));
    				break;
    		case 4: DisplayPack4(& (*Group));
    				break;
       default: printf("ERROR: Invalid package number.\n");
    	}
    }
    
    //Definition of function DisplayPack1.
    //Uses a constant Reservation reference parameter to hold the vacation package information.
    //This function displays the information stored for vacation package 1.
    
    void DisplayPack1(const struct Reservation *Group)
    {
       printf("Number in party: %d\n", (*Group).Packs.Climb.Num);
    	printf("Base charges: $%.2f\n", (*Group).Packs.Climb.BaseCharges);
    	printf("Instruction Cost: $%.2f\n", (*Group).Packs.Climb.Instruction);
    	printf("Equipment Rental: $%.2f\n", (*Group).Packs.Climb.Equipment);
    	printf("Discount: $%.2f\n", (*Group).Packs.Climb.Discount);
    	printf("Total Charges: $%.2f\n", (*Group).Packs.Climb.Charges);
    	printf("Required Deposit: $%.2f\n", (*Group).Packs.Climb.Deposit);
    }
    
    //Definition of function DisplayPack2.
    //Uses a constant Reservation reference parameter to hold the 
    //vacation package information.This function displays the information 
    //stored for vacation package 2. 
    
    void DisplayPack2(const struct Reservation *Group)
    {
       printf("Number in party: %d\n", (*Group).Packs.Scuba.Num);
    	printf("Base charges: $%.2f\n", (*Group).Packs.Scuba.BaseCharges);
    	printf("Instruction Cost: $%.2f\n", (*Group).Packs.Scuba.Instruction);
    	printf("Discount: $%.2f\n", (*Group).Packs.Scuba.Discount);
    	printf("Total Charges: $%.2f\n", (*Group).Packs.Scuba.Charges);
    	printf("Required Deposit: $%.2f\n", (*Group).Packs.Scuba.Deposit);
    }
    
    //Definition of function DisplayPack3.
    //Uses a constant Reservation reference parameter to hold the 
    //vacation package information.This function displays the information 
    //stored for vacation package 3.
    
    void DisplayPack3(const struct Reservation *Group)
    {
       printf("Number in party: %d\n", (*Group).Packs.Sky.Num);
    	printf("Base charges: $%.2f\n", (*Group).Packs.Sky.BaseCharges);
    	printf("Lodging: $%.2f\n", (*Group).Packs.Sky.Lodging);
    	printf("Discount: $%.2f\n", (*Group).Packs.Sky.Discount);
    	printf("Total Charges: $%.2f\n", (*Group).Packs.Sky.Charges);
    	printf("Required Deposit: $%.2f\n", (*Group).Packs.Sky.Deposit);
    }
    
    //Definition of function DisplayPack4.
    //Uses a constant Reservation reference parameter to hold the 
    //vacation package information.This function displays the information 
    //stored for vacation package 4. 
    
    void DisplayPack4(const struct Reservation *Group)
    {
       printf("Number in party: %d\n", (*Group).Packs.Spel.Num);
    	printf("Base charges: $%.2f\n", (*Group).Packs.Spel.BaseCharges);
    	printf("Equipment Rental: $%.2f\n", (*Group).Packs.Spel.Equipment);
    	printf("Discount: $%.2f\n", (*Group).Packs.Spel.Discount);
    	printf("Total Charges: $%.2f\n", (*Group).Packs.Spel.Charges);
    	printf("Required Deposit: $%.2f\n", (*Group).Packs.Spel.Deposit);
    }

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    A floating-point constant literal has type double. Try this:
    Code:
    float somevar = 0.00f; /* Note the f, that makes it float */
    Or you could just eschew float and use double everywhere unless you have a need for single precision.
    My best code is written with the delete key.

  3. #3
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Basically a double is 64-bits wide and a float is 32. So when you convert from 64-bit data type to a 32-bit you lose a possible 32-bits of information. Unless you are trying to send a rocket to the moon or something this probably won't cause miscalculations. However, you can turn the warning off using one of the #pragma statements prior to the conversion. Check your docs for more info.

  4. #4
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    You don't need the #include <iostream.h> and most c programmers don't use 1 line comments ex:
    Code:
    //this is a one line commen
    they prefer this
    /*this is a one line(or multi-line commen)*/
    the one line comment is c99 standart and I personally don't like c99, but I guess it is opinionated

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  2. conversion from double to float - loss of data
    By diyteam in forum C++ Programming
    Replies: 20
    Last Post: 03-04-2008, 02:59 AM
  3. Where's the EPIPE signal?
    By marc.andrysco in forum Networking/Device Communication
    Replies: 0
    Last Post: 12-23-2006, 08:04 PM
  4. Program Crashing
    By Pressure in forum C Programming
    Replies: 3
    Last Post: 04-18-2005, 10:28 PM
  5. Binary Tree, couple questions
    By scoobasean in forum C Programming
    Replies: 3
    Last Post: 03-12-2005, 09:09 PM