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

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




phone number as a structure

2 Pages V  1 2 >  
Reply to this topicStart new topic

phone number as a structure

ser
3 Nov, 2006 - 05:17 AM
Post #1

D.I.C Head
**

Joined: 29 Oct, 2006
Posts: 56


My Contributions
A PHONE NUMBER SUCH AS (212)-767-8900 CAN BE THOUGHT OF AS HAVING THREE PARTS THE AREA CODE (212), THE EXCHANGE (767) AND THE NUMBER (8900). WRITE A PROGRAM THAT USES A STRUCTURE TO STORE THREE PARTS OF A NUMBER SEPARATELY. CALL THE PHONE STRUCTURE PHONE. CREATE TWO STRUCTURE VARIABLES OF PHONE TYPE. INITIALIZE ONE AND HAVE THE USER INPUT A NUMBER FOR THE OTHER ONE AND DISPLAY. THIS IS THE CODE THAT I HAVE DONE SO FAR:
CODE

#include<iostream>

struct  phone
{
    int areacode;
    int exchange;
    int number;
};

void main ()
{
    phone1;
    phone1.areacode = 415;
    phone1.exchange = 555;
    phone1.number =1212;

    cout<<"enter your area code (121),exchange(767) and your number(8900)"<<phone1.areacode.exchange.number<<endl;
    cout<<"my number is 212 767 8900"<<endl
        cout<<"your number is:"<<phone1.areacode.exchange.number<<endl;
}

return 0


edit: fixed [code] tags ~ jayman9
User is offlineProfile CardPM
+Quote Post

NyeNye
RE: Phone Number As A Structure
3 Nov, 2006 - 05:34 AM
Post #2

D.I.C Head
**

Joined: 24 Sep, 2006
Posts: 248


My Contributions
This your mistake

