User Name
Password

Go Back   Planetarion Forums > Non Planetarion Discussions > Programming and Discussion
Register FAQ Members List Calendar Arcade Today's Posts

Reply
Thread Tools Display Modes
Unread 22 Nov 2002, 23:58   #1
Nodrog
Registered User
 
Join Date: Jun 2000
Posts: 8,476
Nodrog has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.Nodrog has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.Nodrog has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.Nodrog has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.Nodrog has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.Nodrog has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.Nodrog has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.Nodrog has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.Nodrog has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.Nodrog has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.Nodrog has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.
Help me remember C!

Code:
#include <stdio.h>
#include <string.h>
#include "struct.h"
#define ARRAY_SIZE 100
#define WELCOME_TEXT "Welcome to the person database!\n\n"
#define PROMPT "Would you like to [A]dd a record, [S]earch for a record, or [Q]uit? "
#define ERROR "You didnt enter a valid option"
#define PRINT_ALL "\nHere are a list of all people in database:\n\n"
#define SEARCH_TEXT "Would you like to search by [F]irst or [L]ast name? "
#line 1

void print_person(PERSON *p);
PERSON search_first_name(char* name);
PERSON search_last_name(char* name);
PERSON search_records();
void add_person();
void print_all();

PERSON records[ARRAY_SIZE];
int number_of_records = 0;

int main() {
	char input;
	printf(WELCOME_TEXT);
	while (input != 'Q') {
		printf(PROMPT);
		scanf("%c", &input);
		if (input >= 'a' && input <= 'z') {input -= 32;}
		if (input == 'A') {add_person();}
		else if (input == 'S') {search_records();}
		else if (input != 'Q') {printf(ERROR);}
	}
	print_all();
}

void print_person(PERSON *ptr) {
	printf("First name: %s\n", ptr->first_name);
	printf("Last name: %s\n", ptr->last_name);
	printf("Phone Number: %s\n", ptr->phone_number);
	printf("Age: %d\n", ptr->age);
}

void add_person() {
	PERSON temp;
	printf("\nFirst Name - ");
	scanf("%s", &(temp.first_name));
	printf("Last Name - ");
	scanf("%s", &(temp.last_name));
	printf("Phone Number - ");
	scanf("%s", &(temp.phone_number));
	printf("Age - ");
	scanf("%d", &(temp.age));
	
	records[number_of_records++] = temp;
}

PERSON search_first_name(char* name) {
	int i;
	printf("woah");
	for (i = 0 ; i != number_of_records ; i++) {
		if (strstr(records[i].first_name, name)) {return records[i];}
	}
}

PERSON search_last_name(char* name) {
	int i;
	for (i = 0 ; i != number_of_records ; i++) {
		if (strstr(records[i].last_name, name)) {return records[i];}
	}
}


PERSON search_records() {
	char type;
	char* search_text;
	PERSON result;

	printf(SEARCH_TEXT);
	scanf("%c", &type);
	//printf("Input string to search for ");
	//scanf("%s", search_text);

	if (type >= 'a' && type <= 'z') {type -= 32;}
	if (type == 'F') {result = search_first_name(search_text);}
	else if (type == 'L') {result = search_last_name(search_text);}

	print_person(&result);
}

void print_all() {
	int i;
	PERSON *current;
	printf(PRINT_ALL);
	for (i = 0 ; i != number_of_records ; i++) {
		current = &records[i];
		print_person(current);
		printf("\n");
	}
}
This produces the following output:


Welcome to the person database!

Would you like to [A]dd a record, [S]earch for a record, or [Q]uit? a

First Name - gordon
Last Name - ross
Phone Number - 342343
Age - 19
Would you like to [A]dd a record, [S]earch for a record, or [Q]uit? You didnt enter a valid optionWould you like to [A]dd a record, [S]earch for a record, or [Q]uit? s
Would you like to search by [F]irst or [L]ast name? First name: e
Last name: ¿üöÿ¿òÑ@ð÷ÿ¿
Phone Number÷ÿ¿
Age: 875757568
Would you like to [A]dd a record, [S]earch for a record, or [Q]uit? q



Note the repetition of text in the prompt. Also, I never typed the 'e' that is in bold. When I press 'S' for search, the program somehow reads an 'e' from stdin, that shouldnt be there.

What have I done wrong?
Nodrog is offline   Reply With Quote
Unread 23 Nov 2002, 00:06   #2
meglamaniac
Born Sinful
 
meglamaniac's Avatar
 
Join Date: Nov 2000
Location: Loughborough, UK
Posts: 4,059
meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.
Forgotten to comment your program for a start.
Bad Nod! Bad!

As for anything else - I'm not too sure.
This looks like a more advanced version of the C coursework I'm working on at the moment (we have to create a simple "phonebook" type database to certain specifications set by the lecturer, but are not required to include a search) but you're doing it a very different way.

Incidentally, why are you #define-ing the messages rather than just storing them as strings? I'm not saying it's wrong - I'm just interested why, as you have considerably more experience than me...

__________________
Worth dying for. Worth killing for. Worth going to hell for. Amen.
meglamaniac is offline   Reply With Quote
Unread 23 Nov 2002, 00:19   #3
Nodrog
Registered User
 
Join Date: Jun 2000
Posts: 8,476
Nodrog has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.Nodrog has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.Nodrog has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.Nodrog has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.Nodrog has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.Nodrog has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.Nodrog has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.Nodrog has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.Nodrog has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.Nodrog has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.Nodrog has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.
Quote:
Originally posted by meglamaniac

Incidentally, why are you #define-ing the messages rather than just storing them as strings? I'm not saying it's wrong - I'm just interested why, as you have considerably more experience than me...

I doubt that, I just started learning C today, although I did get up to just past the hello world stage a few months ago. Im using #defines because preprocessors are cool. How are you doing it out of interest, given its pretty much the same thing you're doing.
Nodrog is offline   Reply With Quote
Unread 23 Nov 2002, 00:43   #4
meglamaniac
Born Sinful
 
meglamaniac's Avatar
 
Join Date: Nov 2000
Location: Loughborough, UK
Posts: 4,059
meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.
I don't have the code available here (I do have access to the network drive but the muppets don't map what you store on it under linux to windows so it's like having two seperate drives even though they're the same space).
I've only got up to the loading of records and the listing of them (I've only had like 3 hours on it).
It's also not pure C since they've decided to try and make it 'easier' for the newbies by using some c++, especially for i/o (hello cout,cin,fstream, etc).

it basicly goes:

Code:
<includes>
<global variables>
<fuction prototypes>

main {
  switch to do a menu
     case to load records
     case to list records
  while condition to restart menu or quit
}

load records function {
  opens file
  loads contents into struct array
  error checks etc
}

display records function {
   error checking stuff (are the any records loaded etc)
   loops through stuct array listing records
}
Theres more to it than that, but thats the best I can give you atm.

__________________
Worth dying for. Worth killing for. Worth going to hell for. Amen.
meglamaniac is offline   Reply With Quote
Unread 23 Nov 2002, 01:15   #5
Epcylon
Registered User
 
Join Date: Apr 2000
Location: Oslo, Norway
Posts: 78
Epcylon is a glorious beacon of lightEpcylon is a glorious beacon of lightEpcylon is a glorious beacon of lightEpcylon is a glorious beacon of lightEpcylon is a glorious beacon of light
Quote:
Originally posted by Nodrog
I doubt that, I just started learning C today, although I did get up to just past the hello world stage a few months ago. Im using #defines because preprocessors are cool. How are you doing it out of interest, given its pretty much the same thing you're doing.
Been a while since I did any c programming (why bother with low-level stuff when you can achive better results with python or java ) but iirc there is a significant difference between using defines and strings.

Could be this gets fixed by your compiler during optimization, but when you use defines, the string gets inserted in the code wherever the defined constant is used, while if you use a string constant to hold it, the string itself is only stored 1 place, and a reference to that location is used every time you use the string.

Now, when it comes to the rest of your code, scanf is evil! Use fgets instead, and I'm willing to bet you get better results almost instantly... (I could go dig up the link to why scanf is evil, but I'm tired and don't remember where I put it... )

There's probably more, but with the lack of comments and me not having coded c in years...
__________________
Epcylon
[R1]: noob | [R2]: B8S/ICD | [R3-5]: ICD | [R6]: HR | [R7-9.5]: HR/NoS |
[R10]: HR RecOff | [R10.5]: HR RO -> HR HC -> HR pe0n | [R11]: HR pe0n -> Leave of Absence |
[R12]: HR free-pe0n | [R13-]: HR pe0n
Epcylon is offline   Reply With Quote
Unread 23 Nov 2002, 02:59   #6
MT
/dev/zero
Retired Mod
 
MT's Avatar
 
Join Date: May 2000
Posts: 415
MT is an unknown quantity at this point
its not right, but its more right ..

Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define ARRAY_SIZE 100
#define WELCOME_TEXT "Welcome to the person database!\n\n"
#define PROMPT "Would you like to [A]dd a record, [S]earch for a record, or [Q]uit? "
#define ERROR "You didnt enter a valid option"
#define PRINT_ALL "\nHere are a list of all people in database:\n\n"
#define SEARCH_TEXT "Would you like to search by [F]irst or [L]ast name? "

typedef struct person {  
  char first_name[15];  
  char last_name[15];  
  char phone_number[20];  
  int age;}
  PERSON;

void print_person(PERSON*);
int search_first_name(PERSON*,char*);
int search_last_name(PERSON*,char*);
void search_records();
void add_person();
void print_all();

PERSON *records[ARRAY_SIZE];
int number_of_records = 0;

