Solved

Ncurses/getch() issues

Posted on 2008-10-23
17
1,145 Views
Last Modified: 2013-11-15
I'm fairly new to C and have come to realize that conio.h isn't part of the standard ANSI C library. Unfortunately I have a data validation project assigned for class that can't really get done without something to capture characters on keypress. After some looking around ncurses seemed like the library to get so I did. Unfortunately now my output comes up quite funny.
I am using the gcc compiler via cygwin and using the netbeans IDE.

Attatched is a picture of the output.
I'm really at a loss of what to do at this point so any information would be helpful.

COLORS.h is just a header with a bunch of screen macro's.. CLS, LOCATE, PAUSE, ect...

/* 

 * File:   p4_c.c

 * Author: Admin

 *

 * Created on October 16, 2008, 2:31 PM

 */

 

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <CTYPE.H>

#include <COLORS.H>

#include <ncurses.h.lnk>

#define BACKSPACE putchar(8)

void paintscreen(),getdata(),prntrpt();

char * inptrtn(int fldlen,char type);

char* inptrtnstr(int fldlen);

char *inptrtnint(int fldlen);

char* inptrtnfloat(int fldlen);

int i;

char buffer[30];

struct name

{char first[15];

 char last[20];

};

struct

{struct name name;

 int credits;

 float GPA;

}student[3];

/*

 * 

 */

int main(int argc, char** argv) {

    paintscreen();

    while (strlen(student[i].name.first)!=0)

    {   getdata();

        PAUSE;

        i++;

        paintscreen();

    }

    return (EXIT_SUCCESS);

}

void paintscreen(){

    CLS;

    LOCATE(1,15);puts("Student Information Data Entry");

    LOCATE(3,1);puts("First Name:______________");

    LOCATE(5,1);puts("Last Name:____________________");

    LOCATE(7,1);puts("Credits:___");

    LOCATE(9,1);puts("GPA:_.__");

    LOCATE(3,12);

    strcpy(student[i].name.first,inptrtn(14,'a'));

}

void getdata(){

    char GPA;

    LOCATE(5,11);strcpy((student[i].name.last),inptrtn(19,'a'));

    LOCATE(7,9);student[i].credits=atoi(inptrtn(3,'i'));

    LOCATE(9,5);strcpy(GPA,inptrtn(1,'f'));

    strcat(GPA,".");

    LOCATE(9,7);strcat(GPA,inptrtn(2,'f'));

    student[i].GPA=atof(GPA);

}

char * inptrtn(int fldlen,char type){

    static char buffer[30];

    switch (type)

    {   case 'a':inptrtnstr(fldlen);break;

        case 'i':inptrtnint(fldlen);break;

        case 'f':inptrtnfloat(fldlen);break;

    }

    return buffer;

}

char * inptrtnstr(int fldlen){

    char c,d;

    int j=0;

    strcpy(buffer,"");

    while ((c=getch())!=13)

    {d=toupper(c);

     if (d>="A"&&d<='Z'||d=='.'||d=='-'||d==' '||d=='\'')

     {  buffer[j++]=c;putchar(c);

      if (j==fldlen)

         break;

     }

     if (c==8 && j!=0)

        {j--;BACKSPACE;}

         buffer[j]='\0';

    }

    return buffer;

}

char * inptrtnint(int fldlen){

    char c;

    int j;

    strcpy(buffer,"");

    while ((c=getch())!=13)

    {if (c>='0' && c<='9')

     {    buffer[j++]=c;putchar(c);

        if(j==fldlen)

            break;

     }

     if (c==8 && j!=0)

     {j--;

      BACKSPACE;

     }

     buffer[j]='\0';

    }

    return buffer;

    }   

char * inptrtnfloat(int fldlen){

    char c;

    int j;

    strcpy(buffer,"");

    while ((c=getch())!=13)

    {    if (fldlen=1)

        {if (c>=0&&c<=4)

            {  buffer[j++]=c;putchar(c);

                if (j==fldlen)

                 break;

            }

        }

         else {if (c>=0&&c<=9)

            {  buffer[j++]=c;putchar(c);

                if (j==fldlen)

                 break;

            }

              }

         if (c==8 && j!=0)

     {j--;

      BACKSPACE;

     }

         buffer[j]='\0';

    }

    return buffer;

}

void printrpt()

{int j=0;

 for (j=0;j<3;j++)

 {    printf("Name: %-15s %-20s\n",student[j].name.first,student[j].name.last);

      printf("Credits: %-3",student[j].credits);

      printf("GPA: %f",student[j].GPA);

 }

}

Open in new window

output.JPG
0
Comment
Question by:Aya861
17 Comments
 
LVL 45

Expert Comment

by:Kdo
ID: 22789055
Hi Aya861,

Where did the message "[Press Enter to close window]" come from?  I don't see it in your source code anywhere.


Kent
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 22789101
Why do you get the student's first name inside the paintscreen function rather than in the getdata function ?

Inside getdata, you declare GPA as a single char, but you treat it as a string (array of char) further.

In inptrtnstr : "A" is not the same as 'A'. The first is a string literal, the second is a character literal. You want a character literal.
0
 
LVL 13

Expert Comment

by:numberkruncher
ID: 22789126
Is getch() returning ERR?

According to the documentation, getch() returns ERR when it is in non-blocking mode.

Try adding the following after paintscreen in your main function.

timeout(-300);
0
 

Author Comment

by:Aya861
ID: 22789175
Well the "A" was a mistake that I didn't catch as was declaring GPA as a single character and not a character array.

As for getting the student's first name in paintscreen, it should set me up to determine whether or not the enter key is pressed.

After fixing those 2 errors, paint screen occurs and then nothing. No keyboard entry is accepted :/
0
 

Author Comment

by:Aya861
ID: 22789241
Edit: The program isn't even locating back to 3,12. It looks like its getting hung up on this line right here.
LOCATE(3,12);

strcpy(student[i].name.first,inptrtn(14,'a'));

Open in new window

0
 
LVL 53

Expert Comment

by:Infinity08
ID: 22789243
You didn't initialize i nor the student array.
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 22789260
Note that you also didn't call initscr() to set up the screen for curses, nor endwin() at the end.
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 22789274
Also, you should probably use the curses output methods (like printw) instead of the stdio ones.
0
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 

Author Comment

by:Aya861
ID: 22789556
Is there any rule saying that I need to use curses screens? Student and i were initialized globally.

I'm only using ncurses for the getch() function at this point.
0
 
LVL 53

Accepted Solution

by:
Infinity08 earned 250 total points
ID: 22789602
>> Student and i were initialized globally.

They were declared globally. They were not initialized.


>> Is there any rule saying that I need to use curses screens?

You said you wanted to use curses :) That's not a rule. That's your choice.


>> I'm only using ncurses for the getch() function at this point.

It doesn't work like that ;) The usage of getch requires that curses has control over the screen.
0
 

Author Comment

by:Aya861
ID: 22789653
Doesn't a global integer variable automatically get initialized at 0? I mean I could initialize the character arrays to "" but I really don't think that'd do much. As for needing curses to be in control of the screen... well I didn't know about that. Will test that out and hope it works.
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 22789766
>> Doesn't a global integer variable automatically get initialized at 0?

Yes, but why not do it explicitly ? For that matter, why have global variables at all ?
0
 

Author Comment

by:Aya861
ID: 22789781
I don't want to pass a pointer to a structure through like 9 different functions?
0
 

Author Comment

by:Aya861
ID: 22789830
Edit... I guess passing them to the 3 functions that use it wouldn't be that difficult... this was just the way the example was written and I didn't really think otherwise.
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 22789835
>> I don't want to pass a pointer to a structure through like 9 different functions?

Only the functions that need it. So, basically, only the getdata and the printdata.

In any case, you should avoid global data as a general rule. It might seem easier at first, but it isn't, trust me. Function interfaces are best made explicit in their signature. Not only will that make code maintenance easier - it will also control the flow of data better, and only have data available where it's needed.
0
 

Author Comment

by:Aya861
ID: 22789903
I'm gradually learning this with some of my own code that isn't assigned as a project...
As for why I haven't checked the code yet... well I'm in Composition class right now and can't really open up my laptop to check.
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 22789915
No problem. Take all the time you need :)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
Workplace bullying has increased with the use of email and social media. Retain evidence of this with email archiving to protect your employees.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

932 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