-1

The problem is my error output keeps stating "Expression must have a constant value" four times.

I've used this site and tried allocating memory and deleting it and that did nothing whatsoever. I changed "mystr.length();" to a number and while that allowed the program to run, it provided invalid results.

char letter = 'Y';
int position = 0;
string productNames[7] =
{ "Pen", "Paper", "Computer", "Pepsi", "Coke", "Book", "Scanner" };

int prizeOfproducts[7] = { 10, 2, 5000, 50, 45, 400, 2000 };
while (letter == 'Y')
{
    string mystr = "";
    cout << "enter the product name you want to search : ";
    cin >> mystr;

    int n = mystr.length();
    char char_array[n + 1];
    strcpy(char_array, mystr.c_str());
    bool flag = false;
    for (int i = 0; i < 7; i++)
    {
        int m = productNames[i].length();
        char char_arrayOrig[m + 1];
        strcpy(char_arrayOrig, productNames[i].c_str());
        if (strstr(char_arrayOrig, char_array) == NULL)
        {
            flag = false;
        }
        else
        {
            flag = true;
            position = i;
            break;
        }
    }
    if (!flag)
    {
        cout <<
            "entered product not found in the product list . Do you want to search again Y/N ? : ";

        cin >> letter;
    }

I simply want the program to work and not stop because of errors, however, I want the results to be accurate.

tripleee
  • 175,061
  • 34
  • 275
  • 318
  • 1
    `char char_array[n + 1];` -- Invalid C++. Use `std::vector char_array(n + 1);`. Also, why are you not using `std::string` throughout the entire program? What's the reason for trying to use char arrays, `strcpy`, etc.? – PaulMcKenzie May 17 '19 at 05:41
  • What PaulMcKenize said. But also have a look if this may help you: https://stackoverflow.com/questions/2340281/check-if-a-string-contains-a-string-in-c –  May 17 '19 at 05:44
  • 1
    Welcome to Stack Overflow! Please do not vandalize your posts. By posting on the Stack Exchange network, you've granted a non-revocable right for SE to distribute that content (under the [CC BY-SA 3.0 license](https://creativecommons.org/licenses/by-sa/3.0/)). By SE policy, any vandalism will be reverted. – Michael Dodd May 17 '19 at 08:26
  • 1
    And it's not just about helping yourself if you found the answer on Reddit, it's about helping others that may encounter this issue in the future. – Michael Dodd May 17 '19 at 08:26
  • 1
    @MichaelDodd I didn't consider that. Thank you for informing me. I shall never make this mistake again. – Hidden Song May 17 '19 at 09:09

1 Answers1

1

There is no need to mix C++-style std::string and C-style char arrays. If you want to use a dynamic size string - std::string is what you need. If you try to use char[] and don't know it's size in advance - you probably don't need C++...

Try to consider std::string or std::vector everywhere where you use char[]. Here is as example of a straightforward solution of your problem:

//char char_array[n + 1];
std::vector<char> char_array(n + 1);
Dmitry Kuzminov
  • 6,180
  • 6
  • 18
  • 40