Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Ncurses/getch() issues

Posted on 2008-10-23
17
Medium Priority
?
1,151 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
[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
17 Comments
 
LVL 46

Expert Comment

by:Kent Olsen
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
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.

 

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
 

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 1000 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

Survive A High-Traffic Event with Percona

Your application or website rely on your database to deliver information about products and services to your customers. You can’t afford to have your database lose performance, lose availability or become unresponsive – even for just a few minutes.

Question has a verified solution.

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

There are many software programs on offer that will claim to magically speed up your computer. The best advice I can give you is to avoid them like the plague, because they will often cause far more problems than they solve. Try some of these "do it…
Invest in your employees with these five simple steps to improve employee engagement and retention.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
The viewer will learn how to successfully download and install the SARDU utility on Windows 8, without downloading adware.

721 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