[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1158
  • Last Modified:

Ncurses/getch() issues

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
Aya861
Asked:
Aya861
1 Solution
 
Kent OlsenData Warehouse Architect / DBACommented:
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
 
Infinity08Commented:
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
 
numberkruncherCommented:
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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
Aya861Author Commented:
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
 
Aya861Author Commented:
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
 
Infinity08Commented:
You didn't initialize i nor the student array.
0
 
Infinity08Commented:
Note that you also didn't call initscr() to set up the screen for curses, nor endwin() at the end.
0
 
Infinity08Commented:
Also, you should probably use the curses output methods (like printw) instead of the stdio ones.
0
 
Aya861Author Commented:
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
 
Infinity08Commented:
>> 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
 
Aya861Author Commented:
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
 
Infinity08Commented:
>> 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
 
Aya861Author Commented:
I don't want to pass a pointer to a structure through like 9 different functions?
0
 
Aya861Author Commented:
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
 
Infinity08Commented:
>> 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
 
Aya861Author Commented:
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
 
Infinity08Commented:
No problem. Take all the time you need :)
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now