I'm only a few days new to C++ and I'm working on a program where I need to give commands and a coresponding function is called. At the moment the code uses the given string and goes through "if string = functionname then..." but I am trying to be more efficient. What I want to do is give the function name, then it searches through a list of function names, when it matches up it calls that function. I hope my coding isn't too confusing. I'm new at this so go easy, any help would be apreciated.
thanks, WhiteHatHacker
code so far:
#include <iostream>
#include <string>
using std::string;
string GetName(string enterYourName); //declares function Login
string GetPass(string enteryourPass);
string GetCmd(string& CMD, string& yourName);
string CheckCmd(string& CMD, string& yourName);
int main()
{
string CMD;
string yourName; //declares userName as string
string yourPass; //declares yourPass as string
yourName = GetName(yourName); //Sends userName to Login to have a value returned to it
yourPass = GetPass(yourPass);
std::cout<<"\n"<<"You are now logged on as:"<<yourName;
std::cout<<"\n"<<yourName<<":";
CMD = GetCmd(CMD, yourName);
return 0;
}
string GetName(string yourName) //Beginning of function
{
string enterYourName;
std::cout<<"Initializing connection..."<<"\n";
std::cout<<"Main screen login, please provide a user name and password."<<"\n";
std::cout<<"Username:";
std::cin>>enterYourName;
return enterYourName;
}
string GetPass(string yourPass)
{
string enterYourPass;
std::cout<<"Password:";
std::cin>>enterYourPass;
return enterYourPass;
}
string GetCmd(string& CMD, string& yourName)
{
std::cin>>CMD;
CMD = CheckCmd(CMD, yourName);
return CMD;
}
string CheckCmd(string& CMD, string& yourName)
{
if (CMD == "god")
std::cout<<"You are god.";
else
std::cout<<"invalid command.";
std::cout<<"\n"<<yourName<<":";
GetCmd(CMD, yourName);
return CMD, yourName;
}