Originally Posted by
TheGreekMan2000
I cannot find what's the problem in that program. Judge me on how stupid I am in 1-10.
Code:
int main(void)
{
char end[5];
do
{
printf("\nDo you want to continue adding or exit?[yes/exit]\n");
scanf("%s",end);
}
while(end!="exit");
return 0;
}
You can use strcmp() to compare strings. Here's another function that does non-case-sensitive comparisons to test for equality:
Code:
#include <ctype.h>
int strlike(const char* left, const char* right)
{
for(;;)
{
char ch = *left++;
if(tolower(ch) != tolower(*right++))
return 0;
if(ch == 0)
break;
}
return 1;
}
Also, be careful with scanf(). If you don't control the number of characters read you could be looking at a buffer overflow.
There are better approaches to fixing this as well. I usually just use a modified form of fgets() for user input:
Code:
#include <stdio.h>
#include <string.h>
char* input(char* buffer, size_t size)
{
fgets(buffer, size, stdin);
char* newline = strstr(buffer, "\n");
if(newline)
*newline = 0;
return buffer;
}
Then:
Code:
int main(void)
{
char buf[64];
for(;;)
{
puts("Enter some input [type 'exit' to quit]");
input(buf, sizeof(buf));
if(strlike(buf, "exit"))
break;
printf("Input: %s\n", buf);
}
return 0;
}