Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

clrscr function for unix

Posted on 1998-03-30
15
Medium Priority
?
972 Views
Last Modified: 2012-06-21
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)
0
Comment
Question by:simi
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
  • 4
15 Comments
 
LVL 5

Expert Comment

by:julio011597
ID: 1257941
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
 

Author Comment

by:simi
ID: 1257942
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
 
LVL 5

Expert Comment

by:julio011597
ID: 1257943
I was confident you could figure out the rest yourself.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:simi
ID: 1257944
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
 
LVL 10

Accepted Solution

by:
RONSLOW earned 400 total points
ID: 1257945
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
 

Author Comment

by:simi
ID: 1257946
What about my scanf error message at runtime ?
0
 
LVL 10

Expert Comment

by:RONSLOW
ID: 1257947
your previous wasn't there when I posted my answer .. I'll have a look now...
0
 
LVL 10

Expert Comment

by:RONSLOW
ID: 1257948
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
 

Author Comment

by:simi
ID: 1257949
I did it and still I get the same message
0
 
LVL 5

Expert Comment

by:julio011597
ID: 1257950
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
 
LVL 5

Expert Comment

by:julio011597
ID: 1257951
BTW, my workaround is to get the value as a string, then apply an atof()... but it's just a workaround.
0
 

Author Comment

by:simi
ID: 1257952
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
 
LVL 10

Expert Comment

by:RONSLOW
ID: 1257953
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
 
LVL 10

Expert Comment

by:RONSLOW
ID: 1257954
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
 

Author Comment

by:simi
ID: 1257955
Thanks
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

609 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question