RSS Forums RSS
Please support our C++ advertiser: Programming Forums
Views: 3636 | Replies: 9 | Thread Tools  Display Modes
Reply
Join Date: Aug 2005
Posts: 14
Reputation: m7r23 is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
m7r23 m7r23 is offline Offline
Newbie Poster

the type def statement and enum

  #1  
Sep 7th, 2005
I have no idea how to use these two. I tried for 2 hours. And I also read the c++ books, but no luck. Can some body start me out.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Apr 2004
Posts: 3,809
Reputation: Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light 
Rep Power: 17
Solved Threads: 147
Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: the type def statement and enum

  #2  
Sep 7th, 2005
What's your question?

[edit]

"An enumeration comprises a set of named integer constant values. Each distinct enumeration constitutes a different enumerated type."

typedef
High Plains Blogger #plains #lounge ## I, for one, welcome our new socialist overlords.
"Capitalism is the unequal distribution of wealth. Socialism is the equal distribution of poverty."
Reply With Quote  
Join Date: Aug 2005
Posts: 14
Reputation: m7r23 is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
m7r23 m7r23 is offline Offline
Newbie Poster

Re: the type def statement and enum

  #3  
Sep 7th, 2005
OK, I will start with one. I will start with how do I use the type statement.In general I need to input two sets (of values) of user-defined data types:
One type named Sign declared by "typedef" to contain only
either +10 or -10. I started my program. I have an attachement. I just dont know how to use it in the program.
Reply With Quote  
Join Date: Apr 2004
Posts: 3,809
Reputation: Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light 
Rep Power: 17
Solved Threads: 147
Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: the type def statement and enum

  #4  
Sep 7th, 2005
Originally Posted by m7r23
OK, I will start with one. I will start with how do I use the type statement.
There is no "type" statement, and in the context of a typedef this still doesn't make the most sense to me.

Originally Posted by m7r23
In general I need to input two sets (of values) of user-defined data types:
One type named Sign declared by "typedef" to contain only
either +10 or -10.
Could you post the code?
Originally Posted by m7r23
I started my program. I have an attachement. I just dont know how to use it in the program.
Huh?
High Plains Blogger #plains #lounge ## I, for one, welcome our new socialist overlords.
"Capitalism is the unequal distribution of wealth. Socialism is the equal distribution of poverty."
Reply With Quote  
Join Date: Aug 2005
Posts: 14
Reputation: m7r23 is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
m7r23 m7r23 is offline Offline
Newbie Poster

Re: the type def statement and enum

  #5  
Sep 7th, 2005
Ok, sorry I was typing a little too fast. Yes I'm talking about the "typedef" statement.

I am trying to get a program that input two sets (of values) of user-defined data types:
One type named Sign declared by "typedef" to contain only
either +10 or -10.

I am clueless how to use the "typedef' statement because my c++ book briefly talk about it.

