We help IT Professionals succeed at work.

cannot convert 'int' to 'const char*' in function dis_isbn[rec*]

d_terano
d_terano asked
on
hi there, can anyone help me with

1. the line of code with the error message read:
cannot convert 'int' to 'const char*' in function dis_isbn[rec*]

2.how do i make the "BookArray[i].category" an enumerated type of {SF-Science Fiction,HR-Horror,RM-Romance}  

thanks,
d_terano
----------------------------------------------------------
#include<stdio.h>
#include<string.h>
#include<conio.h>

void menu (struct rec BookArray[]);
void add (struct rec BookArray[]);
void dis_all (struct rec BookArray[]);
void dis_isbn (struct rec BookArray[]);
void dis_author(struct rec BookArray[]);
void del (struct rec BookArray[]);

struct rec {
                         char isbn[10];
                         char title[30];
                         char author[30];
                         float price;
                         //enum category {SF-Science Fiction,HR-Horror,RM-Romance};
                         int tarikh;  //reconfirm this part
                        };
main()
{
 struct rec BookArray[10];

 //this part to initialize BookArray to empty
 int i;
 for (i=0;i<11;i++)
 {
  strcpy(BookArray[i].isbn," ");
  strcpy(BookArray[i].title," ");
  strcpy(BookArray[i].author," ");
  BookArray[i].price = 0.0;
  //BookArray[i].category = 0.0;
  BookArray[i].tarikh = 0;
 }

 menu(BookArray);

 return 0;
}

///////////////////////////////////////////////////////////////////////////
void menu(struct rec BookArray[10])
{
  int selection;
  clrscr();
  printf("\nMain Menu\n");
  printf("\n 1. Add book record\n");
  printf("\n 2. Display all book records\n");
  printf("\n 3. Search record by ISBN\n");
  printf("\n 4. Search record by Author\n");
  printf("\n 5. Delete record by Title\n");
  printf("\n Please Enter your selection:");
  scanf("%d",&selection);

  switch(selection)
  {
      case 1: add (BookArray);  break;
      case 2: dis_all (BookArray);  break;
      case 3: dis_isbn (BookArray); break;
      case 4: dis_author(BookArray); break;
      case 5: del (BookArray); break;
      default: { printf("Invalid Input, Try again");
                          clrscr();
                          menu(BookArray);
                        }
  }
}

///////////////////////////////////////////////////////////////////////////
void add (struct rec BookArray[10])
{
 int i=0;
 char ans = 'y';
 clrscr();
 while (ans == 'y')
 {
  i++;
  clrscr();
  printf("\nAdd Book Record\n");
  printf("\nRecord no: %d",i);
  printf("\nPlease enter the following information\n");
  printf("\nISBN: ");
  scanf("%s",&BookArray[i].isbn);
  printf("\nTitle: ");
  scanf("%s",&BookArray[i].title);
  printf("\nAuthor: ");
  scanf("%s",&BookArray[i].author);
  printf("\nPrice: ");
  scanf("%f",&BookArray[i].price);
  //printf("\nCategory: ");
  //scanf("%s",&BookArray[i].category);
  printf("\nPublished Date: ");
  scanf("&d",&BookArray[i].tarikh);
  printf("\nDo you want to continue?(y/n) ");
  scanf("%s",&ans);
 }
  menu(BookArray);
}
///////////////////////////////////////////////////////////////////////////
void dis_all (struct rec BookArray[10])
{
 int i=0;
 clrscr();
 printf("\All Records\n");
 while (i < 10)
 {
  i++;
  printf("\nRecord no: %d",i);
  printf("\nISBN: %s",BookArray[i].isbn);
  printf("\nTitle: %s",BookArray[i].title);
  printf("\nAuthor: %s",BookArray[i].author);
  printf("\nPrice: %4.2f",BookArray[i].price);
  //printf("\nCategory: %s",BookArray[i].category);
  printf("\nPublished Date: %d\n",BookArray[i].tarikh);

 }
      printf("\nPress a key to go back to menu: ");
      getch();
      menu(BookArray);

}
///////////////////////////////////////////////////////////////////////////
void dis_isbn (struct rec BookArray[10])
{
 char search[10];
 int i;
 clrscr();
 printf("\nSearch Record by ISBN\n");

 printf("\nPlease Enter ISBN to search for: ");
 scanf("%s",&search);

  for(i=0;i<11;i++)
 {
  if (strcmp(search[i],BookArray[i].isbn))
      printf("\nRecord not found!");
  else
      {
       printf("\nISBN: %s",BookArray[i].isbn);
       printf("\nTitle: %s",BookArray[i].title);
       printf("\nAuthor: %s",BookArray[i].author);
       printf("\nPrice: %4.2f",BookArray[i].price);
       //printf("\nCategory: %s",BookArray[i].category);
       printf("\nPublished Date: %d\n",BookArray[i].tarikh);
      }
 }
 printf("\nPress a key to go back to menu: ");
 getch();
 menu(BookArray);

}
///////////////////////////////////////////////////////////////////////////
void dis_author(struct rec BookArray[10])
{
 clrscr();
 printf("\Search Record by Author");
}
////////////////////////////////////////////////////////////////////////////
void del (struct rec BookArray[10])
{
 clrscr();
 printf("\Delete Record by Title");
}
Comment
Watch Question

