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

S

spoilsport

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);
}

/*************************************************************************************************/
 
L

Leor Zolman

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(18) : 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 address 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
 
J

Jack Klein

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.
 
B

Barry Schwarz

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>>
 
B

BartenderSam

FYI, thanks for the help fellows, I was able to plub the other 3 functions
in and it ran like a champ.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,049
Latest member
Allen00Reed

Latest Threads

Top