User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 391,769 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,185 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C advertiser:
Views: 8500 | Replies: 9
Reply
Join Date: Aug 2004
Posts: 3
Reputation: tnorton is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
tnorton tnorton is offline Offline
Newbie Poster

Help cannot convert from 'double' to 'float [6][3]'

  #1  
Aug 11th, 2004
Using this code:

Fees = ovrh + (wr * z);

I am getting the following error:

error C2440: '=' : cannot convert from 'double' to 'float [6][3]'

Fees, ovrhd, wr and z are all declared as float. I believe if I can figure out what the problem is, I will have a functioning program. After several days straight of this program, I believe that I may be forgetting something basic.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Apr 2004
Posts: 3,449
Reputation: Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light 
Rep Power: 16
Solved Threads: 138
Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: cannot convert from 'double' to 'float [6][3]'

  #2  
Aug 11th, 2004
Could you show more of the code? Specifically the actual declarations for the variables would be helpful. I find no such issue with this minimal code sample.
#include <stdio.h>
 
 int main(void)
 {
    float ovrh = 1.0F, wr = 1.0F, z = 1.0F, Fees = ovrh + (wr * z);
    printf("Fees = %g\n", Fees);
    return 0;
 }
Reply With Quote  
Join Date: Jun 2004
Location: Marin, CA, USA
Posts: 434
Reputation: Chainsaw is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 10
Chainsaw's Avatar
Chainsaw Chainsaw is offline Offline
Unprevaricator

Re: cannot convert from 'double' to 'float [6][3]'

  #3  
Aug 12th, 2004
The calculation of two floats yields a double. Compilers often WARN about converting from double to float because of a possible loss of precision.

YOUR error code sure sounds like you have an ARRAY of floats like

float fees[6][3];

fees = 1.0 * 2.0; // error double to float [6][3]
Reply With Quote  
Join Date: Apr 2004
Posts: 3,449
Reputation: Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light 
Rep Power: 16
Solved Threads: 138
Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: cannot convert from 'double' to 'float [6][3]'

  #4  
Aug 12th, 2004
Slight nitpick:
Originally Posted by Chainsaw
The calculation of two floats yields a double.
fees = 1.0 * 2.0;
Those aren't two floats (1.0 and 2.0), they're two doubles.
Reply With Quote  
Join Date: Aug 2004
Posts: 3
Reputation: tnorton is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
tnorton tnorton is offline Offline
Newbie Poster

Help Re: cannot convert from 'double' to 'float [6][3]'

  #5  
Aug 12th, 2004
The purpose of the program is to store shipping costs in an array and then display them on the screen, in a formatted output.

Here are the declarations:

float Fees [6][3];
float i, j;
int wr, z;
const double ovrh = 2.27; // I tried to enter this as a const float, but the compiler didn't like it, so I changed it to a double which solved that one problem.

The assignment is 4 days late, so I am going to just turn it in as it. I would still like to see it work though. Thank you so much for your help!

Originally Posted by Dave Sinkula
Could you show more of the code? Specifically the actual declarations for the variables would be helpful. I find no such issue with this minimal code sample.
#include <stdio.h>
 
 int main(void)
 {
    float ovrh = 1.0F, wr = 1.0F, z = 1.0F, Fees = ovrh + (wr * z);
    printf("Fees = %g\n", Fees);
    return 0;
 }
Reply With Quote  
Join Date: Aug 2004
Posts: 3
Reputation: tnorton is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
tnorton tnorton is offline Offline
Newbie Poster

Re: cannot convert from 'double' to 'float [6][3]'

  #6  
Aug 12th, 2004
The purpose of the program is to store shipping costs in an array and then display them on the screen, in a formatted output.

Here are the declarations:

float Fees [6][3];
float i, j;
int wr, z;
const double ovrh = 2.27; // I tried to enter this as a const float, but the compiler didn't like it, so I changed it to a double which solved that one problem.

The assignment is 4 days late, so I am going to just turn it in as it. Thank you so much for your help!

Originally Posted by Chainsaw
The calculation of two floats yields a double. Compilers often WARN about converting from double to float because of a possible loss of precision.

YOUR error code sure sounds like you have an ARRAY of floats like

float fees[6][3];

fees = 1.0 * 2.0; // error double to float [6][3]
Reply With Quote  
Join Date: Apr 2004
Posts: 3,449
Reputation: Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light 
Rep Power: 16
Solved Threads: 138
Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: cannot convert from 'double' to 'float [6][3]'

  #7  
Aug 12th, 2004
Aha! So your original statement was not entirely accurate.
Fees, ovrhd, wr and z are all declared as float.
Fees is declared as an array of an array of float -- this is not the same and your compiler was kindly informing you of this. A single float might be Fees[0][0]. Try this (I'm assuming i and j are loop indices -- except that then they must be integral types and not floating point, but I'm used to i and j as loop indices -- and this is inside these loops.
Fees[i][j] = ovrh + (wr * z);
Reply With Quote  
Join Date: Jun 2004
Location: Marin, CA, USA
Posts: 434
Reputation: Chainsaw is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 10
Chainsaw's Avatar
Chainsaw Chainsaw is offline Offline
Unprevaricator

Re: cannot convert from 'double' to 'float [6][3]'

  #8  
Aug 12th, 2004
Ah HAH! As I suspected.

Dave, you are right that '1.0' is a double. However, I still believe that two floats used in a calculation result in a double, in the same way that two chars used in a calculation result in an int. In any case, the compiler can deal with that and generally puts out a warning, not an error.
Reply With Quote  
Join Date: Apr 2004
Posts: 3,449
Reputation: Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light 
Rep Power: 16
Solved Threads: 138
Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: cannot convert from 'double' to 'float [6][3]'

  #9  
Aug 12th, 2004
Originally Posted by Chainsaw
Ah HAH! As I suspected.

Dave, you are right that '1.0' is a double. However, I still believe that two floats used in a calculation result in a double, in the same way that two chars used in a calculation result in an int. In any case, the compiler can deal with that and generally puts out a warning, not an error.
Actually, two chars used in a calculation result in a char, but there may be overflow. Calculations stay withing their type. But, yes. The reason for the 'double' part of the diagnostic was with this.
 const double ovrh = 2.27;
Multiplying two floats results in a float; but adding that result to a double promotes the float result to a double. And yes, attempting to then assign this double result to a float should generate a warning.
Reply With Quote  
Join Date: Apr 2004
Posts: 3,449
Reputation: Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light 
Rep Power: 16
Solved Threads: 138
Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: cannot convert from 'double' to 'float [6][3]'

  #10  
Aug 12th, 2004
Oops. I didn't read the comment.
// I tried to enter this as a const float, but the compiler didn't like it, so I changed it to a double which solved that one problem
It makes me curious -- tnorton: are you saying that the compiler barked about something like this?
const float ovrh = 2.27F;
But the other stuff about the promotions should be correct.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb C Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the C Forum

All times are GMT -4. The time now is 4:35 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC