0

I am getting error : ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings] Can anyone please help me in resolving this error ??? Here is my code......

#include<iostream>
#include<string.h>
#include<conio.h>
#include<ctype.h>
using namespace std;
void main()
{
    char *ptr[4] = {"books","television","computer","sports"};
    char str[10];
    cout << "Enter your favourite liesure pursuit : ";
    cin >> str;
    for(int a=0;a<4;a++)
    {
        if(!strcmp(str,ptr[a]))
        {
            cout << "Your favourite pursuit is available here \n";
        }
        else
        {
            cout << "\n Yopr favourite pursuit isn't available here \n";
        }
        getch();
    }
    
}
tadman
  • 208,517
  • 23
  • 234
  • 262

1 Answers1

3

In C++ a string literal is typed as const char* because modifying them leads to undefined behaviour.

As such, you need those const, like this:

const *ptr[] = { ... };

Also consider using std::vector and std::string since this is C++. When you use C++ instead of fancy C you get code like this:

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>

int main() {
  // Easily compose an extensible list of categories
  std::vector<std::string> categories = { "books", "television", "computers", "sports" };

  std::string input; // Use a string for input
  std::cin >> input; // No more buffer overflows!

  // Find an entry in the list
  if (std::find(categories.begin(), categories.end(), input) != categories.end()) {
    std::cout << "Found" << std::endl;
  }
  else {
    std::cout << "Not Found" << std::endl;
  }

  return 0;
}
tadman
  • 208,517
  • 23
  • 234
  • 262