CODE
void main ()
{
phone1;
phone1.areacode = 415;
phone1.exchange = 555;
phone1.number =1212;


declare your struct on your main...

CODE
void main ()
{
phone phone1;
phone1.areacode = 415;
phone1.exchange = 555;
phone1.number =1212;


is just like a class declaration
User is offlineProfile CardPM
+Quote Post

ser
RE: Phone Number As A Structure
3 Nov, 2006 - 05:45 AM
Post #3

D.I.C Head
**

Joined: 29 Oct, 2006
Posts: 56


My Contributions
i ahve done the changes taht you have said but it still does not work as i have some errors but i dont know where
User is offlineProfile CardPM
+Quote Post

gregoryH
RE: Phone Number As A Structure
3 Nov, 2006 - 05:50 AM
Post #4

D.I.C Regular
Group Icon

Joined: 4 Oct, 2006
Posts: 417


Dream Kudos: 50
My Contributions
QUOTE(ser @ 3 Nov, 2006 - 06:45 AM) *

i ahve done the changes taht you have said but it still does not work as i have some errors but i dont know where

Hi Ser

if you post the compiler error messages, we might be better able to guide you
User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Phone Number As A Structure
3 Nov, 2006 - 06:06 AM
Post #5

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,230



Thanked: 40 times
Dream Kudos: 25
My Contributions
CODE

cout<<"enter your area code (121),exchange(767) and your number(8900)"<<phone1.areacode.exchange.number<<endl;
cout<<"my number is 212 767 8900"<<endl
cout<<"your number is:"<<phone1.areacode.exchange.number<<endl;

You are attempting to access the members of the struct through each other...they are only members of the struct, not of each other. It should look somehing like:
CODE


cout<<"my number is 212 767 8900"<<endl
cout<<"your number is:"<<phone1.areacode<<" "<<phone1.exchange<<" "<<phone1.number<<endl;


User is offlineProfile CardPM
+Quote Post

ser
RE: Phone Number As A Structure
3 Nov, 2006 - 07:29 AM
Post #6

D.I.C Head
**

Joined: 29 Oct, 2006
Posts: 56


My Contributions
i have added the changes but i seem to have an error but i'm not sure were .
[code]
#include<iostream>

struct phone
{
int areacode;
int exchange;
int number;
};

void main ();
{
phone phone1;
phone1.areacode = 415;
phone1.exchange = 555;
phone1.number =1212;

cout<<"enter your area code (121),exchange(767) and your number(8900)"<<phone1.areacode.exchange.number<<endl;
cout<<"my number is 212 767 8900"<<endl
cout<<"your number is:"<<phone1.areacode<<" "<<phone1.exchange<<" "<<phone1.number<<endl;
}

return 0
User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Phone Number As A Structure
3 Nov, 2006 - 08:23 AM
Post #7

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,230



Thanked: 40 times
Dream Kudos: 25
My Contributions
If you looked at the error message your compiler gives you, it will indicate the line on which you have an error.
This:
CODE

cout<<"enter your area code (121),exchange(767) and your number(8900)"<<phone1.areacode.exchange.number<<endl;

needs to have the same changes made as the other line...it is exactly the same issue. areacode,exchange and number are members of the phone1 structure, not each other.

You also have your return 0; outside of the main function...make it the last line INSIDE the main function.

It may be beneficial in the future to post the error messages you receive.
User is offlineProfile CardPM
+Quote Post

ser
RE: Phone Number As A Structure
3 Nov, 2006 - 08:34 AM
Post #8

D.I.C Head
**

Joined: 29 Oct, 2006
Posts: 56


My Contributions
i still have an error message
CODE

#include<iostream>

struct phone
{
int areacode;
int exchange;
int number;
};

void main ();
{
phone phone1;
phone1.areacode;
phone1.exchange;
phone1.number;

cout<<"enter your area code (121),exchange(767) and your number(8900)"<<phone1.areacode<<" "<<phone1.exchange<<" "<<phone1.number<<endl;
cout<<"my number is 212 767 8900"<<endl
cout<<"your number is:"<<phone1.areacode<<" "<<phone1.exchange<<" "<<phone1.number<<endl;


return 0
}


EDIT : Added code Tags - b2c
User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Phone Number As A Structure
3 Nov, 2006 - 08:37 AM
Post #9

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,230



Thanked: 40 times
Dream Kudos: 25
My Contributions
void main ();
The decalaration of the main function does not end with a semi colon. It should be
void main()
or
int main()

You are also not assigning any values to the structure members.

Again, PLEASE post the error message when you are posting about errors.
User is offlineProfile CardPM
+Quote Post

ser
RE: Phone Number As A Structure
3 Nov, 2006 - 09:16 AM
Post #10

D.I.C Head
**

Joined: 29 Oct, 2006
Posts: 56


My Contributions
this is the error message that i have

-------------------Configuration: 1 - Win32 Debug--------------------
Compiling...
error C2146: syntax error : missing ';' before identifier 'cout'
warning C4508: 'main' : function should return a value; 'void' return type assumed

and sorry i dont understand what you mean as it is my first time using c++

[code]
#include<iostream>


struct phone
{
int areacode;
int exchange;
int number;
};

int main ()
{
phone phone1;
phone1.areacode;
phone1.exchange;
phone1.number;

cout<<"enter your area code (121),exchange(767) and your number(8900)"<<phone1.areacode<<" "<<phone1.exchange<<" "<<phone1.number<<endl;
cout<<"my number is 212 767 8900"<<endl
cout<<"your number is:"<<phone1.areacode<<" "<<phone1.exchange<<" "<<phone1.number<<endl;


return 0;
}

User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Phone Number As A Structure
3 Nov, 2006 - 10:02 AM
Post #11

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,230



Thanked: 40 times
Dream Kudos: 25
My Contributions
cout<<"my number is 212 767 8900"<<endl needs to be terminated with a semi colon.
User is offlineProfile CardPM
+Quote Post

ser
RE: Phone Number As A Structure
4 Nov, 2006 - 09:30 AM
Post #12

D.I.C Head
**

Joined: 29 Oct, 2006
Posts: 56


My Contributions
THANKS 4 YOUR HELP THE CODE KNOW WORKS WITH MY SOME OF MY CHANGES THAT I HAVE DONE.
User is offlineProfile CardPM
+Quote Post

2 Pages V  1 2 >
Reply to this topicStart new topic
Time is now: 12/4/08 06:54PM

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