14 Replies - 1959 Views - Last Post: 05 November 2006 - 06:47 PM Rate Topic: -----

#1 ser  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 57
  • Joined: 29-October 06

phone number as a structure

Posted 03 November 2006 - 06:17 AM

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:
#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

Is This A Good Question/Topic? 0
  • +

Replies To: phone number as a structure

#2 NyeNye  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 248
  • Joined: 24-September 06

Re: phone number as a structure

Posted 03 November 2006 - 06:34 AM

This your mistake

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


declare your struct on your main...

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


is just like a class declaration
Was This Post Helpful? 0
  • +
  • -

#3 ser  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 57
  • Joined: 29-October 06

Re: phone number as a structure

Posted 03 November 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
Was This Post Helpful? 0
  • +
  • -

#4 gregoryH  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 60
  • View blog
  • Posts: 656
  • Joined: 04-October 06

Re: phone number as a structure

Posted 03 November 2006 - 06:50 AM

View Postser, on 3 Nov, 2006 - 06:45 AM, said:

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
Was This Post Helpful? 0
  • +
  • -

#5 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: phone number as a structure

Posted 03 November 2006 - 07:06 AM

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:

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


Was This Post Helpful? 0
  • +
  • -

#6 ser  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 57
  • Joined: 29-October 06

Re: phone number as a structure

Posted 03 November 2006 - 08:29 AM

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
Was This Post Helpful? 0
  • +
  • -

#7 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: phone number as a structure

Posted 03 November 2006 - 09:23 AM

If you looked at the error message your compiler gives you, it will indicate the line on which you have an error.
This:
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.
Was This Post Helpful? 0
  • +
  • -

#8 ser  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 57
  • Joined: 29-October 06

Re: phone number as a structure

Posted 03 November 2006 - 09:34 AM

i still have an error message
#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
Was This Post Helpful? 0
  • +
  • -

#9 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: phone number as a structure

Posted 03 November 2006 - 09:37 AM

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.
Was This Post Helpful? 0
  • +
  • -

#10 ser  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 57
  • Joined: 29-October 06

Re: phone number as a structure

Posted 03 November 2006 - 10:16 AM

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;
}
Was This Post Helpful? 0
  • +
  • -

#11 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: phone number as a structure

Posted 03 November 2006 - 11:02 AM

cout<<"my number is 212 767 8900"<<endl needs to be terminated with a semi colon.
Was This Post Helpful? 0
  • +
  • -

#12 ser  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 57
  • Joined: 29-October 06

Re: phone number as a structure

Posted 04 November 2006 - 10:30 AM

THANKS 4 YOUR HELP THE CODE KNOW WORKS WITH MY SOME OF MY CHANGES THAT I HAVE DONE.
Was This Post Helpful? 0
  • +
  • -

#13 ihatepikingnames  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 119
  • Joined: 31-July 06

Re: phone number as a structure

Posted 05 November 2006 - 06:19 PM

[quote name='ser' date='3 Nov, 2006 - 10:34 AM' post='180713']
i still have an error message
#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 <---------------------------------------------------------------------*
}



If a function returns void its returns nothing
if you want it to return 0 (int)
int main()
return char;
char main()
...
Was This Post Helpful? 0
  • +
  • -

#14 NyeNye  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 248
  • Joined: 24-September 06

Re: phone number as a structure

Posted 05 November 2006 - 06:40 PM

ihatepikingnames is right but the thread is solve already...



nice job to all
Was This Post Helpful? 0
  • +
  • -

#15 ihatepikingnames  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 119
  • Joined: 31-July 06

Re: phone number as a structure

Posted 05 November 2006 - 06:47 PM

You never know when someone else might have the same problem...
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1