473,506 Members | 17,309 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

"typedef" and "enum" problem

Fao
Hi, I am in my first year of C++ in college and my professor wants me
to Write a Program with multiple functions,to input two sets of
user-defined data types:

One type named 'Sign' declared by "typedef" to contain only
either +10 or -10 and

the other type named Color declared by "enum" to contain only
black, blue, purple, red, white, and yellow.

For each set of inputs, output their sum, average,
maximum and second largest.

I must also use typedef and enum for two data types.
I do not have a problem with finding the sum, max, avg, second largest,
my problem is how to properly use "typedef" and "enum".

Here is what I have, which is completely wrong(laugh if you want):

#include <iostream>
using namespace std;

enum Color {black, blue, purple, red, white, yellow};
void PrintEnum();

int main();
{
cout << "Enter the First two letters of your favorite color: " << endl;

cin >> ch1 >> ch2;

switch (ch1)
{
case 'a': if (ch2 == '1')
Color = black;
else
Color = red;
break;

case 'b': Color = blue;
break;

case 'c': if (ch2 == 'w')
Color = white;
else
Color = yellow;
break;

case 'd': Color = purple
break;
default: cout << "Illegal input." << endl;

return Colors;
}

void PrintEnum(Favorite Colors)
{
switch (Colors)
{
case black: cout << "black";
break;
case blue: cout << "blue";
break;
case purple: cout << "purple";
break;
case red: cout << "red";
break;
case white: cout << "white";
break;
case yellow: cout << "yellow";
break;
}
}

If anyone, and I mean anyone out there can help me, it would be greatly
appreciated.

Jul 23 '05 #1
6 3717

Fao wrote:
Hi, I am in my first year of C++ in college and my professor wants me
to Write a Program with multiple functions,to input two sets of
user-defined data types:

One type named 'Sign' declared by "typedef" to contain only
either +10 or -10 and

the other type named Color declared by "enum" to contain only
black, blue, purple, red, white, and yellow.

For each set of inputs, output their sum, average,
maximum and second largest.

I must also use typedef and enum for two data types.
I do not have a problem with finding the sum, max, avg, second largest, my problem is how to properly use "typedef" and "enum".

Here is what I have, which is completely wrong(laugh if you want):

#include <iostream>
using namespace std;

enum Color {black, blue, purple, red, white, yellow};
void PrintEnum();

int main();
{
cout << "Enter the First two letters of your favorite color: " << endl;
cin >> ch1 >> ch2;
What are ch1 and ch2 you never declared them
switch (ch1)
{
case 'a': if (ch2 == '1')
Color = black;
else
Color = red;
break;

case 'b': Color = blue;
break;

case 'c': if (ch2 == 'w')
Color = white;
else
Color = yellow;
break;

case 'd': Color = purple
break;
default: cout << "Illegal input." << endl;

return Colors;
again what is Colors you did not declare this. }
how do these values map to the colors. You aked for the first two
letters of your favorite color and as far as I can see that is not what
you are looking for.
void PrintEnum(Favorite Colors) What is type Favorite {
switch (Colors)
{
case black: cout << "black";
break;
case blue: cout << "blue";
break;
case purple: cout << "purple";
break;
case red: cout << "red";
break;
case white: cout << "white";
break;
case yellow: cout << "yellow";
break;
}
}

If anyone, and I mean anyone out there can help me, it would be greatly appreciated.


I am not going to do the assignment for you and I doubt anyone else
here is either. I think that you would find, judging by what you have
above, that if someone here did the assignment your teacher would
surely know that you did not do it.

My first suggestion would be read your textbook from the beginning and
try again. Any, even half ass, book on C++ will cover these topics very
early in the book. You should also go and talk to your teacher and TA
if you have one. Go to a study group. Look at sample programs in the
book and try to see what they are doing.

You are missing some very basic things here and I don't have time to
write you a tutorial here. There are a lot of tutorials on the web, try
http://www.cplusplus.com/doc/tutorial/ or
http://www.intap.net/~drw/cpp/. I just did a quick google search for
C++ tutorials and got over 4 million hits.

Once you take a look at the materials and make a real effort to get
together something that reasonably resembles a C++ program I would be
glad to give you some pointers.

Jul 23 '05 #2
Fao wrote:
Hi, I am in my first year of C++ in college and my professor wants me
to Write a Program with multiple functions,to input two sets of
user-defined data types:

One type named 'Sign' declared by "typedef" to contain only
either +10 or -10 and
That doesn't make sense, you can't define, using a typedef, a type that can
contain 2 values, +10 or -10. What exactly are you required to do?

the other type named Color declared by "enum" to contain only
black, blue, purple, red, white, and yellow.

For each set of inputs, output their sum, average,
maximum and second largest.
What exactly are the inputs? that doesn't make sense with respect to either
of the input types you have described.

I must also use typedef and enum for two data types.
I do not have a problem with finding the sum, max, avg, second largest,
my problem is how to properly use "typedef" and "enum".

Here is what I have, which is completely wrong(laugh if you want):

#include <iostream>
using namespace std;

enum Color {black, blue, purple, red, white, yellow};
That enum is fine.
void PrintEnum();
A function that takes no parameters and returns no value. What is it
supposed to do?

Did you want

std::ostream& operator<<(std::ostream& out, Color c);

instead?

int main();
{
cout << "Enter the First two letters of your favorite color: " << endl;

cin >> ch1 >> ch2;

switch (ch1)
{
case 'a': if (ch2 == '1')
Color = black;
That doesn't make sense. Why should the sequence "a1" correspond to black?
else
Color = red;
break;

case 'b': Color = blue;
break;

case 'c': if (ch2 == 'w')
Color = white;
And why should "cw" be white?
else
Color = yellow;
break;

case 'd': Color = purple
"d" -> purple???
break;
default: cout << "Illegal input." << endl;

return Colors;
The only 2 return values from main() defined by the C++ standard are
EXIT_SUCCESS and EXIT_FAILURE (I think - and I can't remember what header
they are defined in at the moment). As a special exception, if there is no
return statement, it is equivalent to "return EXIT_SUCCESS". Returning
'Colors' is meaningless.
}

void PrintEnum(Favorite Colors)
The signature must match the prototype you declared previously. It should
also mention the types that you want. eg, PrintEnum(Color Colors), meaning
take 1 argument, named 'Colors' of type 'Color'. The overload of
operator<< is a better solution though.
{
switch (Colors)
{
case black: cout << "black";
break;
case blue: cout << "blue";
break;
case purple: cout << "purple";
break;
case red: cout << "red";
break;
case white: cout << "white";
break;
case yellow: cout << "yellow";
break;
Ok, but a better approach would be

std::ostream& operator<<(std::ostream& out, Color c)
{
return out << ColorStrings[c];
}

where ColorStrings is

char const* ColorStrings[6] =
{"black","blue","purple","red","white","yellow" };

You could re-use this array to improve the input of the colors (ie. by
finding the index of the string that matches the first 2 characters of the
input).

}
}

If anyone, and I mean anyone out there can help me, it would be greatly
appreciated.


HTH,
Ian McCulloch

Jul 23 '05 #3
Fao
I thank you for you reply, and I really did not want anyone to do the
assignment for me, it's just that I am lost. This is only the third
week of school and my professor has me doing "typedef" and "enum"
functions in chapter 8 of our book, and I am seriously new to
programming (no prior experience what so ever). My book gives examples
but they have nothing to do with the type of program he wants the class
to complete. With that said, I will take your advice and check out the
tutorial sites you mentioned, considering I have been bugging my
teacher for the past two days about the assignment and he refuses to
help.
And just so you do not think I am a total moron, here is a program I
had to complete before this assignment (It might be simple compared to
the kind of stuff you do, but for someone who have never done this
stuff until this semester, I think I understand it better than most of
the people in my class.) Thanks again! :

#include <iostream>
#include <cmath>
using namespace std;

double number;
const int SENTINEL = -999;
void rootofNumber(double x);

int main()
{

double num, num2;
double sum = 0;
double max ;
int counter = 2;
int avg = 0;
double secMax;

cout << "Enter numbers : To stop program enter" << " " << SENTINEL <<
endl;

rootofNumber(number);

cin >> num2;

if (number > num2)
{
max = number;
secMax = num2;
}

else
{
max = num2;
secMax = number;
}
sum = sum + number + num2;
cin >> num;

while (num != SENTINEL)
{

sum = sum + num;

if (num > max)
{
secMax = max;
max = num;

}
if (max > num && num > secMax)
{
secMax = num;
}

counter++;

avg = sum / counter;

cin >> num;

}
cout << "Sum of numbers is: " << sum << endl;
cout << "The avg is: " << avg << endl;
cout << "The max number is: " << max << endl;
cout << "The Second max is: " << secMax << endl;
return 0;
}

void rootofNumber(double x)
{

cin >> number;

cout << "The Squareroot of "<< number << " is "<< sqrt(number)
<< endl;
return;
}

Jul 23 '05 #4
Fao
Thanks for your reply. The scenario you presented could solve my
problem, except I cannot use strings, arrays, or structs. If I could, I
would probably be done. When you stated:

"That doesn't make sense, you can't define, using a typedef, a type
that can
contain 2 values, +10 or -10. What exactly are you required to do?"

You make the same argument I have been making to my Professor for the
past 2 days!!!
But he says there is a way to do it, I just have to find it.

The instructions that I posted were the exact ones he gave to letter.
Thanks again for your reply, I will research everything you submitted.

Jul 23 '05 #5
Fao wrote:
But he says there is a way to do it, I just have to find it.

HINT: What other type has only two values? Has this type always been a
part of C++? If not, how would you get by without it?

Jul 23 '05 #6

Fao wrote:
I thank you for you reply, and I really did not want anyone to do the
assignment for me, it's just that I am lost. This is only the third
week of school and my professor has me doing "typedef" and "enum"
functions in chapter 8 of our book, and I am seriously new to
programming (no prior experience what so ever). My book gives examples but they have nothing to do with the type of program he wants the class to complete. With that said, I will take your advice and check out the tutorial sites you mentioned, considering I have been bugging my
teacher for the past two days about the assignment and he refuses to
help. Sounds like you might not have a very good teacher.
And just so you do not think I am a total moron, here is a program I
had to complete before this assignment (It might be simple compared to the kind of stuff you do, but for someone who have never done this
stuff until this semester, I think I understand it better than most of the people in my class.) Thanks again! :


I am not trying to imply you are a "moron" just that there are some
very basic elements, i.e. variable declarations, missing from your
code. This program is a whole different story from the one you posted
the first time. I would suggest you use the first program you did as a
starting point for the new one. Change one thing at a time and
recompile and run it so you can make sure that the one thing works. If
it won't compile you will have a very good idea of where the problem
is. When you get something together and still have some questions post
them again.

Jul 23 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1960
by: Patrick Guio | last post by:
Dear all, I wonder whether anyone might have a better idea/solution to the following. I need an associative container <int,string> for a limited and defined number of values (enum-like) but ir...
12
1830
by: Susan Baker | last post by:
Hi, I want to store data in a 3-tuple {a,b,c}. Element a is an enumeration, and element c is an enumeration which is specific (i.e. determined) by a. An example will help clarify further. I...
9
6192
by: Xiangliang Meng | last post by:
Hi, all. I see a very strange fragment code today. Uint32 enum { a = 100; b = 200; }; NOTE: Uint32 is defined to be 'unsigned' in other source files.
1
3522
by: Alfonso Morra | last post by:
Hi, I have the ff data types : typedef enum { VAL_LONG , VAL_DOUBLE , VAL_STRING , VAL_DATASET }ValueTypeEnum ;
0
1434
by: zheng | last post by:
I create a OCX program with ATL project, and One parameter in the interface function is ENUM type, and I has defined the ENUM type in the interface, but compiled falid with MIDL2001, in the...
7
4689
by: for.fun | last post by:
Hi everybody, I have the following problem : B class need A::MyEnum type and A class need B::MyEnum type. In both case, the class type is incomplete so it is obvious that the ::MyEnum can not...
3
2268
by: Chris Cahoon | last post by:
Hello, I am trying to include a header file which was written in C, but my project is in C++. Even when I wrap the include statement or the entire header files with extern "C" (used according to...
4
171291
by: msukumarbabu | last post by:
Hi all, What will be difference between "typedef enum" and "enum". or difference between “typedef structure" and "structure" I am going through some code. in that some place they are using...
9
11153
by: Rohit | last post by:
I am trying to initialize an array whose initializers depend on value of Enums. I take enum and then decide the initializer value, so that even if enum value changes because of addition to list...
0
7220
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7308
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7371
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7023
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7479
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5037
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3188
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3178
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
757
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.