Help with string array
Feb 17, 2013 at 9:46am UTC
I am trying to make my program detect a user inputed string from a list of strings stored in an array. Then respond to it in a certain way using an if statement.
This is my first time using C++.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
#include <iostream>
#include <string>
using namespace std;
int main(){
string input;
// Command list.
const int SIZE = 3;
string commands [SIZE] = { "/run" , "/add" , "/delete" };
cout << "Welcome to ProgramName, please enter a command." << endl;
cin >> input;
for (int i=0; i<SIZE; i++){
if (input==commands[i])
cout << "Command recognized: " << commands[i] << endl;
}
system("pause" );
return (0);
}
I believe I am having an error with the if statement. It saying that the string input needs to be a bool?
Last edited on Feb 17, 2013 at 6:41pm UTC
Feb 17, 2013 at 10:10am UTC
You need to use == if you want comparison, as = is assignment.
Feb 17, 2013 at 6:41pm UTC
Thank you! That was simple.. -__-"
Last edited on Feb 17, 2013 at 6:43pm UTC
Topic archived. No new replies allowed.