Thread: Come and judge me.

  1. #1
    Registered User
    Join Date
    Apr 2020
    Posts
    62

    Come and judge me.

    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;
    }

  2. #2
    Registered User
    Join Date
    Aug 2019
    Location
    inside a singularity
    Posts
    308
    You can't compare using != in C.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook, The Wizardry Compiled

  3. #3
    Registered User
    Join Date
    Apr 2020
    Posts
    62
    What should I do?

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    Start by doing some research. Use your favourite search engine, searching for something like "c string comparison" should give you a good start,

    By the way I'd judge about a 2 maybe 3.

  5. #5
    Registered User
    Join Date
    Apr 2020
    Posts
    62
    Thank you very much my friend.

  6. #6
    Registered User Sir Galahad's Avatar
    Join Date
    Nov 2016
    Location
    The Round Table
    Posts
    277
    Quote Originally Posted by TheGreekMan2000 View Post
    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.

    Code:
    scanf("%4s", end);
    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;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. UVA Online Judge problem 536
    By tonsonson in forum C Programming
    Replies: 1
    Last Post: 03-30-2015, 03:12 AM
  2. ACM online judge (problems)
    By Maragato in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 08-27-2006, 01:11 PM
  3. How do you judge yourself
    By Shadow12345 in forum A Brief History of Cprogramming.com
    Replies: 24
    Last Post: 01-14-2003, 07:44 AM

Tags for this Thread