LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-28-2010, 06:24 AM   #1
Shh226
LQ Newbie
 
Registered: Mar 2010
Posts: 8

Rep: Reputation: 0
Smile C programming using curses -require help


Hello I have a Database using C programming and text based screen using curses. When I run the program and select "add a customer". The startcontract and endcontract variable does not appear. Also, when I add a name, it does not display what I have written down. All that appears is some jargon which I don't know how to fix it.


PHP Code:
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <curses.h>


#define MAX_STRING (80)         /* Longest allowed response       */
#define MAX_ENTRY (1024)        /* Longest allowed database entry */

#define MESSAGE_LINE 6          /* Misc. messages go here         */
#define ERROR_LINE   20         /* The line to use for errors     */
#define Q_LINE       25         /* Line for Questions             */
#define PROMPT_LINE  18         /* Line for prompting on          */

static char current_name[MAX_STRING] = "\0";
static 
char current_address[MAX_STRING];

const 
char *name_file "name.txt";
const 
char *address_file "address.txt";
const 
char *temp_file "accsys.tmp";

void clear_all_screen(void);
void get_return(void);
int get_confirm(void);
int getchoice(char *greetchar *choices[]);
void draw_menu(char *options[], int highlightint start_rowint start_col);
void insert_name(char *customer);
void get_string(char *string);
void add_customer(void);
void count_customer(void);
void find_customer(void);
void list_customer(void);
void remove_customer(void);
void remove_address(void);
void update_customer(void);

char *main_menu[] =
{
    
"aadd a customer",
    
"ffind customer",
    
"ccount customers",
    
"qquit",
    
0,
};

char *extended_menu[] =
{
    
"aadd new customer",
    
"ffind customer",
    
"ccount customers",
    
"llist current customers",
    
"rremove a current customer",
    
"uupdate a customer",
    
"qquit",
    
0,
};

int main()
{
    
int choice;

    
initscr();

    do {
    
choice getchoice("Options:"current_name[0] ? extended_menu main_menu);

    switch (
choice) {
    case 
'q':
        break;

    case 
'a':
        
add_customer();
        break;

    case 
'c':
        
count_customer();
        break;

    case 
'f':
        
find_customer();
        break;

    case 
'l':
        
list_customer();
        break;

    case 
'r':
        
remove_customer();
        break;

    case 
'u':
        
update_customer();
        break;
    }
    } while (
choice != 'q');

    
endwin();
    exit(
EXIT_SUCCESS);

}            

int getchoice(char *greetchar *choices[])
{
    static 
int selected_row 0;
    
int max_row 0;
    
int start_screenrow MESSAGE_LINEstart_screencol 10;
    
char **option;
    
int selected;
    
int key 0;

    
option choices;
    while (*
option) {
    
max_row++;
    
option++;
    }
    if (
selected_row >= max_row)
    
selected_row 0;

    
clear_all_screen();
    
mvprintw(start_screenrow 2start_screencolgreet);

    
keypad(stdscrTRUE);
    
cbreak();
    
noecho();

    
key 0;
    while (
key != 'q' && key != KEY_ENTER && key != '\n') {
    if (
key == KEY_UP) {
        if (
selected_row == 0)
        
selected_row max_row 1;
        else
        
selected_row--;
    }
    if (
key == KEY_DOWN) {
        if (
selected_row == (max_row 1))
        
selected_row 0;
        else
        
selected_row++;
    }
    
selected = *choices[selected_row];
    
draw_menu(choicesselected_rowstart_screenrowstart_screencol);
    
key getch();
    }

    
keypad(stdscrFALSE);
    
nocbreak();
    echo();

    if (
key == 'q')
    
selected 'q';

    return (
selected);

}