int main()
{
	typedef int Sign; //typedef defined

	Sign positive=10;// problem
	Sign negative=-10;// problem

	int num; 	  
	int sum = 0;  	
	int counter = 0; 	
	int max;
	int max2;

	cout<<"**Enter values -10 or 10. Enter -999 to terminate**"<<endl;
	
	
	cin>>num;
	while(num != SENTINEL)	
	     {
                  sum = sum + num; 
		counter++;		     							
	if (num > max) // largest and second largest
		{
			max2 = max;
			max = num;
		
		}
	
	else if (num >= max2 && num != max) //equal numbers
	     {
		 max2 = num;
	     }
		cin>>num;
	}

Sample Output of Program( The way the program is supposed to act)
Enter values -10 or 10, terminated with -999:
-10, -10, 10, -999
sum is -10,
average is -3.333
Largest is 10
Second largest is -10
Reply With Quote  
Join Date: Apr 2004
Posts: 3,809
Reputation: Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light 
Rep Power: 17
Solved Threads: 147
Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: the type def statement and enum

  #6  
Sep 7th, 2005
Hmmm. You want to use something like this?
typedef enum { positive = 10, negative = -10 } Sign;
[edit]As in...
#include <iostream>

typedef enum { positive = 10, negative = -10 } Sign;

int main()
{
   int num;
   do {
      std::cout << "Enter values -10 or 10, otherwise I quit: ";
   } while ( std::cin >> num && (num == positive || num == negative) );
   return 0;
}

/* my output
Enter values -10 or 10, otherwise I quit: 10
Enter values -10 or 10, otherwise I quit: -10
Enter values -10 or 10, otherwise I quit: 10
Enter values -10 or 10, otherwise I quit: 10
Enter values -10 or 10, otherwise I quit: 10
Enter values -10 or 10, otherwise I quit: -10
Enter values -10 or 10, otherwise I quit: 0
*/
???
High Plains Blogger #plains #lounge ## I, for one, welcome our new socialist overlords.
"Capitalism is the unequal distribution of wealth. Socialism is the equal distribution of poverty."
Reply With Quote  
Join Date: Aug 2005
Posts: 14
Reputation: m7r23 is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
m7r23 m7r23 is offline Offline
Newbie Poster

Re: the type def statement and enum

  #7  
Sep 7th, 2005
ok, let me look at it.
Reply With Quote  
Join Date: Aug 2005
Posts: 14
Reputation: m7r23 is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
m7r23 m7r23 is offline Offline
Newbie Poster

Re: the type def statement and enum

  #8  
Sep 7th, 2005
Ok, I think that might be it.
Let me ask two question.
Do the "typedef" statement have to be declared outside the main function.
Also is there anyway for this to work without using "enum" in typedef enum { positive = 10, negative = -10 } Sign;
Also, to terminate the program I am supposed to use a SENTINEL. I set it to -999 in my old program.
Reply With Quote  
Join Date: Apr 2004
Posts: 3,809
Reputation: Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light 
Rep Power: 17
Solved Threads: 147
Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: the type def statement and enum

  #9  
Sep 7th, 2005
Originally Posted by m7r23
Do the "typedef" statement have to be declared outside the main function.
No.
Originally Posted by m7r23
Also is there anyway for this to work without using "enum" in typedef enum { positive = 10, negative = -10 } Sign;
How about just checking for 10 and -10?
Originally Posted by m7r23
Also, to terminate the program I am supposed to use a SENTINEL. I set it to -999 in my old program.
...and -999?
High Plains Blogger #plains #lounge ## I, for one, welcome our new socialist overlords.
"Capitalism is the unequal distribution of wealth. Socialism is the equal distribution of poverty."
Reply With Quote  
Join Date: Aug 2005
Posts: 14
Reputation: m7r23 is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
m7r23 m7r23 is offline Offline
Newbie Poster

Re: the type def statement and enum

  #10  
Sep 8th, 2005
Alright, I know how to declare and what the "typedef" statement now. But how do I use it. Am I on the right track.


void Alias();	

const int SENTINEL = -999;

typedef int Sign;
Sign negative = -10;
Sign positive = 10;


int main()
{
	int num; 	  
	int sum = 0;  	
	int counter = 0; 	
	int max;
	int max2;


		cout << "Enter values -10 or 10 or -999 to terminate.: ";
cin>>num;

	while(num != SENTINEL)					
	{
		sum = sum + num; 					
		counter++;    							


	if (num >= max) 
		{
			max2 = max;
			max = num;
		}
	
	else if (num >= max2 && num != max)
	{
		 max2 = num;
	}
	
	Alias();

	}

	cout<<"The sum of the "<<counter<<" users is "<<sum<<endl;			
	cout<<"The max number is "<<max<<endl;
	cout<<"The second largest number is "<<max2<<endl;
	cout<<"The average is  "<<average(sum,counter)<<endl;

return 0;
 }




User Function for the typedef
void Alias()
{

	int num;

	do 
	{
		cout << "Enter values -10 or 10. Or -999 to terminate.: ";
		cin>>num;

		if (num != positive && num != negative)
			cout<<"Invalid. ";
	} 
	while  ((num == positive || num == negative || num == SENTINEL) );
  
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.



Other Threads in the C++ Forum
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 7:26 am.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC