Sunday, July 12, 2009

C programming!!?

What is wrong with this source code?? Look for %26lt;---Line with problem, that's what the compiler is saying somthing is wrong with...





#include %26lt;stdlib.h%26gt;


#include %26lt;stdio.h%26gt;


#define SPEED 65


#define FAST 55


#define SIDEROADS 25


int main()


{


int miles;


int signs;


int gas_station;


char length[10];


char sign[10];


char stop[10];


char gas[10];


char gas_price[10];


char diff[10];


int MPH=25;


float price=2.25;


float permile=0.10;


float tank=65;


diff=permile*tank*length; %26lt;-----Line with problem

C programming!!?
Should be:





diff=permile*tank*atof(length);





Now don't post your homework lessons on here anymore ... do them yourself. ;)
Reply:You have defined diff as char[10]. It should be float.





change





char diff[10];





to





float diff;





And, length is again a char[10]. I am not sure why you would want to multiply it with floats :-\





Probably, the assignment of 'diff' should come after you get the value for length.. and I see you are converting length to integer already and storing in 'miles' so you should replace length with miles..
Reply:Also, in addition to the top poster, you're attempting to assign data of type float, to a character array. Should be float





char diff[10]; %26lt;-change this to float
Reply:The problem is that "length" is an array of ten characters. If you use "length" all by itself, its value is a pointer to the array. The compiler does not let you multiply a pointer by a float, because it just does not make sense to do so. If you want to use length in the multiplication, you have to provide an index to the member of the array that you want to multiply by. For example, to use the first char in the array, the line of code should be





diff = permile * tank * length[0];





Where "diff" is a variable of type float, not a character array.
Reply:Your problem line is multiplying two numerical values and a char array. These are mismatched data types. Do you mean to use an element of the array, which is an int value (chars are ints in C/C++)?





ex.


permile * tank * length[0]





**Added: What the third poster said - you're also assigning the result to a char array, it should be an element of a float array.





ex.


float diff[10];


.


.


.


diff[0] = permile * tank * length[0];
Reply:Doh!





-----%26gt;Invalid type assignment!!!


No comments:

Post a Comment