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.
m7r23
- 2 Contributors
- forum9 Replies
- 11 Views
- 13 Years Discussion Span
- comment Latest Post by m7r23
Dave Sinkula 2,398
What's your question?
[edit]
"An enumeration comprises a set of named integer constant values. Each distinct enumeration constitutes a different enumerated type."
m7r23
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.
Dave Sinkula 2,398
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.
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?
I started my program. I have an attachement. I just dont know how to use it in the program.
Huh?
m7r23
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
Dave Sinkula 2,398
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
*/
???
m7r23
ok, let me look at it.
m7r23
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.
Dave Sinkula 2,398
Do the "typedef" statement have to be declared outside the main function.
No.
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?
Also, to terminate the program I am supposed to use a SENTINEL. I set it to -999 in my old program.
...and -999?
m7r23
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) );