• C

clrscr function for unix

Actually I have two questions.
1. What can I use to get the same result as clrscr() , but for UNIX or LINUX.

2.#include <stdio.h>
#include <stdlib.h>
int main()
{
float *i;
      i = (float *)malloc(sizeof(float));

      scanf("%f ", &i);
      fflush(stdin);
      printf("%f\n", i);

int *j;
      j = (int *)malloc(sizeof(int));
      scanf("%d", &j);
      printf("%d\n", j);
      return 0;
}

What is wrong with getting the float value.
It prints garbage, so scanf does not put the input in the variable(It puts it for the integer but not for the float)
simiAsked:
Who is Participating?
 
RONSLOWConnect With a Mentor Commented:
try ...

#include <curses.h>
void clrscr (){
  initscr ();
  endwin ();
}

or

#include <stdlib.h>
void main (void)
{
 system("clear");
}

or

#include <stdlib.h>
void main (void)
{
 system("cls");
}

(NOTE: some systems use 'clear' some 'cls' others -- who knows)

There is no portable way (AFAIK) to do this.

Roger


0
 
julio011597Commented:
1. i don't know the name of the exact function, but you should include <curses.h> into your UNIX program and the need function will be there (do a 'man curses' first);

2. scanf() needs pointers to store the values it retrieves; but, "i" is already a pointer, so passing "&i" you actually give a pointer to a pointer;

your code should look like (the usual way):

--//--
float i;

scanf("%f", &i);
--//--

or, similar to your way:

--//--
float *i;

i = malloc(sizeof(float)); /* the cast is descouraged in C */

scanf("%f", i);
--//--

HTH, julio.
0
 
simiAuthor Commented:
Sorry

I played with it a little and I got myself the answer.
That's right, float *i does not work with scanf("%f", &i).
But the whole issue is that if I use printf("%f", i);
I still get nothing(but eventually the adress of the pointer, wich is not what I wanted). The complete thing is:
float *i;
malloc ...
scanf("%f", i);
printf("%f\n", *i);

Thanks anyway

I will reopen the question because I really need to find what shall I use in Linux or Unix for a clrscr() replacement.

Sorry.
0
Improve Your Query Performance Tuning

In this FREE six-day email course, you'll learn from Janis Griffin, Database Performance Evangelist. She'll teach 12 steps that you can use to optimize your queries as much as possible and see measurable results in your work. Get started today!

 
julio011597Commented:
I was confident you could figure out the rest yourself.
0
 
simiAuthor Commented:
O.K.

The problem is a little more complicated anyway, so I increased the points and it is reopen including Jullio.

I can compile the following code, but when I run it I get a message:
scanf: floating points format not linked
Abnormal program termination

I do not understand why.
Here's the code:



#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
enum {saturday, sunday, monday, tuesday, wednesday, thursday, fryday};
typedef struct day
{
      char project[21];
      float rt;
      float ot;
      day *next;
}DAY;

void set_day(int);
int ask_day();
DAY *first_day[7];

int ask_day()
{
char buffer;


      do
      {
            clrscr();
            printf("\n\n\t\tWhat day to you want to add ?\n");
            printf("\t\t1.SATTURDAY\n");
            printf("\t\t2.SUNDAY\n");
            printf("\t\t3.MONDAY\n");
            printf("\t\t4.TUESDAY\n");
            printf("\t\t5.WEDNESDAY\n");
            printf("\t\t6.THURSDAY\n");
            printf("\t\t7.FRYDAY\n");
            buffer = getch();
            if(buffer < '1' || buffer > '7')
            {
                  printf("\n\n\t\tThis is not a valid choice\n");
                  printf("\t\tPlease try again\n");
                  printf("\t\tPress Enter to continue\n");
                  buffer = 8;
                  getchar();
            }
      }while(buffer == 8);


      return buffer;


}



void set_day(int i)
{
DAY *nou, *ptr;

      nou = (DAY *)malloc(sizeof(DAY));

      if(nou == NULL)
      {
            printf("Memory allocation error\n");
            exit(1);
      }
      clrscr();
      printf("\n\n\t\tPlease type project number\n");
      scanf("%20s", nou->project);
      fflush(stdin);
      printf("\t\tPlease type regular hours\n");
      scanf("%f", nou->rt);
      fflush(stdin);
      printf("\t\tPlease type overtime hours\n");
      scanf("%f", nou->ot);
      fflush(stdin);
      printf("ot= %f\n", nou->ot);
      printf("rt= %f\n", nou->rt);
      if(first_day[i] == NULL)
      {
            first_day[i] = nou;
      }
      else
      {
            while(ptr->next != NULL)
            {
                  ptr = ptr->next;
            }
            ptr->next = nou;
            ptr = ptr->next;
      }
      printf("Test inside set_day");
      printf("project = %s\n", first_day[saturday]->project);
      printf("ot = %.2f\n", first_day[saturday]->ot);
      printf("rt = %.2f\n", first_day[saturday]->rt);


}





int main()
{
  switch(ask_day())
  {
      case '1':
      set_day(saturday);
      break;

      case '2':
      printf("This is case 2\n");
      break;

      case '3':
      printf("Tis is case 3\n");
      break;

      case '4':
      printf("This is case 4\n");
      break;

      case '5':
      printf("This is case 5\n");
      break;

      case '6':
      printf("This is case 6\n");
      break;

      case '7':
      printf("This is case 7\n");
      break;

   }

    return 0;
}

0
 
simiAuthor Commented:
What about my scanf error message at runtime ?
0
 
RONSLOWCommented:
your previous wasn't there when I posted my answer .. I'll have a look now...
0
 
RONSLOWCommented:
you are not passing a float pointer to sscanf, just a float.
eg.

printf("\t\tPlease type regular hours\n");
scanf("%f", nou->rt);
fflush(stdin);
printf("\t\tPlease type overtime hours\n");
scanf("%f", nou->ot);

should be

printf("\t\tPlease type regular hours\n");
scanf("%f", &nou->rt);
fflush(stdin);
printf("\t\tPlease type overtime hours\n");
scanf("%f", &nou->ot);

0
 
simiAuthor Commented:
I did it and still I get the same message
0
 
julio011597Commented:
I have the same problem with DOS TurboC++ (run under WinNT4.0, if this makes any difference).
It just refuses to accept an "%f" for scanf().

Well, i've not been able to solve the problem... waiting for an answer, me too :)
0
 
julio011597Commented:
BTW, my workaround is to get the value as a string, then apply an atof()... but it's just a workaround.
0
 
simiAuthor Commented:
I am using Turbo C++ under Dos
Thank you

I will try your workaround and if someday we get the answer let's let the other know.
0
 
RONSLOWCommented:
also you are using 'ptr' before you are initializing it in:

            while(ptr->next != NULL)
            {
                  ptr = ptr->next;
            }

ptr has not been set at this stage, and so has rubbish in it.

0
 
RONSLOWCommented:
It sounded compiler dependant

Sounds like a poor excuse compiler if scanf doesn't work !!! :-)

Good luck - and don't forget to fix up your init of 'ptr'
0
 
simiAuthor Commented:
Thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.