Welcome to Dream.In.Code
Become a C++ Expert!

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




how to use strucure variables in a called function?

 
Reply to this topicStart new topic

how to use strucure variables in a called function?, structuer + function in the main()

curiose
11 Dec, 2007 - 04:10 AM
Post #1

D.I.C Head
**

Joined: 28 Oct, 2007
Posts: 94


My Contributions
I'm trying to call a function in the main()....using the variables in the structure.... blink.gif but i have an error anb i'm unable to correct it....i will be glad if u help me in correcting them...this is my code:
CODE

  #include<stdio.h>
//#include<conion.h>
void main()
    {

    struct customer
    {
    char name[10],street[10],id[5];
    long int mobile,pn,pob;
    }c1;

    printf("Please enter the customer ID\t");
    gets(c1.id);
    printf("\nPlease enter the customer name\t");
    gets(c1.name);
    printf("\nPlease enter the customer street\t");
    gets(c1.street);
    printf("\nPlease enter the customer mobilephone number\t");
    scanf("%li",&c1.mobile);
    fflush(stdin);
    printf("\nPlease enter the customer passport number\t");
    scanf("%li",&c1.pn);
    printf("\nPlease enter the customer post box numbe\t");
    scanf("%li",&c1.pob);    
                void display(char,char,char,long int,long int, long int);
    display(c1.id,c1.name,c1.street,c1.mobile,c1.pn,c1.pob);
                }

void display(char a,char b,char c,long int d,long int e, long int f)
    {
    //clrscr();
                printf("\nThe customer ID is %s",a);
    printf("\nThe customer name is %s",B);
    printf("\nThe customer street is %s",c);
    printf("\nThe customer  mobile phone numberis %li",d);
    printf("\nThe customer passport number is %li",e);
    printf("\nThe customer post box number is %li",f);
    
    }

i have encluded my message of the syntax error..


Attached thumbnail(s)
Attached Image
User is offlineProfile CardPM
+Quote Post

Amadeus
RE: How To Use Strucure Variables In A Called Function?
11 Dec, 2007 - 05:30 AM
Post #2

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,355



Thanked: 51 times
Dream Kudos: 25
My Contributions
First and foremost, your structure should be declared outside of the main function, but the problems you are getting as far as errors go is because the structure variables name, street, and id are all character arrays (multiple characters) while your function display() is expecting name, street, and id to being single characters. you'll need to modify your function to accept character arrays (or pointers).
User is online!Profile CardPM
+Quote Post

curiose
RE: How To Use Strucure Variables In A Called Function?
11 Dec, 2007 - 10:43 AM
Post #3

D.I.C Head
**

Joined: 28 Oct, 2007
Posts: 94


My Contributions
OK...it runs now but with anew problem...that's im not getting the right inputs that i have entered
i have made this change in to my code
[code]
void display(char,char,char,long int,long int, long int) ;
display(c1.id[5],c1.name[10],c1.street[10],c1.mobile,c1.pn,c1.pob);
}[\code]
and i have kept the structure inside.....
i made anew code with the sturcture outside the main,but what if i have more than one structure??and i want one of them to be in the main???

i would be grateful if someone give me the hint of fixing this...
User is offlineProfile CardPM
+Quote Post

Amadeus
RE: How To Use Strucure Variables In A Called Function?
11 Dec, 2007 - 10:52 AM
Post #4

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,355



Thanked: 51 times
Dream Kudos: 25
My Contributions
You need to change this:
CODE

void display(char,char,char,long int,long int, long int);

to this:
CODE

void display(char [],char[],char[],long int,long int, long int);

and keep this:
CODE

display(c1.id,c1.name,c1.street,c1.mobile,c1.pn,c1.pob);

You should declare your function prototype outside main as well.

As for the structure, you can declare the structure itself outside of main, and create the instances inside.
User is online!Profile CardPM
+Quote Post

curiose
RE: How To Use Strucure Variables In A Called Function?
11 Dec, 2007 - 01:20 PM
Post #5

D.I.C Head
**

Joined: 28 Oct, 2007
Posts: 94


My Contributions
yah i have got the answer when i add the structure and the function outside the main....but i didnt use parameters in side ... my maintained code is:
CODE

struct customer
    {
    char name[10],street[10],id[5];
    long int mobile,pn,pob;
    }c1;

void display();
void enter();
void main()
    {
int x,option;
         do
    {
    printf("\nDo you want to:\n \t1.Enter Details \t2.Display Details");
    printf("\nPlease press 1 or 2");
    scanf("%d",&option);
    getchar();
    switch(option)
    {
    case 1:enter();break;
    case 2:display();break;
    default:printf("\nInvalid Option");
    }
        if (c1.id>=c1.id)
    {
    printf("\n\nId can accept only 5 characters");
         }

       printf("\n______Do you want to add anew customer?_______\n\t1.yes\t2.no");
       scanf("%i",&x);
          }while(x==1);
    }



void enter()
    {
        //clrscr();
        printf("Please enter the customer ID\t");
    gets(c1.id);
    printf("\nPlease enter the customer name\t");
    gets(c1.name);
    printf("\nPlease enter the customer street\t");
    gets(c1.street);
    printf("\nPlease enter the customer mobilephone number\t");
    scanf("%li",&c1.mobile);
    fflush(stdin);
    printf("\nPlease enter the customer passport number\t");
    scanf("%li",&c1.pn);
    fflush(stdin);
    printf("\nPlease enter the customer post box numbe\t");
    scanf("%li",&c1.pob);
        display();
        }
      


void display()
    {
    //clrscr();
        printf("\nThe customer ID is %s",c1.id);
    printf("\nThe customer name is %s",c1.name);
    printf("\nThe customer street is %s",c1.street);
    printf("\nThe customer  mobile phone numberis %li",c1.mobile);
    printf("\nThe customer passport number is %li",c1.pn);
    printf("\nThe customer post box number is %li",c1.pob);
    }



it works, but i was trying to gather all the ways in doing this code....
thanx
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/8/09 04:57PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

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