473,320 Members | 1,976 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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

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

/************************************************** ***********************************************/
Nov 14 '05 #1
5 4520
On 2 Mar 2004 19:17:45 -0800, ba**********@earthlink.net (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(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
le**@bdsoft.com
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
Nov 14 '05 #2
On 2 Mar 2004 19:17:45 -0800, ba**********@earthlink.net (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
Nov 14 '05 #3
On 2 Mar 2004 19:17:45 -0800, ba**********@earthlink.net (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>>
Nov 14 '05 #4
On 4 Mar 2004 02:42:35 GMT, Barry Schwarz <sc******@deloz.net> 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
le**@bdsoft.com
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
Nov 14 '05 #5
FYI, thanks for the help fellows, I was able to plub the other 3 functions
in and it ran like a champ.

---
Sam
Nov 14 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

30
by: Tim Johansson | last post by:
I'm new to C++, and tried to start making a script that will shuffle an array. Can someone please tell me what's wrong? #include <iostream.h> #include <string.h> int main () {...
5
by: Vijai Kalyan | last post by:
Hello, I have come back to C++ after a couple of years with Java so I am quite rusty and this question may seem poor: My platform is Windows XP with MSVC 7.1. I have a class with a...
18
by: ben.carbery | last post by:
Hi, I have just written a simple program to get me started in C that calculates the number of days since your birthdate. One thing that confuses me about the program (even though it works) is...
3
by: philwozza | last post by:
Im trying to implement a THREAD class that encapsulates a posix thread. Here is an outline of my THREAD class. class THREAD { public: // returns 1 if thread sucessfully started int...
11
by: Martin Jørgensen | last post by:
Hi, I'm using this alloc_mem-function: - - - - - - - - - - - - - - - - - - - - - - - - void *alloc_mem (size_t num_elems, size_t elem_size, char *filename, int line, size_t *total_mem) {
6
by: thomas.luce | last post by:
Okay, I have been programming for a long time, and am getting back into C after about a 4 year break from it. Below is some code that won't compile for the life of me, and it is driving me crazy!...
37
by: Zhiv Kurilka | last post by:
Hi, I have a text file with following content: "((^)|(.* +))§§§§§§§§" if I read it with: k=System.IO.StreamReader( "file.txt",System.Text.Encoding.ASCII); k.readtotheend()
9
by: Frederic Mayot | last post by:
Hi, Can someone give me an explanation why the following code produces a bug. I think it's related to the compiler and that the first line is not OK. I have no pb with gcc 3.4 but the bug appeared...
7
by: somenath | last post by:
Hi All, I am trying to undestand "Type Conversions" from K&R book.I am not able to understand the bellow mentioned text "Conversion rules are more complicated when unsigned operands are...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.