Hi guys,
I have to write a program for uni. It's a beginner C class and we have to write a simple dumb program.
One of the feature of the dumb program is when the user enters "toggle" into the command line, the program says "off" and when you put toggle again, the program outputs "on".
I've been at this for over two hours now and I think I may have gotten the syntax wrong.
Here's part of my code:
Whenever I enter toggle into the command prompt, it outputs "command not recognized so it's skipping over the if statements for some reason. C isn't like Java, which I'm pretty used to so I'm really confused as to why this is happening.
Oh and just for reference, I have these two variables:
char toggle[4] and char command[100]
Help please?
I have to write a program for uni. It's a beginner C class and we have to write a simple dumb program.
One of the feature of the dumb program is when the user enters "toggle" into the command line, the program says "off" and when you put toggle again, the program outputs "on".
I've been at this for over two hours now and I think I may have gotten the syntax wrong.
Here's part of my code:
Code:
while (keep_going = 1) {
printf("Enter command: ");
fgets(command, sizeof(command), stdin);
command[strlen(command)-1] = '\0';
if(strcmp(command, "toggle")) {
if(toggle == "on") {
strcpy(toggle, "off");
printf("Toggle is now %c\n", toggle);
}
else if(toggle == "off") {
strcpy(toggle, "on");
printf("Toggle is now %c\n", toggle);
}
}
else
printf("Command unrecognized.\n");
Whenever I enter toggle into the command prompt, it outputs "command not recognized so it's skipping over the if statements for some reason. C isn't like Java, which I'm pretty used to so I'm really confused as to why this is happening.
Oh and just for reference, I have these two variables:
char toggle[4] and char command[100]
Help please?
Last edited: