You shouldn't be using iostream at all. It's a C++ library. In C, you should be using stdio.h for all of your standard input and output operations. Also, cmath is a C++ library. The equivalent C library is math.h. You still shouldn't need it for the code snippet I was looking over for you. Again, std::cin is a C++ operation that doesn't exist in C.
Anyway, here's my working sample for you again...just this time really heavily commented. If you still don't understand, just try to tell me precisely what's getting you and I'll give you a more appropriate explanation.
/**
* You need stdio.h in order to use printf() and fgets()
*/
#include <stdio.h>
/**
* You need string.h to use strcmp
*/
#include <string.h>
/**
* When you run a program, it runs preprocessor commands (commands that start with #),
* and then begins execution at the main().
*/
int main(int argc, char **argv) {
/**
* You really shouldn't use // for comments in C. That's a C++ thing.
*
* This initializes an array of characters. You have space for up to ten characters.
* Really that's nine characters, though, because you need the null terminator.
*/
char command[10];
/**
* As you're aware, this prints a line to the console. Notice that I'm not ending
* with a newline character. I'm relying on the next printf in the loop to handle that
* because I want to make sure it starts with a new line every time the loop runs.
*/
printf("\n\n******************\nEnter a command. Type 'help' for a list of commands.");
/**
* A do loop is assured to run at least once before ever checking the continuation
* condition. You want to use a do loop here partially because you always want it
* to ask the user for input at least once and partly because you aren't initializing
* command to contain anything. There is an astronomically low chance it could contain
* "quit" initially. It just contains whatever is in memory at that location if you
* don't tell it otherwise.
*/
do {
/**
* This starts with a newline because I always want that prompt to be
* on its own line.
*/
printf("\n-> ");
/**
* You know, I didn't know this command existed until looking to solve
* your problem. I couldn't get scanf to work, so I checked the manual
* for stdio.h and started looking for commands. If you're running
* anything Unix based (Linux, MacOSX, etc), you can get to that entry
* by opening a terminal/console window and typing the following:
*
* man stdio.h
*
* That manual page contains information on the contents of that header
* file. To get to the manual page for fgets(), use the following:
*
* man 3 fgets
*
* Or just use Google. That works too.
*
* The signature for fgets() is:
* fgets(char *s, int size, FILE *stream);
*
* --s is the character array that you're storing the input in. Make sure
* the array is large enough for the largest input you expect.
* --size is the size of the buffer. Just hand it the size of the
* character array using sizeof();
* --stream is the input stream you're using. If you want to take input
* directly from the terminal, just pass it stdin.
*
* stdin - Standard Input - stdin is an input stream that is always open
* and all input goes through it unless you tell it differently.
* Similarly stdout and stderr are where normal output and error output go
* by default. You can always redirect any of these, but it usually
* isn't necessary.
*/
fgets(command, sizeof(command), stdin);
/**
* strcmp() takes two const char * variable and compares them against
* each other. If they are the same, it returns 0, if they aren't
* it returns either a positive or negative number to denote the
* result of the comparision.
*
* conditionals (if, while, etc) can be passed a number. C assumes that
* 0 is FALSE and anything else is TRUE. Conveniently, strcmp() returns
* 0 if and only if the strings are a match. Calling strcmp() with an
* ! asks if it is false, or returns 0. If it does, display help info.
* Otherwise move on.
*/
if (!strcmp(command, "help\n")) {
printf("Here is a list of commands in the game:\n");
printf("Check your progress:\t'status'\n");
printf("Move to the next room:\t'next'\n");
printf("Rest:\t\t\t'rest'\n");
printf("Attack (battle only):\t'attack'\n\n");
printf("There may be additional, secret commands. Try to find them!\n");
}
/**
* In a do loop, the continuation condition appears after the code block. In this case, we aren't
* using a ! because we want to keep going so long as the commands isn't quit.
*/
} while (strcmp(command, "quit\n"));
printf("END OF PROGRAM\n");
/**
* The C standard specifies that the main() should return 0 on a normal exit. It's actually
* returning a code to the operating system that describes why the program ended execution.
* Theoretically, if the program crashes, it should return an error code specifying what
* went wrong. Returning 0 is considered a normal exit.
*/
return 0;
}
Also, if you don't tell it differently, gcc assumes you're using the C89 standard. If you want to use the C99 standard (or some other standard), you'll have to explicitly tell gcc that with the std parameter. Here's an example of how you might do that:
gcc --std=c99 sirus.c
C99 gives you a number of features that aren't in C89. Notably is the for loop.
EDIT: You know, it'd probably be helpful to point out that the strange fgets() command I'm using actually does have a meaningful name. fgets stands for Filestream Get String. In my example, the filestream I'm using is stdin. Get is exactly what it says on the tin. It's getting something from the filestream. String means that the specific thing I'm getting from the filestream is a string. In the context of my example, I'm getting a string from stdin.