Hello,

I'm just having a heck of a time trying to figure our the answer(s) to the following questions:

1. Declare a struct type, Time, that represents an amount of time consisting of minutes and seconds.    
2. Write statements that assign the time 6 minutes and 54 seconds to a variable, someTime, of type Time, as declared in Exercise 5. 
3. Declare a struct type, Song, that represents a song entry in an MP-3 library. It should have fields for title, album, artist, playing time in minutes and seconds (use the type declared in Exercise 5), and music category. The music category is represented by an enumeration type called Category.   
4. Write statements to declare a variable called mySong of type Song, and assign it a set of values. For the playing time, use the variable someTime declared in Exercise 6. Make up values for the other fields. Assume that the enumeration type Category includes any song category that you wish to use.
5. Write a statement to output the playing time from mySong, as declared in Exercise 8, in the format mm:ss.
6. Write a declaration of a union type called Temporal that can hold a time represented as a string, as an integer, or as a value of type Time, as declared in Exercise 5.
7. Write the declaration of a variable called shift of type Temporal, as declared in Exercise 10, and a statement that assigns the value of someTime, as declared in Exercise 6, to shift.
8. Write statements to declare a variable called mySong of type Song, and assign it a set of values. For the playing time, use the variable someTime declared in Exercise 6. Make up values for the other fields. Assume that the enumeration type Category includes any song category that you wish to use.    
9. Write a statement to output the playing time from mySong, as declared in Exercise 8, in the format mm:ss.    
10. Write a declaration of a union type called Temporal that can hold a time represented as a string, as an integer, or as a value of type Time, as declared in Exercise 5.
11. Write the declaration of a variable called shift of type Temporal, as declared in Exercise 10, and a statement that assigns the value of someTime, as declared in Exercise 6, to shift.

Granted I'm not looking to get every answer, but just some help to try and get the ball rolling.

Thanks!

Recommended Answers

All 17 Replies

Why do you study? What can you possibly learn from people answering these questions, therefore, what is the point of this task?

My advice:

LOOK at Exercise 6 etc.. and LEARN!

I'm just having a heck of a time trying to figure our the answer(s) to the following questions

Those aren't questions -- they are problems for you to do. Start by doing the first problem, then when that is finished do the second, etc. When you get one that you can't complete then post the code you have tried here at DaniWeb and ask some questions or explain what it is that you have a difficult time figuring out. That's the only way we can or will help you. No one is going to just hand you the solutions to those problems.

Looks like you have already done some sort of exercises. Perhaps all you need to do first is to review those exercises you underwent. E. g. what is a struct and how to declare a struct? How to declare variables and assign value to them? etc. etc.

Do them first and I bet it will help you "get the ball rolling".

Also, all these answers can be easily found in any course book, so that's another place where you can look. And also, you can always check the online documentation of the C++ language, which is pretty thick, and has a lot of explanations and example. Here's the link: Click Here. Browse around till you'll find the required answers. Try to "explore" the world a bit. :D

Alright, I'm sorry about how I approached this when I started this topic. That was unprofessional of me. I've finally gotten some time to go through and read/attempt this questions.

1.  Declare a struct type, Time, that represents an amount of time consisting of minutes and seconds.
    a.  struct Time
    {
        unsigned char minutes;
        unsigned char seconds;
    }

2.  Write statements that assign the time 6 minutes and 54 seconds to a variable, someTime, of type Time, as declared in Exercise 5.
    a.  struct Time
    {
        unsigned char minutes = 6;
        unsigned char seconds = 54;
    } someTime

That is my attempt at the first two questions.

How did I do?

The second problem is incorrect. You can't assign values to the variables inside the structure. Instead, inside main() declare an instance of the structure then assign the values to that instance.

The second problem is incorrect. You can't assign values to the variables inside the structure. Instead, inside main() declare an instance of the structure then assign the values to that instance.

This is my next attempt at #2. I'm putting two different ways that I am thinking about.

int main()
{
    struct Time
    {
        unsigned char minute;
        unsigned char seconds;
    };

    char minute = 6;
    char seconds = 54;

    Time someTime = minute;
    Time someTime = seconds;
}

or

int main()
{
    struct Time
    {
        unsigned char minute;
        unsigned char seconds;
    };

    Time someTime = 6;
    Time someTime = 54;
}

Is either of these correct?

No.

int main()
{
    struct Time
    {
        unsigned char minute;
        unsigned char seconds;
    };

    Time someTime;
    someTime.minute = 6;
    someTime.seconds = 54;
}

No

Now that I see the way you did it, I feel dumb. -_- I guess I'm just approaching this from the direction of "This is new, so it must be completely different."

Thank you for the help.

Here's the next three, let's see how I did.

