Help me with this basic c program

This is a discussion on Help me with this basic c program within the C Programming forums, part of the General Programming Boards category; hello, im actually a diploma student. i have a C Programmming assignment that i've to pass up early next month ...

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    1

    Unhappy Help me with this basic c program

    hello,

    im actually a diploma student. i have a C Programmming assignment that i've to pass up early next month (March,2002).

    i'm having problem with this program:/* create.c */
    /* create a friend file */

    #include "mylib.c"
    #include <stdio.h>

    /* declare a simple record */
    struct FriendRec {
    int FriendID;
    char Nick[10];
    char Name[20];
    char Address[25];
    char Email[20];
    char Tel[15];
    int Month;
    int Day;

    };

    /* declare global working var */
    struct FriendRec Friend;
    int iKount = 0;
    char cKont = 'Y';

    /* indicates FILE pointer */
    FILE *fPtr;

    main() {

    /* open file for writing */
    fPtr = fopen("friend.dat", "w");

    if (fPtr == NULL) {

    /* dispaly file creation error */
    printf("\nUnable to open \"friend.dat\"! Aborts operation!");

    } else {
    do {
    DispTitle();
    GetFriendDetails();
    StoreInFile();
    GetKontinueYN();
    } while (cKont=='Y' || cKont=='y');

    CloseFile();

    TempHalt();
    }
    }

    DispTitle() {
    /* display title */
    ClrRow(3);
    printf("\nThis program is to create a file with details of friend. ");
    printf("\nUser have to key in friend information that will later");
    printf("\nbe stored in a file named \"friend.dat\".");
    ClrRow(1);
    }

    GetFriendDetails() {

    /* assigns friendid*/
    Friend.FriendID = 1001 + iKount;
    printf("\nFriend Id : %d", Friend.FriendID);
    iKount++;

    /* get friend nick name */
    printf("\nNick name: ");
    gets(Friend.Nick);

    /* get friend name */
    printf("\nName: ");
    gets(Friend.Name);

    /* get friend address */
    printf("\nAddress: ");
    gets(Friend.Address);

    /* get friend e-mail address */
    printf("\nE-Mail: ");
    gets(Friend.Email);

    /* get friend telephone number */
    printf("\nTel.Number : ");
    gets(Friend.Tel);

    /* get friend month of birth */
    do {
    printf("\nMonth Of Birth: ");
    scanf("%d", &Friend.Month);
    if (Friend.Month > 12 || Friend.Month < 0) {
    printf("Invalid month!Between 0 - 12 pls!");
    }
    } while (Friend.Month > 12 || Friend.Month < 0);

    /* get friend day of birth */
    do {
    printf("\nDay Of Birth: ");
    scanf("%d", &Friend.Day);
    if (Friend.Day > 31 || Friend.Day < 0) {
    printf("Invalid day!Between 0 - 31 pls!");
    }
    } while (Friend.Day > 31 || Friend.Day < 0);

    }

    GetKontinueYN() {
    /* get permission to Kontinue */
    printf("Input next friend? [Y/N] : ");
    cKont=getche();
    ClrRow(1);
    }

    StoreInFile() {
    fwrite(&Friend, sizeof(struct FriendRec), 1, fPtr);
    }

    CloseFile() {
    /* close file */
    fclose(fPtr);
    printf("\nFile \"friend.dat\" is successfully appended!");
    }

    ==============================================
    The problem is, when u choose to input next friend, the cursor jumps from Friend ID to Name. I'm unable to input the nickname for the second person.

    i've tried to change all possible codes but it doesnt work. this code is the same as the one that my lecturer gave me (on another lesson).

    i really need someone to help me out. im really stressed now since the completion date is just around the corner.

    Please Help!

  2. #2
    Registered User
    Join Date
    Aug 2001
    Posts
    202
    Well, to begin with, the decleration of main is one of the following:

    int main(void){
    int main(int argc, char *argv[]){
    int main(int argc, char *argv[], char **env){

    Before you fix anything else fix that.

    Secondly, try fixing the mode of fopen from w to a (append), which will add to the file instead of over-writing it each time the program is called (if this is an issue), in which case you'll also need to read the last line to get the highest number record.

    I would also advise using fgets() instead of gets(), it provides for safer string reading.

    But to your problem...

    I see that you're using a getche() method, and can honestly say that I have never heard of it. try getchar(); instead.

    Hope this helps

    --starX
    www.axisoftime.com

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 01:27 PM
  2. Basic encryption program???
    By Finchie_88 in forum C++ Programming
    Replies: 14
    Last Post: 09-10-2004, 10:01 AM
  3. basic animation program
    By Noobie in forum C++ Programming
    Replies: 16
    Last Post: 02-17-2003, 06:33 PM
  4. IDEA: A basic drawing program
    By ygfperson in forum Contests Board
    Replies: 0
    Last Post: 08-13-2002, 12:15 AM
  5. Replies: 2
    Last Post: 05-10-2002, 05:16 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21