Please support our C++ advertiser: Programming Forums
![]() |
What's your question?
[edit]
"An enumeration comprises a set of named integer constant values. Each distinct enumeration constitutes a different enumerated type."
typedef
[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."
"Capitalism is the unequal distribution of wealth. Socialism is the equal distribution of poverty."
•
•
Join Date: Aug 2005
Posts: 14
Reputation:
Rep Power: 4
Solved Threads: 0
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.
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.
•
•
•
•
Originally Posted by m7r23
OK, I will start with one. I will start with how do I use the type statement.
•
•
•
•
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.
•
•
•
•
Originally Posted by m7r23
I started my program. I have an attachement. I just dont know how to use it in the program.
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."
"Capitalism is the unequal distribution of wealth. Socialism is the equal distribution of poverty."
•
•
Join Date: Aug 2005
Posts: 14
Reputation:
Rep Power: 4
Solved Threads: 0
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.
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
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
Hmmm. You want to use something like this?
[edit]As in... ???
typedef enum { positive = 10, negative = -10 } Sign;#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."
"Capitalism is the unequal distribution of wealth. Socialism is the equal distribution of poverty."
•
•
Join Date: Aug 2005
Posts: 14
Reputation:
Rep Power: 4
Solved Threads: 0
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.
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.
•
•
•
•
Originally Posted by m7r23
Do the "typedef" statement have to be declared outside the main function.
•
•
•
•
Originally Posted by m7r23
Also is there anyway for this to work without using "enum" in typedef enum { positive = 10, negative = -10 } Sign;
•
•
•
•
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.
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."
"Capitalism is the unequal distribution of wealth. Socialism is the equal distribution of poverty."
•
•
Join Date: Aug 2005
Posts: 14
Reputation:
Rep Power: 4
Solved Threads: 0
Alright, I know how to declare and what the "typedef" statement now. But how do I use it. Am I on the right track.
User Function for the typedef
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) );
![]() |
Other Threads in the C++ Forum
- Previous Thread: Please Help Matrix Library Needed!!!
- Next Thread: C++ question
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)






Linear Mode