3. Declare a struct type, Song, that represents a song entry in an MP-3 library. It should have fields for title, album, artist, playing time in minutes and seconds (use the type declared in Exercise 5), and music category. The music category is represented by an enumeration type called Category.

enum Category
{ROCK, POP, COUNTRY
};

struct Song
{
    string Title;
    string Album:
    string Artist;
    Time someTime;
    string Category;
};

4. Write statements to declare a variable called mySong of type Song, and assign it a set of values. For the playing time, use the variable someTime declared in Exercise 6. Make up values for the other fields. Assume that the enumeration type Category includes any song category that you wish to use.

Int main()
{
    struct Time
    {
        unsigned char minute;
        unsigned char seconds;
    };

    Time someTime;
    someTime.minute = 6;
    someTime.seconds = 54;

    enum Category
    { ROCK, POP, COUNTRY
    }

    struct Song
    {
        string Title;
        string Album:
        string Artist;
        Time someTime;
        string Category;
    };

    Song mySong;
    mySong.Title = Test;
    mySong.Album = Testing;
    mySong.Artist = Tester;
    mySong.someTime.minute;
    mySong.someTime.seconds
    mySong.Category = ROCK;
}

5. Write a statement to output the playing time from mySong, as declared in Exercise 8, in the format mm:ss.

cout << entry.someTime.minute << ":" << entry.someTime.seconds << endl;

Item 4. You're close. Move the structure definitions up above main() and outside any function so that they can be use in other functions when you write them. Structurde defined inside a function are not visible to other functions. Putting them at the top of the program, after includes, makes them global to the entire *.cpp or *.c file.

you can remove lines 26-28 because they were intended for the first exercise and not relevent to the 4th.

line 44: You have to put string literals in quotes, e.g. mySong.Title = "Test";

lines 47-48: you need to assign a value to those structure members like you did the other structure members.

So... more like this?

mySong.Title = "Test";
mySong.Album = "Testing";
mySong.Artist = "Tester";
mySong.someTime.minute = 6;
mySong.someTime.seconds = 54;
mySong.Category = "ROCK";

Int main()
{
    enum Category
    { ROCK, POP, COUNTRY
    };

    struct Time
    {
        unsigned char minute;
        unsigned char seconds;
    };

    struct Song
    {
        string Title;
        string Album:
        string Artist;
        Time someTime;
        string Category;
    };
    Song mySong;
}

Or did I move the wrong part?

No, like this.

    enum Category
    { 
        ROCK, POP, COUNTRY
    };

    struct Time
    {
        unsigned char minute;
        unsigned char seconds;
    };


    struct Song
    {
        string Title;
        string Album:
        string Artist;
        Time someTime;
        Category cat; // <<< this should not be a string
    };

int main()
{
    Song mySong;
    mySong.Title = "Test";
    mySong.Album = "Testing";
    mySong.Artist = "Tester";
    mySong.someTime.minute = 6;
    mySong.someTime.seconds = 54;
    mySong.cat = ROCK; // <<< No quotes on this one because it isn't a string
}

That was the other option I was thinking of, but wasn't sure which it would be.

Here are the last two. Let's see how I did.

6.  Write a declaration of a union type called Temporal that can hold a time represented as a string, as an integer, or as a value of type Time, as declared in Exercise 2.

union Temporal
{
    string testTime;
    int testTime;
    Time testTime;
}

and

7.  Write the declaration of a variable called shift of type Temporal, as declared in Exercise 10, and a statement that assigns the value of someTime, as declared in Exercise 3, to shift.

union Temporal
{
    string testTime;
    int testTime;
    Time testTime;
};

main()
{
    Temporal shift;
    shift = someTime;
}

Or is it this...

struct Time
{
    unsigned char minute;
    unsigned char seconds;
};


union Temporal
{
    string testTime;
    int testTime;
    Time testTime;
};

main()
{

    Time someTime;
    someTime.minute = 6;
    someTime.seconds = 54;

    Temporal shift;
    shift = someTime;
}

Or am I competely screwing it up?

Or am I competely screwing it up?

Put it in a compiler and try to compile it. The compiler will tell you if you are correct or not.

Here's what I came up with after running it through the compiler. Still having some issues though.

Not sure if I did it right, but the issues I'm having are with lines 21 and 22.

struct Time
{
    unsigned char minute;
    unsigned char seconds;
};

union Temporal
{
    char testTime;
    int i;
    Time tTime;
};

int main()
{
    Time someTime;
    someTime.minute = 6;
    someTime.seconds = 54;

    Temporal shift;
    shift.testTime = someTime;
    shift.i = someTime;
    shift.tTime = someTime;
}

you can only assign someTime to a variable that is of the same type as someTime. Look at the structure and figure out which member can be assigned someTime.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.