int main() {
  char input;
  printf(WELCOME_TEXT);
  while (input != 'Q') {
    printf(PROMPT);
    scanf("%c\n", &input);
    if (input >= 'a' && input <= 'z') 
      input -= 32;
    if (input == 'A') 
      add_person();
    if (input == 'S') 
      search_records();
    if (input != 'A' && input != 'S' && input != 'Q') 
      printf("\n%s",ERROR);
  }
  print_all();
  return 0;
}

void print_person(PERSON *ptr) {
  printf("First name: %s\n", ptr->first_name);
  printf("Last name: %s\n", ptr->last_name);
  printf("Phone Number: %s\n", ptr->phone_number);
  printf("Age: %d\n", ptr->age);
}

void add_person() {
  PERSON *temp = malloc(sizeof(PERSON));
  printf("\nFirst Name - ");
  scanf("%s\n", temp->first_name);
  printf("Last Name - ");
  scanf("%s\n", temp->last_name);
  printf("Phone Number - ");
  scanf("%s\n", temp->phone_number);
  printf("Age - ");
  scanf("%d\n", &temp->age);
  
  records[number_of_records++] = temp;
}

int search_first_name(PERSON *ptr,char* name) {
  int i;
  printf("woah");
  for (i = 0 ; i != number_of_records ; i++) {
    if (strstr(records[i]->first_name, name)) {ptr = records[i]; return 0;}
  }
  return 1;
}

int search_last_name(PERSON *ptr, char* name) {
  int i;
  for (i = 0 ; i != number_of_records ; i++) {
    if (strstr(records[i]->last_name, name)) {ptr = records[i]; return 0;}
  }
  return 1;
}


void search_records() {
  int rc;
  char type;
  char* search_text;
  PERSON *result;

  printf(SEARCH_TEXT);
  rc = scanf("%c\n", &type);
  printf("Input string to search for ");
  scanf("%s\n", search_text);

  if (type >= 'a' && type <= 'z') {type -= 32;}
  if (type == 'F') {
    if (search_first_name(result,search_text)) {
      printf("%s","Error - not found!\n");
      return;
    }
  }
  else if (type == 'L') 
    if (search_last_name(result,search_text)) {
      printf("%s","Error - not found!\n");
      return;
    }
  print_person(result);
}

void print_all() {
  int i;
  printf(PRINT_ALL);
  for (i = 0 ; i != number_of_records ; i++) {
    print_person(records[i]);
    printf("\n");
  }
}
always compile with -Wall (warn all), will tell you where there are norty things.

btw, its picking up \n as the char to read, thats some of the nortyness.
__________________
#linux : Home of Genius

<idimmu> ok i was chained to a desk with this oriental dude
MT is offline   Reply With Quote
Unread 23 Nov 2002, 08:52   #7
queball
Ball
 
queball's Avatar
 
Join Date: Oct 2001
Posts: 4,410
queball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so little
Quote:
Originally posted by MT

always compile with -Wall (warn all), will tell you where there are norty things.

btw, its picking up \n as the char to read, thats some of the nortyness.
Then instead of scanf("%c"), use scanf(" %c").
queball is offline   Reply With Quote
Unread 23 Nov 2002, 16:47   #8
djbass
mmm.. pills
 
djbass's Avatar
 
Join Date: Apr 2000
Location: Australia
Posts: 2,152
djbass has a reputation beyond reputedjbass has a reputation beyond reputedjbass has a reputation beyond reputedjbass has a reputation beyond reputedjbass has a reputation beyond reputedjbass has a reputation beyond reputedjbass has a reputation beyond reputedjbass has a reputation beyond reputedjbass has a reputation beyond reputedjbass has a reputation beyond reputedjbass has a reputation beyond repute
Personally, I'm a fan of getch() ..particularly as you're only dealing with single character input, it's a lot less messy.

=[DJ Bass]=
__________________
CSS : the result of letting artists design something only an engineer should touch.
djbass is offline   Reply With Quote
Unread 23 Nov 2002, 17:28   #9
Gayle29uk
Bitch
 
Join Date: Jun 2002
Location: North Yorkshire
Posts: 3,848
Gayle29uk is just really niceGayle29uk is just really niceGayle29uk is just really niceGayle29uk is just really nice
I never like the overhead of printf/scanf/derivatives either, both in terms of size and speed. They so make things simpler though
__________________
ACHTUNG!!!
Das machine is nicht fur gefingerpoken und mittengrabben. Ist easy
schnappen der springenwerk, blowenfusen und corkenpoppen mit
spitzensparken. Ist nicht fur gewerken by das dummkopfen. Das
rubbernecken sightseeren keepen hands in das pockets. Relaxen und vatch
das blinkenlights!!!
Gayle29uk is offline   Reply With Quote
Reply



Forum Jump


All times are GMT +1. The time now is 14:42.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2002 - 2018