Thread: How does this not work? (structures)

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    1

    Question How does this not work? (structures)

    Hi folks,

    I'm pretty new to C++ and I tried to write a little something, just to try out my newly aqquired skills- But somehow it doesnt work and I dont quiet get the error-messages. Could somebody please have a look at this?

    best wishes Alekij

    Code:
    #include <iostream>
    
    using namespace std;
    
    struct data
    {
      int id;
      int age;
      int phone;
    }
    
    void print(data)
    {
      cout<<"ID:  " <<data.id <<"\n" <<"Age:  " <<data.age << "\n" <<"Phone:  " <<data.phone;
    }
    
    int main()
    {
      data alex;
      alex.id = 1;
      alex.age = 23;
      alex.phone = 123456;
      data tesa;
      tesa.id = 2;
      tesa.age = 26;
      tesa.phone = 234567;
      cout<<"Bitte geben sie eine Person ein.\n" <<"1   Alex\n" <<"2    Tesa\n";
      int input;
      switch(input)
      {
          case 1:
            print(alex);
            break;
          case 2:
            print(tesa);
            break;
      }
      cin.get();
    }

  2. #2
    Registered User
    Join Date
    Apr 2009
    Posts
    11
    When declaring a struct you need to add a semicolon after the declaration, for example:

    Code:
    struct data
    {
      int id;
    }; <-- notice the semicolon
    Also you've declared the data type being passed to your print function, but not its name. Use this instead:
    Code:
    void print(data currentdata)
    {
      cout<<"ID:  " <<currentdata.id <<"\n" <<"Age:  " <<currentdata.age << "\n" <<"Phone:  " <<currentdata.phone;
    }
    Try reading the compiler error messages, they are usually pretty handy at identifying the source of the problem even though its quite difficult at first.

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Code:
    int input;
      switch(input)
    You're also not getting any input here. You need to get input from the user (through cin), then enter the switch.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems with Nested Structures and Arrays of Structures
    By Ignoramus in forum C Programming
    Replies: 4
    Last Post: 03-02-2010, 01:24 AM
  2. Accessing Structures Inside Structures
    By Mellowz in forum C Programming
    Replies: 1
    Last Post: 01-13-2008, 03:55 AM
  3. How to work with structures in functions.
    By kamoda_pawel in forum C Programming
    Replies: 3
    Last Post: 01-15-2007, 06:22 AM
  4. Structures, passing array of structures to function
    By saahmed in forum C Programming
    Replies: 10
    Last Post: 04-05-2006, 11:06 PM
  5. Replies: 5
    Last Post: 04-11-2002, 11:29 AM

Tags for this Thread