void draw_menu(char *options[], int current_highlightint start_rowint start_col)
{
    
int current_row 0;
    
char **option_ptr;
    
char *txt_ptr;

    
option_ptr options;
    while (*
option_ptr) {
    if (
current_row == current_highlightattron(A_STANDOUT);
    
txt_ptr options[current_row];
    
txt_ptr++;
    
mvprintw(start_row current_rowstart_col"%s"txt_ptr);
    if (
current_row == current_highlightattroff(A_STANDOUT);
    
current_row++;
    
option_ptr++;
    }

    
mvprintw(start_row current_row 3start_col"Move highlight then press Return ");

    
refresh();
}

void clear_all_screen()
{
    
clear();
    
mvprintw(2Q_LINE"%s""Account system");
    if (
current_name[0]) {
    
mvprintw(ERROR_LINE0"Current name: %s: %s\n"current_namecurrent_address);
    }
    
refresh();
}

void get_return()
{
    
int ch;
    
mvprintw(230"%s"" Press return ");
    
refresh();
    while ((
ch getchar()) != '\n' && ch != EOF);
}

int get_confirm()
{
    
int confirmed 0;
    
char first_char 'N';

    
mvprintw(Q_LINE7"Are you sure? ");
    
clrtoeol();
    
refresh();

    
cbreak();
    
first_char getch();
    if (
first_char == 'Y' || first_char == 'y') {
    
confirmed 1;
    }
    
nocbreak();

    if (!
confirmed) {
    
mvprintw(Q_LINE1"    Cancelled");
    
clrtoeol();
    
refresh();
    
sleep(1);
    }
    return 
confirmed;
}

void insert_name(char *customer)
{
    
FILE *fp fopen(name_file"a");
    if (!
fp) {
    
mvprintw(ERROR_LINE0"cannot open customer's name in database");
    } else {
    
fprintf(fp"%s\n"customer);
    
fclose(fp);
    }
}

void get_string(char *string)
{
    
int len;

    
wgetnstr(stdscrstringMAX_STRING);
    
len strlen(string);
    if (
len && string[len 1] == '\n')
    
string[len 1] = '\0';
}

void add_customer()
{
    
char Name[MAX_STRING];
    
char Address[MAX_STRING];
    
char email[MAX_STRING];
    
char payment[MAX_STRING];
    
char startcontract[MAX_STRING];
    
char endcontract[MAX_STRING];
    
char customer_entry[MAX_STRING];

    
int screenrow MESSAGE_LINE;
    
int screencol 10;

    
clear_all_screen();
    
mvprintw(screenrowscreencol"Enter New Customer Details: ");
    
screenrow += 2;

    
mvprintw(screenrowscreencol"Enter Name: ");
    
get_string(Name);
    
screenrow++;

    
mvprintw(screenrowscreencol"Enter Address: ");
    
get_string(Address);
    
screenrow++;

    
mvprintw(screenrowscreencol"Enter Email: ");
    
get_string(email);
    
screenrow++;

    
mvprintw(screenrowscreencol"Enter Payment: ");
    
get_string(payment);
    
screenrow++;

    
mvprintw(screenrowscreencol"Enter Start Contract: ");
    
get_string(startcontract);
    
screenrow++;

    
mvprintw(screenrowscreencol"Enter Contract Expiry Date: ");
    
get_string(endcontract);
    
screenrow++;

    
mvprintw(155"About to add this new entry:");
    
sprintf(customer_entry"%s,%s,%s,%s,%s,%s"NameAddressemailpaymentstartcontractendcontract);
    
mvprintw(175"%s",customer_entry);
    
refresh();

    
move(PROMPT_LINE0);
        if (
get_confirm()) {
    
insert_name(customer_entry);
    
strcpy(current_nameName);
    
strcpy(current_addressAddress);
    }
}

void count_customer()
{
    
FILE *name_fp, *address_fp;
    
char entry[MAX_ENTRY];
    
int name 0;
    
int address 0;

    
name_fp fopen(name_file"r");
    if (
name_fp) {
    while (
fgets(entryMAX_ENTRYname_fp))
        
name++;
    
fclose(name_fp);
    }
    
address_fp fopen(address_file"r");
    if (
address_fp) {
    while (
fgets(entryMAX_ENTRYaddress_fp))
        
address++;
    
fclose(address_fp);
    }
    
mvprintw(ERROR_LINE0"Database contains %d names, with a total of %d addresses."nameaddress);
    
get_return();
}

void find_customer()
{
    
char match[MAX_STRING], entry[MAX_ENTRY];
    
FILE *name_fp;
    
int count 0;
    
char *found, *name, *address;

    
mvprintw(Q_LINE0"Enter a string to search for a customer: ");
    
get_string(match);

    
name_fp fopen(name_file"r");
    if (
name_fp) {
    while (
fgets(entryMAX_ENTRYname_fp)) {

       
address entry;
        if (
found strstr(address",")) {
        *
found 0;
        
name found 1;

        if (
found strstr(name",")) {
            *
found '\0';

            
/* Now see if the match substring is present */
            
if (found strstr(namematch)) {
            
count++;
            
strcpy(current_addressaddress);
            
strcpy(current_namename);
            }
        }
        }
    }
    
fclose(name_fp);
    }
    if (
count != 1) {
    if (
count == 0)
        
mvprintw(ERROR_LINE0"Sorry, no matching customer name found. ");
    if (
count 1)
        
mvprintw(ERROR_LINE0"Sorry, match is ambiguous: %d name's found. "count);
    
current_name[0] = '\0';
    
get_return();
    }
}
void remove_address()
{
    
FILE *address_fp, *temp_fp;
    
char entry[MAX_ENTRY 1];
    
int address_length;

    if (
current_name[0] == '\0')
    return;

    
address_length strlen(current_address);

    
address_fp fopen(address_file"r");
    if (
address_fp == (FILE *)NULL) return;
    
temp_fp fopen(temp_file"w");
      
     while (
fgets(entryMAX_ENTRYaddress_fp)) {
    if (
strncmp(current_addressentryaddress_length) != 0)
        
fputs(entrytemp_fp);
    }
    
fclose(address_fp);
    
fclose(temp_fp);

    
unlink(address_file);
    
rename(temp_fileaddress_file);
}
void remove_customer()
{
    
FILE *name_fp, *temp_fp;
    
char entry[MAX_ENTRY];
    
int address_length;

    if (
current_name[0] == '\0')
    return;

    
clear_all_screen();
    
mvprintw(PROMPT_LINE0"About to remove customer %s: %s. "current_addresscurrent_name);
    if (!
get_confirm())
    return;

    
address_length strlen(current_address);

    
name_fp fopen(name_file"r");
    
temp_fp fopen(temp_file"w");

    while (
fgets(entryMAX_ENTRY,name_fp)) {
    if (
strncmp(current_addressentryaddress_length) != 0)
        
fputs(entrytemp_fp);
    }
    
fclose(name_fp);
    
fclose(temp_fp);

    
unlink(name_file);
    
rename(temp_filename_file);

    
remove_address();

    
current_name[0] = '\0';
}

#define BOXED_LINES    11
#define BOXED_ROWS     60
#define BOX_LINE_POS   8
#define BOX_ROW_POS    2

void list_customer()
{
    
FILE *address_fp;
    
char entry[MAX_ENTRY];
    
int address_length;
    
int lines_op 0;
    
WINDOW *address_pad_ptr;
    
int address 0;
    
int key;
    
int first_line 0;

    if (
current_name[0] == '\0') {
    
mvprintw(ERROR_LINE0"You must select a name first. "stdout);
    
get_return();
    return;
    }
    
clear_all_screen();
    
address_length strlen(current_address);

    
address_fp fopen(address_file"r");
    if (!
address_fp)
    return;
    while (
fgets(entryMAX_ENTRYaddress_fp)) {
    if (
strncmp(current_addressentryaddress_length) == 0)
        
address++;
    }
    
fclose(address_fp);
    
address_pad_ptr newpad(address BOXED_LINESBOXED_ROWS 1);
    if (!
address_pad_ptr)
    return;

    
address_fp fopen(address_file"r");
    if (!
address_fp)
    return;

    
mvprintw(40" Name, Address Listing\n");

    while (
fgets(entryMAX_ENTRYaddress_fp)) {
    if (
strncmp(current_addressentryaddress_length) == 0) {
        
mvwprintw(address_pad_ptrlines_op++, 0"%s"entry address_length 1);
    }
    }
    
fclose(address_fp);

    if (
lines_op BOXED_LINES) {
    
mvprintw(MESSAGE_LINE0"Cursor keys to scroll, RETURN or q to exit");
    } else {
    
mvprintw(MESSAGE_LINE0"RETURN or q to exit");
    }
    
wrefresh(stdscr);
    
keypad(stdscrTRUE);
    
cbreak();
    
noecho();

    
key 0;
    while (
key != 'q' && key != KEY_ENTER && key != '\n') {
    if (
key == KEY_UP) {
        if (
first_line 0)
        
first_line--;
    }
    if (
key == KEY_DOWN) {
        if (
first_line BOXED_LINES address)
        
first_line++;
    }
    
/* now draw the appropriate part of the pad on the screen */
    
prefresh(address_pad_ptrfirst_line0,
         
BOX_LINE_POSBOX_ROW_POS,
         
BOX_LINE_POS BOXED_LINESBOX_ROW_POS BOXED_ROWS);
/*    wrefresh(stdscr); */
    
key getch();
    }

    
delwin(address_pad_ptr);
    
keypad(stdscrFALSE);
    
nocbreak();
    echo();
}

void update_customer()
{
    
FILE *address_fp;
    
char address_name[MAX_STRING];
    
int len;
    
int address 1;
    
int screen_line 1;
    
WINDOW *box_window_ptr;
    
WINDOW *sub_window_ptr;

    
clear_all_screen();
    
mvprintw(PROMPT_LINE0"Re-entering address for customer. ");
    if (!
get_confirm())
    return;
    
move(PROMPT_LINE0);
    
clrtoeol();

    
remove_address();

    
mvprintw(MESSAGE_LINE0"Enter a blank line to finish");

    
address_fp fopen(address_file"a");

    
/* Just to show how, enter the information in a scrolling, boxed,
       window. The trick is to set-up a sub-window, draw a box around the
       edge, then add a new, scrolling, sub-window just inside the boxed
       sub-window. */
    
box_window_ptr subwin(stdscrBOXED_LINES 2BOXED_ROWS 2,
                
BOX_LINE_POS 1BOX_ROW_POS 1);
    if (!
box_window_ptr)
    return;
    
box(box_window_ptrACS_VLINEACS_HLINE);

    
sub_window_ptr subwin(stdscrBOXED_LINESBOXED_ROWS,
                
BOX_LINE_POSBOX_ROW_POS);
    if (!
sub_window_ptr)
    return;
    
scrollok(sub_window_ptrTRUE);
    
werase(sub_window_ptr);
    
touchwin(stdscr);

    do {

    
mvwprintw(sub_window_ptrscreen_line++, BOX_ROW_POS 2"Address %d: "address);
    
clrtoeol();
    
refresh();
    
wgetnstr(sub_window_ptraddress_nameMAX_STRING);
    
len strlen(address_name);
    if (
len && address_name[len 1] == '\n')
        
address_name[len 1] = '\0';
    if (*
address_name)
        
fprintf(address_fp"%s,%d,%s\n"current_addressaddressaddress_name);
    
address++;
    if (
screen_line BOXED_LINES 1) {
        
/* time to start scrolling */
        
scroll(sub_window_ptr);
        
screen_line--;
    }
    } while (*
address_name);
    
delwin(sub_window_ptr);

    
fclose(address_fp);

This is the screen where I input customer details. This is where the problem where name does not appear.
PHP Code:
                    Account system



          Enter 
New Customer Details:

          
Enter Nametom 
          Enter Address
3 red lane
          Enter Email
tom@mail.com 
          Enter Payment
230



     About to add this 
new entry:

     &
#65533;����~D=,3 red lane,tom@mail.com,230


     
Are you sure
 
Old 04-28-2010, 11:11 PM   #2
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
I think you will get more help if you reduce the problem to something smaller that is still fully reproducible. While doing so, you may well discover your error all by yourself. If you have little or no prior programming experience, then this is where you start learning to debug your program. At the very least, it would be helpful to point out where in the source code the faulty output is expected to be coming from. In describing your problem, try to use terms that are more generic; no one but you understands the domain of the application, but many here probably understand the general methods you are trying to employ.

--- rod.
 
Old 04-29-2010, 01:35 AM   #3
broken
Member
 
Registered: Apr 2010
Location: Your mom's trailer
Distribution: NetBSD
Posts: 31

Rep: Reputation: 15
Code:
$ gcc -std=c99 -pedantic -Wall -Wextra shh226.c -lncurses -o shh226
shh226.c: In function ‘find_customer’:
shh226.c:335:9: warning: suggest parentheses around assignment used as truth value
shh226.c:339:9: warning: suggest parentheses around assignment used as truth value
shh226.c:343:13: warning: suggest parentheses around assignment used as truth value
$ ./shh226


                         Account system



          Enter New Customer Details:

          Enter Name: tom
          Enter Address: 3 red lane
          Enter Email: tom@mail.com
          Enter Payment: 230
          Enter Start Contract: sc
          Enter Contract Expiry Date: ced

     About to add this new entry:
                                 
     tom,3 red lane,tom@mail.com,230,sc,ced







       Are you sure?
 
Old 04-29-2010, 08:54 AM   #4
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Yes, it seems to work for me, too. The nature of the garbage characters printed makes me think the problem is related to the actual screen handling. I wonder if the OP is testing with some obscure terminal type not supported by curses. I tested with RHEL4, KDE Konsole and with xterm.
--- rod.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Apache Auth: Using Require User and Require Group newmanium2001 Linux - Software 1 08-25-2009 02:39 PM
Apache2 Require user with Require group Russianspi Linux - Server 2 01-30-2008 11:21 AM
spec file programming - Require tag rindjon Programming 2 03-29-2006 02:17 AM
Mouse programming in curses muhazam Programming 0 12-15-2004 08:03 PM
Require Help in kernel programming ranendra_dutta Linux - Networking 0 07-04-2002 07:35 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:01 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration