Thread: Help with declaring variables

  1. #1
    Registered User
    Join Date
    Mar 2014
    Posts
    1

    Help with declaring variables

    I am having some difficulties with my code. I am new to C Programming and I am receiving the following error.
    error: ‘clrscr’ was not declared in this scope


    Code:
    #include <stdio.h>
    
     
    
    int main(void) {
    
        int i,n,sum =0,l,s,list[100];
    
       float avg;
    
       clrscr();
    
       /* Reading the array value */
    
       printf("Total value in the array\n");
    
       scanf("%d",&n);
    
       printf("Enter %d value \n",n);
    
       for(i=0;i<n;i++)
    
          scanf("%d",&list[i]);
    
     
    
       s=l=sum= list[0]; /* Take first value of array and assign into sum,l,s*/
    
       /* Compare and sum */
    
       for(i=1;i<n;i++)
    
       {
    
          if(list[i]>l) l=list[i];
    
          if(list[i]<s) s=list[i];
    
          sum+=list[i];
    
       }
    
       /* Print result */
    
       avg=sum/(float)n;
    
       printf("Data list is:\n");
    
       for(i=0;i<n;i++)
    
           printf("%5d",list[i]);
    
       printf("\n\n");
    
       printf("Largest Value   = %d\n",l);
    
       printf("Smallest Value  = %d\n",s);
    
       printf("Sum of all data = %d\n",sum);
    
       printf("Average         = %f\n",avg);
    
       getch();
    
     
    
    }

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    clrscr() is a function.

    It is also non-standard. If it is included in the library that comes with your compiler, it is typically in a header file named <conio.h>. If not, you're going to have to write some function to achieve the desired effect (of clearing the screen).

    Similar comment goes for getch() (it gets character input from a console window).


    Also, it is better to call "your code" what it is. What you've actually done is taken code (or been given code) from somewhere else and tweaked it. Because there is no way code YOU had written would make use of specific non-standard functions without you knowing what they are.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    By the way, this example doesn't seem to even call for the functionality implied by clrscr or getch. Just get rid of them and try the code as it stands without them. I personally don't like the terminal clearing everytime I launch a new command.

  4. #4
    Registered User C-learner's Avatar
    Join Date
    Feb 2014
    Posts
    32
    yeah clrscr() is not a standard function and can be absent in your compiler implementation. But if you insist on using them
    system("cls") can be put in place of clrscr() if your on windows, system("cls") goes with the headerfile stdlib.h(C header)/cstdlib(C++ header) or nothing depending on your compiler and getch() goeswith the header file conio.h. and where clrscr() is placed in your above coding is rubbish.

    #include <stdlib.h> or #include <cstdlib>
    for system("cls");

    #include<conio.h>
    for getch();

    both function takes a semicolon at the end
    Last edited by C-learner; 03-08-2014 at 11:29 AM.
    ~If significant coding comes to you easily then you are definitely doing something wrong~
    `

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by C-learner View Post
    system("cls") can be put in place of clrscr() if your on windows, system("cls")
    The behaviour of system() is also implementation defined (i.e. not guaranteed to do anything in particular). It is not actually hard to change the effect of system("cls") under windows, for example, externally to your program.

    If it was solely a choice between using system("cls") and conio clrscr() to clear a screen, then I'd use the latter.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Declaring letters as variables and using them
    By lolcats22 in forum C Programming
    Replies: 4
    Last Post: 10-12-2011, 08:21 PM
  2. Declaring Variables
    By Godders_2k in forum C Programming
    Replies: 10
    Last Post: 11-18-2007, 04:50 AM
  3. dynamically declaring variables
    By smartalco in forum C++ Programming
    Replies: 7
    Last Post: 01-02-2007, 03:02 PM
  4. Declaring variables
    By Marlon in forum C++ Programming
    Replies: 6
    Last Post: 06-21-2005, 04:16 AM
  5. Declaring variables after statements.
    By Thantos in forum C Programming
    Replies: 4
    Last Post: 10-24-2004, 08:58 AM

Tags for this Thread