Commented:
1) replace the line with
if (strcmp(search,BookArray[i].isbn))
because you want to compare the entire string stored in search, rather than just the first element of it.  You do realise that the logic of this loop means that it will display results for all ten books.

2) enum categories {SCIFI,HORROR,ROMANCE};
   categories category;

will work.  Read up on enumerations, though - you might be mistaking what they're for.

Also, in lots of places you use the \ within strings incorrectly.  eg

void del (struct rec BookArray[10])
{
clrscr();
printf("\Delete Record by Title");
}

will not compile, because there is no \D character escape sequence.  I guess you mean ..."\nDelete ".

Bear in mind that for production code, this kind of thing would be better done in C++ with std::string and iostreams.  What you have here is C code.

Also, thanks for the comment in the other question.  :-)

Commented:
IainHere is right...
enum variables are not character arrays to accept from input functions.
better you display another menu for category using printf statemnts,
Ex:

1.S.F - Science Fiction
2.HR - Horror
3.RM - Romance
Enter The No : 1

so if input from the user is 1 then you can do something like,
BookArray[i].category=SF;



Author

Commented:
hey there "lainhere",

   good to hear from you...hope we're back in good terms hey...

   thanks a lot for your help regarding this question, it works fine now... except for the enum part and the date part...

   doesn't the enum thing work like a pull down menu (or something like that) where users are required to select one of the three selection??

   one more question,at the add function, why does my "published date: " (BookArray[i].tarikh) doesn't ask for an input? it jumps straight to the next line of code?
And in the question, it ask for date format in "nested date structure type dd/mm/yy" what does that mean?

thanks again for your help, hope you could get back to me with this date and enum thing...

i also would like to thank "srithi" for some additional explaination. the prob is that the question require the  book category part to be an enumerated type of {SF-Science Fiction,HR-Horror,RM-Romance} how do you do that..
 
regard,
d_terano

Commented:
You're mistaken about the use of enum - explanation from MSDN library
<MSDN>
The enum keyword specifies an enumerated type.

An enumerated type is a user-defined type consisting of a set of named constants called enumerators. By default, the first enumerator has a value of 0, and each successive enumerator is one larger than the value of the previous one, unless you explicitly specify a value for a particular enumerator. Enumerators needn?t have unique values. The name of each enumerator is treated as a constant and must be unique within the scope where the enum is defined. An enumerator can be promoted to an integer value. However, converting an integer to an enumerator requires an explicit cast, and the results are not defined.
</MSDN>

You'll like the error with the published date code - you've typed &d instead of %d for the format specifier.

>>date format in "nested date structure type dd/mm/yy"
would mean creating a new struct called date or something, with an int member for day, year and month.  You would then declare the publishedDate member of the rec struct as a date struct.  You'll have to alter the bit of code you just fixed for the date entry though.  Either accept "31/12/2001" as a string input and parse it into the three parts (good way) or have the user enter each part separately

>>Enter Month: 12
>>Enter Day: 31
>>Enter Year: 2001

Clunky, but probably easier for you to code right.

Getting the enum thing to work is similar - you'll have to accept a code, say "HR" as a string, and then map that to the enum's value.  In pseudo-code:

if (Input String == "HR") then
   category = HORROR;

A widely used alternative (DO NOT DO THIS) is to accept a number - 1 for HORROR, 2 for SCIFI of whatever, and have the enum set up in the same way. Then cast from the int to the enum.  See the final couple of sentences in the MSDN snippet above for why this is bad.

Author

Commented:

Author

Commented:
thanks again for your help...

regards,
d_terano