Thread: exit loop by input not case sensitive

  1. #1
    Registered User
    Join Date
    Nov 2016
    Posts
    1

    Question exit loop by input not case sensitive

    Hello, my task is to write a program for a user to input integer values and calculate the percentage of entered even numbers. The math part is kind of easy but then the task is to give the option to exit the input loop by typing "exit" not case sensitive or by just hitting enter. I am new to C programming and just started learning in school. Sorry if my question seems easy or dumb. But I have been at it for some time now and can't get it to work.
    This is what I have so far:
    Code:
    int main()
    {
    int a, i, sum1=0, sum2=0, rel;
    int d[100];
    printf ("How many numbers will be entered\n");
    scanf ("%d", &a);
    printf ("Enter %d numbers (to exit program type - exit) \n", a);
    for(i=0;i<a;i++)
    {
    if(1!=scanf ("%d", &d[i]))
    {
    if (strcmp(d[i], "exit") != 0)
    break;
    }
    /* {
    if (strcmp(d[i], "\n") != 0)
    break;
    } */
    if (d[i]%2==0)
    {
    sum1=sum1+d[i];
    }
    else
    {
    sum2=sum2+d[i];
    }
    }
    printf("\nElements in array are: ");
    for(i=0; i<a; i++)
    {
    printf("%d, ",d[i]);
    }
    rel=sum1*100/(sum1+sum2);
    printf ("\nRelative amount of evens in total amount is: %d percent\n", rel);
    
    
        return 0;
    }
    Been looking though forums and tutorials and can't find the answer that would be the right one.
    Thanks in advance for any help you can give me.

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Reading integers and strings with "scanf()" requires different format specifiers - it will not work as you have it.

    I would recommend reading all input as strings (perhaps with "fgets()"), and if the string is not "exit", extracting the number from the string (perhaps with "sscanf()") for processing.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. case sensitive and spacing
    By return0 in forum C Programming
    Replies: 4
    Last Post: 03-09-2013, 12:02 AM
  2. Case sensitive
    By soled_boy in forum C++ Programming
    Replies: 1
    Last Post: 02-02-2009, 02:45 PM
  3. program is case-sensitive
    By dra in forum C++ Programming
    Replies: 8
    Last Post: 05-02-2005, 10:40 AM
  4. case sensitive
    By GanglyLamb in forum C Programming
    Replies: 3
    Last Post: 11-09-2002, 04:25 AM
  5. Case Sensitive
    By JamMan in forum C++ Programming
    Replies: 1
    Last Post: 11-19-2001, 05:48 PM

Tags for this Thread