I'm fairly new to C++ and just learning it on my own, so please go somewhat easy on me. What I am trying to do:
- Print an array that has program names on it and ask the user to type the name of a program
- Get the user input and check if it matches an entry in the string array
- If the user input has a match, run the program that matched
The code I wrote functions as I want it to, yet the errors and warnings in the build terminal indicate that something is not correct. I have tried using different data types but I have run out of ideas. By using a char * array for the program names I got it down to one warning: "ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]". I would love to get some clarity on what I am doing wrong that causes this warning.
This is the code:
#include <stdio.h>
#include <conio.h>
#include <iostream>
#include <string.h>
#include <windows.h>
using namespace std;
void keyControl(), randomNumber();
// What data type for array?
string programList[99] = {"keyControl", "randomNumber"};
char writeWord[18] = "";
int main(){
// Is there an equivalent of strcmp() for string datatype?
while(strcmp(writeWord, "quit") != 0){
system("@cls");
cout << ("\n\tProgram list:\n\t");
for(int x = 0; x < 99; x++){cout << (programList[x]) << (", ");}
cout << ("\n\n\tType a program to run or 'quit' to exit:\n\n<");
// Is there an equivalent of gets() for string datatype?
gets(writeWord);
system("@cls");
if(strcmp(writeWord, programList[1]) == 0){
//keyControl();
}
if(strcmp(writeWord, programList[2]) == 0){
//randomNumber();
}
}
return 0;
}
And yes, I have read everywhere that gets() is a no-no but this is only a quickly thrown together test program and I would use something else if I was actually writing code seriously or for work, etc. I'm only doing this for fun and learning purposes currently, and want to focus on one thing at a time :-)
Thanks