Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,480 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,704 people online right now. Registration is fast and FREE... Join Now!




[C] using strncpy

 
Reply to this topicStart new topic

[C] using strncpy

mtber
6 Mar, 2008 - 12:12 PM
Post #1

New D.I.C Head
*

Joined: 28 Nov, 2007
Posts: 5


My Contributions
I am trying to replace the input from the user in the function readEmployee and replace that with the information that is in struct employee e. Does anyone know if that is possible. i am trying to get the output to be whatever the user inputs in the readEmployee function and to do that it needs to replace the information that i have set as a test in the struct employee e. If anyone can help that would be great. Thanks,

CODE

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


struct employee
{
    char *name;
    char ssn[9];
    int YearBorn;
    int salary;
};

struct employee e = {"Kyle", "689543598", 1900, 100000};

void display(struct employee e)
{
    printf("\n%s , %3.3s-%2.2s-%4.4s , %d\n", e.name, e.ssn, e.ssn + 3, e.ssn + 5, e.YearBorn);
    printf("$%d,%.3d\n", e.salary/1000, e.salary%1000);
    
}

void readEmployee(struct employee e)
{
    e.name = (char*)malloc(100);
    printf("\nEmployee Name: ");
    gets(e.name);
    strncpy(*name, e.name);
    printf("\nEmployee SSN: ");
    gets(e.ssn);
    strncpy(ssn, e.ssn);
    printf("\nEnter Year you were born: ");
    scanf("%d", &e.YearBorn);
    strncpy(YearBorn, e.YearBorn);
    printf("\nPlease enter Employee Salary: ");
    scanf("%d", &e.salary);
    strncpy(salary, e.salary);
}

int main()
{
    readEmployee(e);
    display(e);
    return 0;
}

User is offlineProfile CardPM
+Quote Post

pbl
RE: [C] Using Strncpy
6 Mar, 2008 - 01:30 PM
Post #2

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,116



Thanked: 202 times
Dream Kudos: 75
My Contributions


CODE


void readEmployee(struct employee e)
{
    e.name = (char*)malloc(100);
    printf("\nEmployee Name: ");
    gets(e.name);
                // it is already in e.name no need to copy
                // and name is not defined
    // strncpy(*name, e.name);
    printf("\nEmployee SSN: ");
    gets(e.ssn);
                // same thing
    // strncpy(ssn, e.ssn);
    printf("\nEnter Year you were born: ");
    scanf("%d", &e.YearBorn);
                // same thing and strncpy is for "string copy"
                // e.YearBorn is a int
    // strncpy(YearBorn, e.YearBorn);
    printf("\nPlease enter Employee Salary: ");
    scanf("%d", &e.salary);
                // you scanf it into e.salary
    // strncpy(salary, e.salary);
}


User is online!Profile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/2/08 06:49PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month