Answered # the type def statement and enum

Dave Sinkula 2,398 Discussion Starter m7r23 Dave Sinkula 2,398 Discussion Starter m7r23 Dave Sinkula 2,398 Discussion Starter m7r23 Dave Sinkula 2,398 Discussion Starter m7r23 Write a C program that should create a 10 element array of random integers (0 to 9). The program should total all of the numbers in the odd positions of the array and compare them with the total of the numbers in the even positions of the array and indicate ...

0

What's your question?

[edit]

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

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.

0

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?

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.

```
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

0

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
*/
```

???

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.

0

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?

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.

```
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) );
```

This question has already been answered. Start a new discussion instead.

Recommended Articles

Hi. so this is actually a continuation from another question of mineHere but i was advised to start a new thread as the original question was already answered.

This is the result of previous question answered :

code for the listbox - datagridview interaction

At the top of the code ...

the function that I created to find the ...