Hey there, I'm having some trouble with a function that is supposed to prompt a user to enter the name of a customer they would like to edit. I have the customer names stored in an array. If the user input matches one of the customer names in the array, I should be prompted to edit the customers information.
void customerEdit(string customerName[],string boatName[],float contract[],float PTD[])
{
string userInput;
float floatInput;
size_t contractArrSize = sizeof(contract)/sizeof(contract[0]);
size_t ptdArrSize = sizeof(PTD)/sizeof(PTD[0]);
int customerArrSize = sizeof(customerName)/sizeof(customerName[0]);
int boatNameArrSize = sizeof(boatName)/sizeof(boatName[0]);
cout<<"Enter customer name you would like to edit\n";
//read in user Input
getline(cin,userInput);
for(int i = 0; i<customerArrSize; i++)
{
if(userInput == customerName[i])
{
cout<<"which field would you like to edit(Options are: boat name, contract, PTD). \n";
getline(cin,userInput);
//transform(userInput.begin(),userInput.end(),userInput.begin,::tolower);
for(int i =0; i <userInput.length(); i++)
{
userInput[i]=tolower(userInput[i]);
}
if(userInput == "boat")
{
cout<<"Enter a new boat name\n";
getline(cin,userInput);
if (userInput.length() <= 0 || userInput.length() > 15)
{
cout<< "Customer name cannot be blank and must be longer than 15 chars.\n";
}
else
{
for(int i =0; i<boatNameArrSize; i++)
{
boatName[i]= userInput;
}
}
}
elseif(userInput == "contract")
{
cout<<"enter new contract amount\n";
cin>>floatInput;
cin.ignore();
//verify if input is !<0
if (floatInput < 0 )
{
cout<< "Value entered cannot be less than 0.\n";
}
else
{
//swap element for user input
for(int i=0; i<contractArrSize; i++)
{
contract[i] = floatInput;
}
}
}
elseif(userInput == "ptd")
{
cout<<"Enter new PTD amount\n";
cin>>floatInput;
cin.ignore();
if (floatInput < 0 )
{
cout<< "Value entered cannot be less than 0.\n";
}
else
{
//swap element for user input
for(int i=0; i<ptdArrSize; i++)
{
PTD[i] = floatInput;
}
}
}
}
//if user input != customerName[i]
else
{
cout<<"User not found.\n";
}
}
}
I believe the problem is with the first control statement in the first for loop, since "User not found" is displayed after the search. I tried using the debugger, but I'm a bit too new to fully comprehend it. I am using onlinegdb IDE if that helps.
size_t contractArrSize = sizeof(contract)/sizeof(contract[0]);
size_t ptdArrSize = sizeof(PTD)/sizeof(PTD[0]);
int customerArrSize = sizeof(customerName)/sizeof(customerName[0]);
int boatNameArrSize = sizeof(boatName)/sizeof(boatName[0]);
Yeah, these don't work on array parameters.
The sizeof thing only works on arrays that are in scope. As soon as you pass an array to a function, all you're left with is a pointer. The size information has gone.
You need to pass the actual size as an additional parameter.
void customerEdit(string customerName[],string boatName[],float contract[],float PTD[],int size)
{
string userInput;
float floatInput;
cout<<"Enter customer name you would like to edit\n";
//read in user Input
getline(cin,userInput);
for(int i = 0; i<size; i++)
{
//if user input != customerName[i]
if(userInput != customerName[i])
{
cout<<"User not found.\n";
}
elseif(userInput == customerName[i])
{
cout<<"which field would you like to edit(Options are: boat name, contract, PTD). \n";
getline(cin,userInput);
//transform(userInput.begin(),userInput.end(),userInput.begin,::tolower);
for(int i =0; i <userInput.length(); i++)
{
userInput[i]=tolower(userInput[i]);
}
if(userInput == "boat")
{
cout<<"Enter a new boat name\n";
getline(cin,userInput);
if (userInput.length() <= 0 || userInput.length() > 15)
{
cout<< "Customer name cannot be blank and must be longer than 15 chars.\n";
}
else
{
for(int i =0; i<size; i++)
{
boatName[i]= userInput;
}
}
}
elseif(userInput == "contract")
{
cout<<"enter new contract amount\n";
cin>>floatInput;
cin.ignore();
//verify if input is !<0
if (floatInput < 0 )
{
cout<< "Value entered cannot be less than 0.\n";
}
else
{
//swap element for user input
for(int i=0; i<size; i++)
{
contract[i] = floatInput;
}
}
}
elseif(userInput == "ptd")
{
cout<<"Enter new PTD amount\n";
cin>>floatInput;
cin.ignore();
if (floatInput < 0 )
{
cout<< "Value entered cannot be less than 0.\n";
}
else
{
//swap element for user input
for(int i=0; i<size; i++)
{
PTD[i] = floatInput;
}
}
}
}
}
}
I'm still having some issues with the name comparison. It prints out both that it did not find a match and the prompt for the next step. After the prompt for the next, nothing happens, the menu I created is prompted again for the user(I have the menu in main). If I'm understanding the debugger correctly, it sees that the user input does not match what it is the first element, it spits out that there's not match and goes back into the loop since the array is still full.
void customerEdit(string customerName[], string boatName[], float contract[], float PTD[], int size)
{
string userInput;
float floatInput;
cout << "Enter customer name you would like to edit\n";
//read in user Input
getline(cin, userInput);
for (int i = 0; i < size; ++i) {
if (userInput == customerName[i]) {
cout << "which field would you like to edit(Options are: boat name, contract, PTD). \n";
getline(cin, userInput);
//transform(userInput.begin(),userInput.end(),userInput.begin,::tolower);
for (int i = 0; i < userInput.length(); ++i)
userInput[i] = tolower(userInput[i]);
if (userInput == "boat") {
cout << "Enter a new boat name\n";
getline(cin, userInput);
if (userInput.length() <= 0 || userInput.length() > 15)
cout << "Boat name cannot be blank and must be no longer than 15 chars.\n";
elsefor (int i = 0; i < size; ++i)
boatName[i] = userInput;
} elseif (userInput == "contract") {
cout << "Enter new contract amount\n";
cin >> floatInput;
cin.ignore();
//verify if input is !<0
if (floatInput < 0)
cout << "Value entered cannot be less than 0.\n";
else
//swap element for user input
for (int i = 0; i < size; i++)
contract[i] = floatInput;
} elseif (userInput == "ptd") {
cout << "Enter new PTD amount\n";
cin >> floatInput;
cin.ignore();
if (floatInput < 0)
cout << "Value entered cannot be less than 0.\n";
else
//swap element for user input
for (int i = 0; i < size; i++)
PTD[i] = floatInput;
}
return;
}
}
cout << "User not found.\n";
}