ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Hey, im new to the forums and new to programming in linux. I hope to learn a lot from this forum. Im not sure if im posting this is the correct place, but just let me know if im not.
Ok, im writing a simple program in C and i keep getting this error message new1.c: In function `void enterdata(float&, float&, float&)':
new1.c:21: error: cannot convert `float' to `const char*' for argument `1' to `int scanf(const char*, ...)'
new1.c:23: error: cannot convert `float' to `const char*' for argument `1' to `int scanf(const char*, ...)'
new1.c:25: error: cannot convert `float' to `const char*' for argument `1' to `int scanf(const char*, ...)'
new1.c: In function `void printresults(float&)':
new1.c:38: error: cannot convert `float' to `const char*' for argument `1' to `int printf(const char*, ...)'
scanf expects at least two argumants. The first is a format string that describes the variables that are to be read in and the remaining arguments are pointers to the the variables that you wich to set up so:
I didn't think function prototypes inside main would be allowed or work, but it compiles and works fine. Maybe it just me? but I think it's bad practice, unless someone can tell me a viable reason for doing it?
ugh, now its saying this
new3.c: In function `main':
new3.c:7: error: syntax error before '&' token
new3.c:8: error: syntax error before '&' token
new3.c: At top level:
new3.c:17: error: syntax error before '&' token
new3.c: In function `enterdata':
new3.c:20: error: `gascost' undeclared (first use in this function)
new3.c:20: error: (Each undeclared identifier is reported only once
new3.c:20: error: for each function it appears in.)
new3.c:22: error: `milesoftrip' undeclared (first use in this function)
new3.c:24: error: `milespergallon' undeclared (first use in this function)
new3.c: At top level:
new3.c:34: error: syntax error before '&' token
new3.c: In function `printresults':
new3.c:36: error: `totalcosta' undeclared (first use in this function)
im really new to linux and no one has taught me how to do this
yeah, see thats the problem, i have learned the basics of c++, then i get this project in C and now i have no clue what to do...so lost..., well if anyone can help me some more that would be great
lol them errors sort of answer my questions. C doesn't have refs but uses pointers, so I you change it to this it should work, havn't tested it but it should be ok. If you dont understand what ive done just ask and ill go through it.
<edited see next post>
to pass by reference(different than a reference) you need to pass the address of the variable(if it's not already a pointer) and accept a pointer in the func:
example
int a;the variable
foo(int*);func prototype
No, im taking these projects off some cd that i got a wile back, it goes along with a book but i dont know where i placed it, the only class i took on the subject was C++ in high school, but that was some time ago and i cant remeber half that stuff.
No, im taking these projects off some cd that i got a wile back, it goes along with a book but i dont know where i placed it, the only class i took on the subject was C++ in high school, but that was some time ago and i cant remeber half that stuff.
Ok Im sorry.
Did you see the post i made earlier or would you like me to post it again?
Don't worry it takes a while to really understand what they do and mean but because they are so important in C / C++ it is worth spending the time to understand them.
this is how i would write it plus added a deference to show how its done
Code:
#include <stdio.h>
//function prototypes
float calculatecost(float ,float ,float* );
void enterdata(float*, float* , float* );
void printresults(float);
int main()
{
float gas, miles, mpg;
//pass by reference instead of value using the "&" which
//gives the address
enterdata(&gas, &miles, &mpg);
printresults( calculatecost(gas,miles,&mpg) );
return 0;
}
void enterdata(float* gas, float* miles, float* mpg)
{
printf("Enter GAS Cost ");
//these are already pointers so no need to pass the address
scanf("%f",gas);
printf("Enter MILES ");
scanf("%f",miles);
printf("Enter MPG ");
scanf("%f",mpg);
}
//lets just pass by reference mpg to show how to get the
//value from a pointer
float calculatecost(float gas,float milesp,float* mpg)
{
//"*" deferences a pointer and gets the value of the variable
//it points to
//remove the temp as there was no need for it
return ((miles / *mpg) * gas);
}
void printresults(float cost)
{
printf("cost was %f\n",cost);
}
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.