Thread: Not reading characters

  1. #1
    Registered User
    Join Date
    Sep 2021
    Posts
    1

    Not reading characters

    Hello everyone. I am making a project for my C class and i cannot find where exactly the flaw is. I have made printf to insert numbers and it works perfectly. After that I have made another printf to insert letters but that's where the problem begins. I insert the cases I told the program to read but it does not accept them. It doesnt even show a message. I am kind of a beginner at C so it could be something just ridiculously easy the solution to the problem.main.c This is the program I have made so far. Thank you in advance for any help i do receive.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,675
    First off, indent your code.
    Code:
    #include<stdio.h>
    #include<math.h>
    
    int inrange(int, int, int, int);
    int cubesum(int, int, int, int);
    int avsqrsum(int, int, int, int);
    int squaresum(int, int, int, int);
    int inrage(int, int, int, int);
    
    int main()
    {
    
      int a, b, c, d;
      char chr;
    
      printf("Eisagete 4 arithmous\n");
      scanf("%d%d%d%d", &a, &b, &c, &d);
    
      printf("Tora dialekste ena gramma apo ta S, C, A\n");
      scanf("%c", &chr);
    
      while (chr != 'W') {
        if (inrange(a, b, c, d) == 1) {
          switch (chr) {
          case 'S':
            printf("SUM OF THE SQUARES %d\n", squaresum(a, b, c, d));
            break;
          case 'C':
            printf("SUM OF THE CUBES %d\n", cubesum(a, b, c, d));
            break;
          case 'A':
            printf("AVERAGE OF THE SQUARES %d\n", avsqrsum(a, b, c, d));
            break;
          }                         /*kleinei to switch */
        } /* kleinei to if */
        else
          printf("INVALID DATA\n");
      }                             /* kleinei to while */
    
      return 0;
    }
    
    int inrange(int a, int b, int c, int d)
    {
      int flag = 1;
      if ((a >= 10 || a <= 20) || (b >= 10 || b <= 20) || (c >= 10 || c <= 20) || (d >= 10 || d <= 20))
        flag = 1;
      return flag;
    }
    
    int squaresum(int a, int b, int c, int d)
    {
      return pow(a, 2) + pow(b, 2) + pow(c, 2) + pow(d, 2);
    }
    
    int cubesum(int a, int b, int c, int d)
    {
      return pow(a, 3) + pow(b, 3) + pow(c, 3) + pow(d, 3);
    }
    
    int avsqrsum(int a, int b, int c, int d)
    {
      return (pow(a, 2) + pow(b, 2) + pow(c, 2) + pow(d, 2)) / 4;
      return 0;
    }
    %c is tricky, because it doesn't obey the usual "skip whitespace" rule that all the other conversions follow.
    So you'll probably want
    Code:
    // note the initial space in this conversion.
    scanf(" %c", &chr);
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. reading characters from a file
    By nmn in forum C Programming
    Replies: 2
    Last Post: 03-19-2013, 12:29 PM
  2. reading off characters
    By amie001 in forum C Programming
    Replies: 2
    Last Post: 06-07-2011, 12:36 AM
  3. reading characters
    By sara.stanley in forum C Programming
    Replies: 9
    Last Post: 02-12-2006, 06:07 PM
  4. reading characters in a file
    By jane in forum C Programming
    Replies: 2
    Last Post: 03-25-2003, 07:10 AM
  5. Reading Characters.
    By Red Army in forum C++ Programming
    Replies: 4
    Last Post: 05-30-2002, 11:36 AM

Tags for this Thread