We're a community of 1.1M IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,080,514 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Printing Structs

Okay, so pretty much I have to use structs to print out information about 4 dogs. However, I can only get the first dog, Smokey's information to print. Any advice on how to get the other four to print?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define pause system("pause")
#define SIZE 100

//Write your structs here
  typedef struct{
      char name[SIZE];
      int age;
      int weight;
      char breed[SIZE];
  } DOG;

//Prototype your functions here
  void displayDog(DOG);
  void loadDog(DOG *s);
  void loadDog2(DOG *s);
  void loadDog3(DOG *s);
  void loadDog4(DOG *s);

main(){
    DOG Smokey, Oscar, Leo, Jose;
    loadDog(&Smokey);
    loadDog(&Oscar);
    loadDog(&Leo);
    loadDog(&Jose);
    displayDog(Smokey);
    displayDog(Oscar);
    displayDog(Leo);
    displayDog(Jose);

    pause;

}//end main


void displayDog(DOG s){
    printf("Student name.....: %s. \n", s.name);
    printf("Age.....: %i. \n", s.age);
    printf("Weight.....: %i. \n", s.weight);
    printf("Breed.....: %s. \n\n", s.breed);
}//end displayDog


void loadDog(DOG *s){
    strcpy(s->name,"Smokey");
    s->age = 5;
    s->weight = 27;
    strcpy(s->breed,"Boston Terrier");
}
//end loadDog

void loadDog2(DOG *s){
    strcpy(s->name,"Oscar");
    s->age = 2;
    s->weight = 70;
    strcpy(s->breed,"German Shepard");
}
//end loadDog

void loadDog3(DOG *s){
    strcpy(s->name,"Leo");
    s->age = 6;
    s->weight = 50;
    strcpy(s->breed,"Siberian Husky");
}
//end loadDog

void loadDog4(DOG *s){
    strcpy(s->name,"Jose");
    s->age = 1;
    s->weight = 60;
    strcpy(s->breed,"Dalmation");
}
//end loadDog
2
Contributors
1
Reply
10 Minutes
Discussion Span
3 Months Ago
Last Updated
11
Views
samii1017
Newbie Poster
8 posts since Oct 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

You are using the same function, loadDog, to put data into all the four dog objects, so you are loading the Smokey data into all four. Perahps you meant to use each loading function once (loadDog, loadDog2, loadDog3, loadDog4).

Moschops
Posting Shark
949 posts since Sep 2008
Reputation Points: 317
Solved Threads: 180
Skill Endorsements: 5

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page generated in 0.0583 seconds using 2.67MB