LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-06-2006, 09:38 AM   #1
scjeff05
LQ Newbie
 
Registered: Feb 2006
Location: South Carolina
Posts: 11

Rep: Reputation: Disabled
Question..


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*, ...)'


here is the code
Code:
int main()
{
float gascost, milesoftrip, milespergallon, totalcosta;
float calculatecost(float gascost,float milesoftrip,float milespergallon);
void enterdata(float &gascost, float &milesoftrip, float &milespergallon);
void printresults(float &totalcosta);

enterdata(gascost, milesoftrip, milespergallon);
totalcosta=calculatecost(gascost,milesoftrip,milespergallon);

printresults(totalcosta);
return 0;
}
void enterdata(float &gascost, float &milesoftrip, float &milespergallon)
{
printf("Enter GAS Cost ");
scanf(gascost);
printf("Enter MILES ");
scanf(milesoftrip);
printf("Enter MPG ");
scanf(milespergallon);

}
float calculatecost(float gascost,float milesoftrip,float milespergallon)
{
float totalcost;
totalcost = (milesoftrip/milespergallon) * gascost;
return totalcost;

}
 
void printresults(float &totalcosta)
{
printf(totalcosta);
}
its probably a mess because all i have been taught it c++, but if anyone could help that would be great. thanks in advance
 
Old 02-06-2006, 10:10 AM   #2
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
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:
Code:
scanf(gascost);
should be
Code:
scanf("%f",&gascost);
 
Old 02-06-2006, 10:25 AM   #3
scjeff05
LQ Newbie
 
Registered: Feb 2006
Location: South Carolina
Posts: 11

Original Poster
Rep: Reputation: Disabled
Thanks!!!!
 
Old 02-06-2006, 10:29 AM   #4
scjeff05
LQ Newbie
 
Registered: Feb 2006
Location: South Carolina
Posts: 11

Original Poster
Rep: Reputation: Disabled
One more quick question, i need to create a makefile, i know what goes in it, i just dont know how to create it.
 
Old 02-06-2006, 10:34 AM   #5
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
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?
Code:
int main()
{
float gascost, milesoftrip, milespergallon, totalcosta;
float calculatecost(float gascost,float milesoftrip,float milespergallon);
void enterdata(float &gascost, float &milesoftrip, float &milespergallon);
void printresults(float &totalcosta);
....
I would normally write it like:

Code:
float calculatecost(float,float,float);
void enterdata(float&, float&, float&);
int main()....
plus this is "C" correct?
the enterdata func accepts float refs? are these meant to be pointers?

Last edited by dmail; 02-06-2006 at 10:40 AM.
 
Old 02-06-2006, 11:15 AM   #6
scjeff05
LQ Newbie
 
Registered: Feb 2006
Location: South Carolina
Posts: 11

Original Poster
Rep: Reputation: Disabled
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

but here is the code
Code:
#include <stdio.h>

int main()
{
float gascost, milesoftrip, milespergallon, totalcosta;
float calculatecost(float gascost,float milesoftrip,float milespergallon);
void enterdata(float &gascost, float &milesoftrip, float &milespergallon);
void printresults(float &totalcosta);

enterdata(gascost, milesoftrip, milespergallon);
totalcosta=calculatecost(gascost,milesoftrip,milespergallon);

printresults(totalcosta);
return 0;
}

void enterdata(float &gascost, float &milesoftrip, float &milespergallon)
{
printf("Enter GAS Cost ");
scanf("%f",&gascost);  
printf("Enter MILES ");  
scanf("%f",&milesoftrip);
printf("Enter MPG ");
scanf("%f",&milespergallon);
}

float calculatecost(float gascost,float milesoftrip,float milespergallon)
{
float totalcost;
totalcost = (milesoftrip/milespergallon) * gascost;
return totalcost;
}

void printresults(float &totalcosta)
{
scanf("%f",&totalcosta);
}
 
Old 02-06-2006, 11:17 AM   #7
scjeff05
LQ Newbie
 
Registered: Feb 2006
Location: South Carolina
Posts: 11

Original Poster
Rep: Reputation: Disabled
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
 
Old 02-06-2006, 11:26 AM   #8
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
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

foo(&a);this passes the address of a to foo.

Last edited by dmail; 02-06-2006 at 11:40 AM.
 
Old 02-06-2006, 11:35 AM   #9
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
Hmmm just noticed you said
Quote:
...then i get this project in C...
Please advise if its a school coursework etc. in future ;(
 
Old 02-06-2006, 12:42 PM   #10
scjeff05
LQ Newbie
 
Registered: Feb 2006
Location: South Carolina
Posts: 11

Original Poster
Rep: Reputation: Disabled
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.
 
Old 02-06-2006, 01:01 PM   #11
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
Quote:
Originally Posted by scjeff05
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?
 
Old 02-06-2006, 01:08 PM   #12
scjeff05
LQ Newbie
 
Registered: Feb 2006
Location: South Carolina
Posts: 11

Original Poster
Rep: Reputation: Disabled
Im not sure, i understand how it works, i just dont understand how to apply it to the code.
 
Old 02-06-2006, 01:16 PM   #13
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
Okay,

you are confusing & with *

The * says this will be a pointer to a memory location
Whereas & says take this variable and return the address.

So when you define a function you will tell it to expect a pointer as follows:
Code:
void enterdata(float *gascost
              ,float *milesoftrip
              ,float *milespergallon);
When you call the function you want to pass the address of the variables as follows:
Code:
enterdata(&gascost, &milesoftrip, &milespergallon);
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.

Last edited by graemef; 02-06-2006 at 01:18 PM.
 
Old 02-06-2006, 01:21 PM   #14
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
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);
}

Last edited by dmail; 02-06-2006 at 01:25 PM.
 
Old 02-06-2006, 01:32 PM   #15
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
heres a post i made a while ago about pointers it may help
http://www.linuxquestions.org/questi...d.php?t=383082
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Question, Apples Contribution to Open Source + MacOs file structure question Higgy3k Other *NIX 5 07-25-2005 04:23 AM
Not your regular GRUB question - just a short question for a fried MBR!! ziphem Linux - General 3 01-31-2005 01:51 PM
Question 1 Firewall Log Question 2 Network Monitor Soulful93 Linux - Networking 4 08-04-2004 11:05 PM
login prompt question & kde scheme question JustinCoyan Slackware 2 06-09-2004 02:02 PM
samba smb.config question (quick question) TheDOGG Linux - Networking 1 03-02-2004 07:19 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:52 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration