Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > going crazy, invalid conversion from `char*' to `char' help needed

Reply
Thread Tools

going crazy, invalid conversion from `char*' to `char' help needed

 
 
spoilsport
Guest
Posts: n/a
 
      03-03-2004
Ive got to write a multi-function program and I'm plugging in the
functions but I keep getting this error from line 40. Im new to this
and cant find an answer anywhere.

Sam


#include <stdio.h>

int main (void)
{
void top_of_page_info (void);
void print_item_info (char item_name, double item_price);

char item_name[15], name;
double item_price;

printf ("\n\nPlease enter the name of the item: ");
scanf ("%s", & item_name);

printf ("\n\nPease enter the price of the item: $");
scanf ("%f", & item_price);

top_of_page_info ();
print_item_info (item_name, item_price); /***LINE 40***/

return 0;

}

/*******BEGIN TOP_OF_PAGE_INFO
FUNCTION**************************************/

void top_of_page_info (void)
{
printf ("\n\n PROGRAM DESCRIPTION\n");
printf (" by\n");
printf (" YOUR NAME HERE\n");
printf (" Date Written: DDMMMYY\n");
printf ("------------------------------------------------------\n\n");
}

/*******BEGIN PRINT_ITEM_INFO
FUNCTION**************************************/

void print_item_info (char item_name, double item_price)

{
printf ("\n\nThe item name is %s\n", item_name);
printf ("The item price is %f\n", item_price);
}

/************************************************** ***********************************************/
 
Reply With Quote
 
 
 
 
Leor Zolman
Guest
Posts: n/a
 
      03-03-2004
On 2 Mar 2004 19:17:45 -0800, http://www.velocityreviews.com/forums/(E-Mail Removed) (spoilsport)
wrote:

>Ive got to write a multi-function program and I'm plugging in the
>functions but I keep getting this error from line 40. Im new to this
>and cant find an answer anywhere.


I guess you are...I haven't seen a compiler that doesn't give a clear,
concise message for this type of error. Here's what MSVC 7.1 says:

sam.c(1 : warning C4047: 'function' : 'char' differs in levels of
indirection from 'char [15]'

If this is driving you crazy, you're going to have quite a challenge in
front of you...

>
>Sam
>
>
>#include <stdio.h>
>
>int main (void)
>{
> void top_of_page_info (void);
> void print_item_info (char item_name, double item_price);


item_name is a "string", represented in C as an array of char or a pointer
to char (an as a function parameter, both definitions say the same thing:
that it is a pointer to char). So, picking one:

void print_item_info(const char *item_name, double item_price);

The const is to advertise that the text won't be changed as a result of
calling the function.

>
> char item_name[15], name;
> double item_price;
>
> printf ("\n\nPlease enter the name of the item: ");
> scanf ("%s", & item_name);


Don't need the & there; a naked array name used in this context is
equivalent to taking the address of its first element, so
scanf("%s", item_name);
and
scanf("%s", &item_name[0]);
are both okay (the former being easier to type and the way most folks do
it). The way you did it tends to be allowed by compilers, but I'd use just
the array name.

Now, scanf has lots of problems: you can't have white space in the item
name, if you enter too many characters it'll overrun the buffer...look
into, say, fgets and sscanf together as an alternative to using scanf.

>
> printf ("\n\nPease enter the price of the item: $");
> scanf ("%f", & item_price);
>
> top_of_page_info ();
> print_item_info (item_name, item_price); /***LINE 40***/


This should now work with the fixed declaration/definition of
print_item_info.

>
>return 0;
>
>}
>
>/*******BEGIN TOP_OF_PAGE_INFO
>FUNCTION**************************************/
>
>void top_of_page_info (void)
>{
> printf ("\n\n PROGRAM DESCRIPTION\n");
> printf (" by\n");
> printf (" YOUR NAME HERE\n");
> printf (" Date Written: DDMMMYY\n");
> printf ("------------------------------------------------------\n\n");
>}
>
>/*******BEGIN PRINT_ITEM_INFO
>FUNCTION**************************************/
>
>void print_item_info (char item_name, double item_price)

void print_item_info(const char *item_name, double item_price)

>
>{
> printf ("\n\nThe item name is %s\n", item_name);
> printf ("The item price is %f\n", item_price);
>}
>
>/************************************************** ***********************************************/



BTW, nicely formatted program! It is a real treat to see early efforts
posted so legibly...kudos!

Good luck,
-leor

Leor Zolman
BD Software
(E-Mail Removed)
www.bdsoft.com -- On-Site Training in C/C++, Java, Perl & Unix
C++ users: Download BD Software's free STL Error Message
Decryptor at www.bdsoft.com/tools/stlfilt.html
 
Reply With Quote
 
 
 
 
Jack Klein
Guest
Posts: n/a
 
      03-03-2004
On 2 Mar 2004 19:17:45 -0800, (E-Mail Removed) (spoilsport)
wrote in comp.lang.c:

In addition to all of Leor's comments:

> Ive got to write a multi-function program and I'm plugging in the
> functions but I keep getting this error from line 40. Im new to this
> and cant find an answer anywhere.
>
> Sam
>
>
> #include <stdio.h>
>
> int main (void)
> {
> void top_of_page_info (void);
> void print_item_info (char item_name, double item_price);


Putting function prototypes inside of a function is not illegal, but
it is not good practice.

> char item_name[15], name;
> double item_price;
>
> printf ("\n\nPlease enter the name of the item: ");
> scanf ("%s", & item_name);
>
> printf ("\n\nPease enter the price of the item: $");
> scanf ("%f", & item_price);


You need to use the "%lf" conversion specifier with any of the
*scanf() functions for a double. "%f" is for float.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
 
Reply With Quote
 
Barry Schwarz
Guest
Posts: n/a
 
      03-04-2004
On 2 Mar 2004 19:17:45 -0800, (E-Mail Removed) (spoilsport)
wrote:

>Ive got to write a multi-function program and I'm plugging in the
>functions but I keep getting this error from line 40. Im new to this
>and cant find an answer anywhere.
>
>Sam
>
>
>#include <stdio.h>
>
>int main (void)
>{
> void top_of_page_info (void);
> void print_item_info (char item_name, double item_price);


You apparently expect the compiler to recognize that item_name is an
array and therefore intended the first parameter to be an array of
char. It doesn't work that way. In a prototype, the names of the
parameters are irrelevant. The only thing that counts is the
parameter type. In this case, the first parameter is a char.

>
> char item_name[15], name;
> double item_price;
>
> printf ("\n\nPlease enter the name of the item: ");
> scanf ("%s", & item_name);
>
> printf ("\n\nPease enter the price of the item: $");
> scanf ("%f", & item_price);
>
> top_of_page_info ();
> print_item_info (item_name, item_price); /***LINE 40***/


Here you call the function but the first argument is an array of char.
Your compiler is correctly telling you that the first argument is
incompatible with the expected type. That is, there is no way for the
compiler to automatically convert the array to a char.

If you replace the parameter in the prototype with either
char[]
or
char item_name[]
(the 15 is optional and irrelevant in either case), your argument will
match the type of the parameter and the compiler will not complain.

>
>return 0;
>
>}
>
>/*******BEGIN TOP_OF_PAGE_INFO
>FUNCTION**************************************/
>
>void top_of_page_info (void)
>{
> printf ("\n\n PROGRAM DESCRIPTION\n");
> printf (" by\n");
> printf (" YOUR NAME HERE\n");
> printf (" Date Written: DDMMMYY\n");
> printf ("------------------------------------------------------\n\n");
>}
>
>/*******BEGIN PRINT_ITEM_INFO
>FUNCTION**************************************/
>
>void print_item_info (char item_name, double item_price)


You must also change the parameter in the function definition.

>
>{
> printf ("\n\nThe item name is %s\n", item_name);


Obviously, you really wanted to receive an array and not just a single
char.

> printf ("The item price is %f\n", item_price);


Use %lf for doubles.

>}
>
>/************************************************** ***********************************************/




<<Remove the del for email>>
 
Reply With Quote
 
Leor Zolman
Guest
Posts: n/a
 
      03-04-2004
On 4 Mar 2004 02:42:35 GMT, Barry Schwarz <(E-Mail Removed)> wrote:

>
>> printf ("The item price is %f\n", item_price);

>
>Use %lf for doubles.


I think it is the scanf that needs the %lf, not the printf.
I didn't even catch that error in the scanf call, Jack did (good eyes,
Jack).
-leor

>
>>}
>>
>>/************************************************** ***********************************************/

>
>
>
><<Remove the del for email>>


Leor Zolman
BD Software
(E-Mail Removed)
www.bdsoft.com -- On-Site Training in C/C++, Java, Perl & Unix
C++ users: Download BD Software's free STL Error Message
Decryptor at www.bdsoft.com/tools/stlfilt.html
 
Reply With Quote
 
BartenderSam
Guest
Posts: n/a
 
      03-04-2004
FYI, thanks for the help fellows, I was able to plub the other 3 functions
in and it ran like a champ.

---
Sam


 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
OT: Windows XP: Going, going ... gone? OTHMAN MCSE 2 03-25-2008 04:45 PM
XP search going all lame on me..explorer going crazy when trying tosearch. fotoobscura Computer Support 8 01-12-2008 01:09 AM
Firefighters at the site of WTC7 "Move away the building is going to blow up, get back the building is going to blow up." Midex Python 24 05-07-2007 04:23 AM
floppy drive just keeps on going and going and ... Weylon Bulloch Computer Support 1 09-07-2003 07:17 AM
VPN going up but traffic going one way PLP Cisco 1 07-11-2003 08:28 AM



Advertisments