Solved

clrscr function for unix

Posted on 1998-03-30
15
954 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
  • 6
  • 5
  • 4
15 Comments
 
LVL 5

Expert Comment

by:julio011597
Comment Utility
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
Comment Utility
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
Comment Utility
I was confident you could figure out the rest yourself.
0
 

Author Comment

by:simi
Comment Utility
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 100 total points
Comment Utility
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
Comment Utility
What about my scanf error message at runtime ?
0
 
LVL 10

Expert Comment

by:RONSLOW
Comment Utility
your previous wasn't there when I posted my answer .. I'll have a look now...
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 10

Expert Comment

by:RONSLOW
Comment Utility
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
Comment Utility
I did it and still I get the same message
0
 
LVL 5

Expert Comment

by:julio011597
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

772 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now