problem in struct string

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • lye85
    New Member
    • Sep 2009
    • 3

    problem in struct string

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>


    struct account {
    char AccName[20];
    int Age;
    double AccBalance;
    struct account *Next;
    };

    typedef struct account BankAcc;

    void insert(struct account **headOfList, char *name, int age, double balance);
    int Delete(struct account **headOfList, char *name );
    void print(struct account *headOfList );
    void printAbove50(st ruct account *headOfList);


    void main() {
    BankAcc *startPtr = NULL;

    int selection;
    printf("Enter selection and following below: \n");
    printf("1. Insert Account \n2. Delete Account \n3. Print All \n4. Print Age > 50 \n");
    printf("Enter your selection : ");
    scanf("%d",&sel ection);

    while(selection != 5) {
    switch(selectio n) {
    case 1:
    char *Username;
    char TmpName[20];
    int UserAge;
    double UserBalance;



    // Enter Account Name
    printf("Enter Account Name : ");
    scanf("%s",&Tmp Name);
    Username = TmpName;
    // Enter Age
    printf("Enter Age : ");
    scanf("%d",&Use rAge);
    // Enter Account Balance
    printf("Enter Account Balance : ");
    scanf("%d",&Use rBalance);

    insert(&startPt r,Username,User Age,UserBalance );
    break;

    case 2:
    char *del;
    char tmpdelete[20];

    printf("Enter Account Name : ");
    scanf("%s",&tmp delete);
    del = tmpdelete;
    Delete(&startPt r,del);
    break;

    case 3:
    print(startPtr) ;
    break;

    case 4:
    printAbove50(st artPtr);
    break;
    }

    printf("---------------------------------------------------------\n");
    printf("Enter selection and following below: \n");
    printf("1. Insert Account \n2. Delete Account \n3. Print All \n4. Print Age > 50 \n");
    printf("Enter your selection : ");
    scanf("%d",&sel ection);
    }

    }

    void insert(struct account **headOfList, char *name, int age, double balance) {
    struct account *newPtr;
    struct account *previousPtr;
    struct account *currentPtr;

    newPtr = (account*) malloc(sizeof(a ccount));

    if(newPtr != NULL) {

    //newPtr->AccName = (char *)malloc(40 * sizeof(char));
    strcpy (newPtr->AccName,name );

    newPtr->Age = age;
    newPtr->AccBalance = balance;
    newPtr->Next = NULL;


    previousPtr = NULL;
    currentPtr = *headOfList;



    while(currentPt r != NULL && balance < currentPtr->AccBalance) {
    previousPtr = currentPtr;
    currentPtr = currentPtr->Next;
    }

    if(previousPtr == NULL) {
    newPtr->Next = *headOfList;
    *headOfList = newPtr;
    } else {
    previousPtr->Next = newPtr;
    newPtr->Next = currentPtr;
    }
    }

    //printf("%s\n",h eadOfList->AccName);

    }


    int Delete(struct account **headOfList, char *name ){



    char mm[20];
    strcpy (mm,name);
    printf("%s\n",m m);

    char m2[20];
    strcpy (m2,(*headOfLis t)->AccName);
    printf("%s\n",m 2);
    //printf("%s\n",( *headOfList)->AccName);

    if(mm == m2){
    printf("Valid\n ");
    }





    return 0;

    }

    void print(struct account *headOfList ) {
    if(headOfList == NULL) {
    printf("List is empty. \n");
    } else {
    printf("Account Name\tAge\tAcco unt Balance\n");
    printf("_______ _______________ _______________ ____________\n" );

    while(headOfLis t != NULL) {
    printf("%s\t%d\ t%d\n",headOfLi st->AccName,headOf List->Age,headOfLi st->AccBalance);
    headOfList = headOfList->Next;
    }
    }
    }


    void printAbove50(st ruct account *headOfList ) {
    if(headOfList == NULL) {
    printf("List is empty. \n");
    } else {
    printf("Account Name\tAge\tAcco unt Balance\n");
    printf("_______ _______________ _______________ ____________\n" );

    while(headOfLis t != NULL) {
    if(headOfList->Age > 50){
    printf("%s\t%d\ t%d\n",headOfLi st->AccName,headOf List->Age,headOfLi st->AccBalance);
    }
    headOfList = headOfList->Next;
    }
    }
    }



    in my Delete function, if(mm == m2){ printf("Valid\n ");} , Valid cannot be printed when mm & m2 have same value, some expert pls help me on this...
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    Originally posted by lye85
    in my Delete function, if(mm == m2){ printf("Valid\n ");} , Valid cannot be printed when mm & m2 have same value, some expert pls help me on this...
    No the code says the opposite, Valid will only be printed when mm and m2 have the same value. However mm and m2 can never have the same value so Valid can never be printed.

    However mm == m2 compares the values of the address of those 2 arrays. If you actually wanted to compare the 2 strings they contained you would need to call strcmp.

    Comment

    Working...