Hey ya'll, this is my first time working with linked list and I have to create a menu for the user to mutate their list. The user is only allowed to enter primary colors(red,yellow,blue). I'm having some trouble with one of my function calls and I can't seem to fix what's wrong with the function. Any hints would be appreciated.
Heres the error the compiler throws:
error: no matching function for call to ‘ColorsList::appendNode(std::string (&)())’
color.appendNode(validateUserInput);
#include <stdio.h>
#include <iostream>
#include <string>
#include "ColorsList.h"
usingnamespace std;
//Prototypes
int get_user_choice();
void display_menu();
string validateUserInput();
int main()
{
//ColorsList test;
//test.appendNode("Red");
//cout<<"node appended";
int user_input;
ColorsList color;
do{
display_menu();
user_input = get_user_choice();
switch(user_input)
{
case 1:
color.appendNode(validateUserInput);
break;
case 2:
cout<<"InsertNode method";
break;
case 3:
cout<<"deletenode method";
break;
case 4:
cout<<"printList method";
break;
case 5:
cout<<"ReverseList method";
break;
case 6:
cout<< "SearchList method";
break;
case 7:
cout<<"Exit Mehtod";
break;
}
}while(user_input != 7);
return 0;
}
int get_user_choice()
{
//will hold the user's choice
int user_choice = 0;
//reading in user choice
cin >> user_choice;
return user_choice;
}
void display_menu()
{
cout<< "\n"<<"Please enter a value (1-7)\n"<<"1. Append\n" << "2. Insert\n" << "3. Delete\n" <<
"4. Print\n"<< "5. Reverse\n" <<"6. Search\n"<<"7. Exit\n"<<endl;
}
//Method used for high level validation
string validateUserInput()
{
string user_input;
cout<<"Please enter a color you'd like to add to your list\n";
cout<<"Your choices are Red, Yellow, and Blue\n";
getline(cin, user_input);
for(int i =0; i < user_input.length(); i++)
{
tolower(user_input[i]);
}
while(user_input != "red" && user_input != "yellow" && user_input != "blue")
{
cout<< user_input<< " is not a color that is allowed"<<endl;
}
return user_input;
}
#ifndef COLORSLIST_H
#define COLORSLIST_H
#include<string>
usingnamespace std;
class ColorsList
{
private:
//declare structure for list
struct ListNode
{
//The value that is stored in node
string colorType;
//Pointer which points to next node
struct ListNode *next;
};
//Creating head pointer for List
ListNode *head;
public:
//Class constructor
//setting pointer to head of list to nullptr
ColorsList()
{ head = nullptr; }
//Destructor
~ColorsList()
{
}
//Linked list operations
void appendNode(string); //requires validation for input in main
void insertNode(); //requires validation for input in main
void deleteNode(); //requires validation for input in main
void printNode();
void printReverse();
void searchNode(); //requires validation for input in main
void exitMenu();
};
#endif
#include <iostream>
#include <iomanip>
#include <string>
#include "ColorsList.h"
usingnamespace std;
void ColorsList::appendNode(string color)
{
//Will point to a new node
ListNode *newNode;
//Pointer used to traverse though list
ListNode *nodePtr;
//create a new node
newNode = new ListNode;
//Mutator validation
if(color != "red" && color != "yellow" && color !="blue")
{
cout<<"Invalid color. Enter a primary color\n. Ex: Red, Yellow, or Blue\n";
exit(EXIT_FAILURE);
}
else
{
//strore color in new node
newNode-> colorType = color;
}
//will point to last node of list
newNode-> next=nullptr;
//If there are no nodes in list
//Make the new node the first node in list
if(!head)
{
//make newNode the first node in list
head = newNode;
}
else
{
//Set nodePtr to the beginning of list
nodePtr = head;
//While we haven't reached the end of the list
while (nodePtr->next)
{
//set nodePtr to the next node in list
nodePtr = nodePtr->next;
//set newNode to the node nodePtr is pointing to
nodePtr->next = newNode;
}
}
}
validateUserInput is a function. You want to pass the return value from that function, so you need to call the function. So, add the () needed to call the function.
Line 27 should be color.appendNode(validateUserInput());
By the way, when you encounter an error, go ahead an paste the entire error message here, including file names, line numbers, etc. It will help us locate the problem more quickly.
Edit: Your prompt for colors says "Red, Yellow, and Blue", but your code accepts "red", "yellow" and "blue". Capitalization matters, and your code is inconsistent. You can use tolower() to change characters to lower case, but why not just prompt for "red, yellow, and blue" to get started?
Ah, thanks for the correction. I did use a for loop that converts the user string into lowercase in my validateUserInput method. I'll be sure to fix the promt as well.