LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 06-06-2013, 01:04 PM   #1
lleb
Senior Member
 
Registered: Dec 2005
Location: Florida
Distribution: CentOS/Fedora/Pop!_OS
Posts: 2,983

Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
.c error when compiling: %lf expects double but 2nd is char.


this is total bunk as i have zero char set. i only set double as that is all im using:

Code:
//The formula V=1/3lwh gives the volume (V) of a rectangular
//pyramid with length (l), width (w) and height (h)

//##########################################################
// Created by Raymond L. Brunkow 
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 2 or version 3 of the
// license, at your option.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
//##########################################################
#include<stdio.h>

//Problem Input
// double for l, w, h in meters
//Problem Output
// double in cubic meters
// Formula:  V=1/3*(l*w*h) or V=(double)1/(double)3*(l*w*h*) or V=(l*w*h*)/(double)3

int main(void)

{

	// setting variables
	double l, w, h, v;

	//get inputs for length, width, and height in meters.
	printf("\nPlease enter the (l)ength, (w)idth, and (h)eight in meters: \n");
	scanf("%lf", "%lf", "%lf", &l, &w, &h);

	//Formula for Volume of rectangular pyramid
	v = ( l * w * h ) / (double)3;

	// output 
	printf("\nThe volume of your rectangular pyramid with %.2f (l)ength, %.2f (w)idth, and %.2f (h)eight in meters is %.2f cubic meters.\n", l, w, h, v);

	sleep(5);

	return(0);

}
absolutely zero char set as variables, only double. double requires %lf, so that is exactly what im using. what is gcc doing?

error:
Code:
ssma-imac:ENG-3211 ssma$ gcc -o VOL volume_rectangular_pyramid.c 
volume_rectangular_pyramid.c: In function ‘main’:
volume_rectangular_pyramid.c:38: warning: writing into constant object (argument 2)
volume_rectangular_pyramid.c:38: warning: format ‘%lf’ expects type ‘double *’, but argument 2 has type ‘char *’
again i dont have a 'char *' type in my script.

no this is not homework, just practice. only homework this week is to read cpt 1-2, and 3.1/3.2 but i dont like going that long (3 weeks) without some kind of practice. im practicing the set type (double)3 in my equation line along with combining input/output on the same line of script.
 
Old 06-06-2013, 01:20 PM   #2
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301
That's not how you use scanf. The first argument to scanf is a string containing what you want to scan. You have split this string across the first 3 arguments. Also note that you are missing an include of unistd.h, where sleep() is declared.
 
Old 06-06-2013, 01:30 PM   #3
lleb
Senior Member
 
Registered: Dec 2005
Location: Florida
Distribution: CentOS/Fedora/Pop!_OS
Posts: 2,983

Original Poster
Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
Quote:
Originally Posted by H_TeXMeX_H View Post
That's not how you use scanf. The first argument to scanf is a string containing what you want to scan. You have split this string across the first 3 arguments. Also note that you are missing an include of unistd.h, where sleep() is declared.
ok im a bit confused, still very new to C, i was looking at an example the prof. gave last class with scanf:

Code:
double a, b, c, disc, root_1, root2;
foo
printf("Please enter a, b, and c: ");
scanf("%lf %lf %lf", &a, &b, &c);
i saw one error i made with "" around each %lf and ',' that didnt need to be there. still same error as before.

interesting, i have the sleep(n) in all of my scripts and have never had to use anything other then stdio.h

thank you. that did the trick. odd that with unistd.h not being in any of my other scripts that sleep(n) worked without a hitch. any idea's why that is?

edited to add the corrected code:

Code:
#include<unistd.h>


scanf("%lf %lf %lf", &l, &w, &h);
 
Old 06-07-2013, 02:32 AM   #4
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301
Your version of gcc may be lenient by default and recognize the implied include of unistd.h. I've seen older versions error out. Either way, including it is the right thing to do.
 
1 members found this post helpful.
Old 06-07-2013, 09:02 AM   #5
lleb
Senior Member
 
Registered: Dec 2005
Location: Florida
Distribution: CentOS/Fedora/Pop!_OS
Posts: 2,983

Original Poster
Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
thanks for the answer.
 
  


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
error: invalid conversion from ‘const char*’ to ‘char*’ zx_ Programming 2 09-06-2011 08:17 PM
invalid conversion from `const char*' to `char' error holystinken Programming 7 11-23-2009 06:01 PM
'C' error in printf warning: format ‘%lf’ expects type ‘double’ ... SuperNomad Programming 4 11-17-2009 09:07 AM
error: invalid conversion from const char* to char* Dahakon Programming 1 08-31-2009 09:33 AM
How to convert double to char* Jane2008 Programming 5 02-13-2009 09:26 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 12:05 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