#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.