By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,388 Members | 1,761 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,388 IT Pros & Developers. It's quick & easy.

error: cannot convert `float' to `float*' for argument `1' to `f

P: 10
Hi,
I'm trying to write a program using the Dev C++ 4.9.9.2 compiler that takes input numbers from a word document, sums them together, and then gives me a result. It should be easy as the book I have tells me what to do, problem is I get the error message
"cannot convert `float' to `float*' for argument `1' to `float array_sum(float*, int)' " for line 22 of the program. This is what I have;

#include <iostream.h>
#include <fstream.h>

float array_sum(float c[100], int m);

int main()
{
float a[100];
int n;
float sum; //b[100];
ifstream read("C:\\Dev-Cpp\\numbers one to one hundred.doc");
if(!read)
{
cout << "input file failed to open";
system ("pause");
exit(1);
}
while(!read.eof())
{
read >> a[100];
}
sum = array_sum(a[100],m);
cin >> n;
cout << "a sum = " << sum << endl;
read.close();
system ("pause");
}

please keep your replies simple, I'm still quiet new to the whole programming thing.
Pixie
Jul 3 '06 #1
Share this Question
Share on Google+
4 Replies


P: 10
I should have said the numbers I was extracting from word were just 1 to 100, in order, with no repetition
Pixie
Jul 3 '06 #2

100+
P: 293
D_C
Line 22:
sum = array_sum(a[100],m);

First, a[100] is the 100th entry of array a, which is 100(float) according to you. Second, arrays cannot be passed all at once. It's looking for a pointer to the array. Change Line 22 so it now uses the address of a, instead of the 100th element of array a. Also make the appropriate updates to array_sum function so the first argument is a pointer to an array of floats.

sum = array_sum(&a,m); // Should give the starting address of array a

Also, I did not see where m was defined. I am assuming it's 100, the length of a. Generally if you pass an array by address, it only knows the starting address, not how many elements there are in it. A character array might be the exception, as it should have the null string terminator at the end, and it should know where to stop. That's not always the case though.
Jul 3 '06 #3

Banfa
Expert Mod 5K+
P: 8,916
Hope you don't mind D_C most of what you say is true but...

First, a[100] is the 101st entry of array a, and in this example an out of bounds access as a only has 100 entries.

&a is not a good way to get the address of a, it has type float (*)[100]. More usual would be just a or &a[0] which are both of type float *. It should be noted that all these expressions will access the same location in memory, however you have no garunttee from the C standard that

&a == &a[0]

because a give platform could have a different notation for holding pointer to array and pointer to float.

Since normally you would declare the called function

void fn( float *InArray );

you want to pass a variable of type float *. Some compilers will issue a warning if you pass &a to fn.
Jul 3 '06 #4

P: 10
thanks for the help, its pretty well working now but I have to toy with it so it doesn't freeze when the numbers get too large. In case anyone is interested this is what i currently have.
-------------------------------------------------------------------------------------------------------------------------
#include <iostream.h>
#include <fstream.h>
int main()
{
int m, j;
float a[100], sum[m], A;
cout << "the value of m is ";
cin >> m;
ifstream read("C:\\Dev-Cpp\\numbers one to one hundred.doc");
ofstream prt("C:\\Dev-Cpp\\summed one to one hundred.doc", ios::trunc);
if(!read)
{
cout << "input file failed to open";
system ("pause");
exit(1);
}
if(!prt)
{
cout << "output file failed to open";
system ("pause");
exit(1);
}
sum[0] = 0;
for (j = 0; j <= m; j++)
{
cout << "a[" << j << "] = " << A << "\n";
prt << "a[" << j << "] = " << A << " ";
read >> A;
cout << "a sum = " << sum[j] << endl;
prt << "a sum = " << sum[j] << endl;
sum[j+1] = sum[j] + A;
}
read.close();
system ("pause");
}

Pixie
Jul 4 '06 #5

Post your reply

Sign in to post your reply or Sign up for a free account.