• 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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
julio011597Commented:
I was confident you could figure out the rest yourself.
0
Powerful Yet Easy-to-Use Network Monitoring

Identify excessive bandwidth utilization or unexpected application traffic with SolarWinds Bandwidth Analyzer Pack.

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
RONSLOWCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.