1
// Function displays course information
// instructor defaults to Staff
// enrollment defualts to 30
// main() demonstrates function can be called three ways
#include<iostream>
using namespace std;

int main()
{
   void displayCourseInfo(char, char* = "Staff", int = 30);
displayCourseInfo("ENG101");
displayCourseInfo("PSY151", "Bossert");
displayCourseInfo("CIS200", "Edwards", 24);
return 0;
 }
 void displayCourseInfo(char course, char* instructor, int enrollment)
 {  cout << course <<  " taught by "  << instructor <<
  " enrollment " << enrollment << endl;
}

When I try to run this code I get this error message. It says that I cannot convert *const char to char. Thank you for your time and help.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • 7
    Welcome to Stack Overflow! Sounds like you could use a [good C++ book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – NathanOliver Feb 28 '18 at 15:23
  • 4
    Please don't post pictures of text, post the text. – molbdnilo Feb 28 '18 at 15:24
  • 1
    What's unclear about the error message? String literals are of type `const char*`, which are incompatible with a `char*` type. – Algirdas Preidžius Feb 28 '18 at 15:24
  • 3
    Hint: String literals in C++ *decay* to `const char*` function parameter types. A C compiler permits decay to a `char*` but C++ fixed that mess. Then have a look at `std::string`. – Bathsheba Feb 28 '18 at 15:24
  • 1
    And you can't convert either `char*` or `const char*` to `char`. Both your parameters should be `const char*` (or `const std::string&`). – molbdnilo Feb 28 '18 at 15:26
  • 1
    aside: These aren't modules; they're functions. Of all the terminology you need to get write - I'd put that high up there. – UKMonkey Feb 28 '18 at 15:34
  • why is this post up-voted? Besides the fact that the problem is a lack of basic language understanding (which in itself it not frowned upon, we all had to start somewhere, but SO is not the place to post about it) it is not well formulated, code indentation is bad and it contains external picture to error message... – bolov Feb 28 '18 at 15:43

2 Answers2

2

String literals in C++ (opposite to C) have types of constant character arrays. For example the string literal "Staff" has the type const char [6].

Used in expressions array designators with rare exceptions are converted to pointers to their first elements. So the string literal "Staff" used as an argument is converted to a pointer of the type const char *.

The first parameter of the function displayCourseInfo is declared as having the type char while you are trying to pass a string literal as an argument.

A valid program can look the following way

// Function displays course information
// instructor defaults to Staff
// enrollment defualts to 30
// main() demonstrates function can be called three ways
#include<iostream>
using namespace std;

int main()
{
    void displayCourseInfo( const char *, const char * = "Staff", int = 30 );

    displayCourseInfo("ENG101");
    displayCourseInfo("PSY151", "Bossert");
    displayCourseInfo("CIS200", "Edwards", 24);

    return 0;
}

void displayCourseInfo( const char *course, const char *instructor, int enrollment)
{  
    cout << course <<  " taught by "  << instructor 
         << " enrollment " << enrollment << endl;
}

Its output is

ENG101 taught by Staff enrollment 30
PSY151 taught by Bossert enrollment 30
CIS200 taught by Edwards enrollment 24
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
1

This happens because "test in quotes" is by default const char*.
Values with const in their declaration cannot be changed.
You can't pass const variable to function that takes non-const parameters.

You could just make function's parameters const:

#include<iostream>

using namespace std;

void displayCourseInfo(const char *);
void displayCourseInfo(const char *, const char *);
void displayCourseInfo(const char *, const char *, const int);

int main() {
    displayCourseInfo("ENG101");
    displayCourseInfo("PSY151", "Bossert");
    displayCourseInfo("CIS200", "Edwards", 24);
    return 0;
}

void displayCourseInfo(const char *course, const char *instructor) {
    cout << course << " taught by " << instructor <<
         " enrollment " << 30 << endl;
}

void displayCourseInfo(const char *course) {
    cout << course << " taught by " << "Staff" <<
         " enrollment " << 30 << endl;
}

void displayCourseInfo(const char *course, const char *instructor, const int enrollment) {

    cout << course << " taught by " << instructor <<
         " enrollment " << enrollment << endl;
}
J K
  • 632
  • 7
  • 24