#include<iostream>
#include<iomanip>
#include<string>
#include<cctype>
#include<stdlib.h>
using namespace std;

struct Product
{
    string PLU_Code;

    string Fruit_name;

    int SalesType;

    double  UnitPrice;

    double InventoryLevel;

};

const int SIZE = 100;

int populateInventory(Product *[], int);
bool validatePLU(const string &);

int main()
{
    /*
    (Normal way of allocating
    )
    Product *NumArray;
    NumArray = new Product[SIZE];

    */

    Product *NumArr[SIZE];

    populateInventory(NumArr, SIZE);

    return 0;
}

int populateInventory(Product * arrProd[], int max_size)
{

    //cout << "hello world" << endl;
    string PLU;
    cout << "Enter PLU, zero if done: ";
    //cin >> PLU;
    getline(cin, PLU);
    cout << PLU << endl;

    while (!validatePLU(PLU))
    {
        cout << "Enter PLU, zero if done: ";
        //cin >> PLU;
        getline(cin, PLU);
    }

    return 0;
}

bool validatePLU(const string &s)
{
    int length = 0;
    length = s.length();

    for (int i = 0; i < length; i++)
    {
        if (isalpha(s.at(i)||isdigit(s.at(i))))
        {
            return true;
        }

    }

    return true;
}

I am trying to determine if the string entered by the user contains only alphabets and numbers. if it contains any characters like !,@,#,$,%,^, etc, it should again ask for the user to input the string but for some reason it asks the user to again enter the string even if the user enters a correct string. Please tell me where I am going wrong.

The validatePLU function appears to return true regardless of the string. I see no "return false" anywhere in it. Which makes your statement here seem odd:

for some reason it asks the user to again enter the string even if the user enters a correct string. Please tell me where I am going wrong.

Seems to me it should NEVER ask for a string to be entered again since the function always returns true/valid even when it should return false/invalid. You are describing the opposite problem.

Note there is also an isalnum function.
I find your design strategy wrong. If you would test in the input and allow only alphanumeric chars to go through, you would know your PLU string is correct and not have to validate it any more.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.