Hi.
BEFORE YOU THINK I AM JUST GETTING SOMEONE TO DO MY WORK, SCROLL DOWN AS I HAVE MADE A VERY GOOD ATTEMPT AND AM RECEIVING ERRORS!

Basically i am fairly new to C++ and have been given a small class exercise to do for home work and am struggling to make it happen.

Basically i need to make a program which asks the user to enter messages, along with the time they inputted it ( hours, minutes, seconds) and then displays them at the end of the program.

We have to use structures and dynamic arrays in the exercise, so the program should run as:

* Prompt user for how many messages they want to input
// store that as the array

*  Enter that amount (initally inputted) of messages as follows:

     message: sdfsdfsdfsdfs

// for the time the user is to enter it manually

x hours
x minutes
x second

Once all messages and time have been displayed they are to be displayed, an example is as follows:

Enter amount of messages: 3

Messages received:
i love C++
2 hours
35 minutes
10 seconds

This subject is hard
4 hours
5 minutes
14 seconds

Please help me
6 hours
33 minutes
54 seconds

NOW HE IS MY ATTEMPT AT THE PROGRAM
As mentioned i am very new to this and may have many errors therefore any advice would be greatly appreciated

#include <iostream>
using namespace std;

struct messages
{
    string message;
    int hours;
    int minutes;
    int seconds;

};

int main()
{
    int size;
    int value;
    int* array;
    cout << "How many numbers?";
    cin >> size;

    array = new messages[size];

    for (int i = 0; i < size; i++)
    {
        cout << "Input " << i+1 << ": " << endl;
        cin >> value;

        array[i].messages = value;
}


    cout << "Message Recieved  were: ";

    for (int j = (size-1); j >= 0; j--)
    {
        cout << array[j].messages << " ";
    }

    return 0;
}

If you would like to assist me you can simply reply to this thread or better yet contact me at (snip) either by email or msn

Many Thanks

Recommended Answers

All 4 Replies

array = new messages[size];

array is of type int and you are trying to create an array of type messages. Change the datatpye of array to messages.

array[i].messages = value;

I cant say what you are trying to do here, messages is the name of the structure. If you mistyped it and meant message instead then also you are trying to store an int to a string.

You will have to take the inputs of each elements of the structure seperately like

for (int i = 0; i < size; i++)
{
cout << "Input " << i+1 << ": " << endl;
cin>>array[i].message>>array[i].hours>>array[i].minutes>>array[i].seconds;
}

And display them in similar manner.

I dont quite understand yet, i understand it once i read it, but struggle to write it from scratch.

If you happen to reply to this again would u be able to re-post my code and make any alts to it in red so i can see what you have done, then i will be able to understand it.
I tried to make some changes but came back with heaps of errors.

Thankyou very much for your help hammerhead, do u understand what the program is tryign to do? As you questioned some of my code, and i don't even know if it was even meant to be there i was merely brain storming.

In lieu of int * array use message * array or vector<message> array;

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

// identifier messages causes a name clash with the facet
// because of the using directive (using namespace std)
struct message_t
{
    string message;
    int hours;
    int minutes;
    int seconds;
};

int main()
{
  int size;
  message_t* array;
  cout << "How many numbers?";
  cin >> size;
  array = new message_t;

  for (int i = 0; i < size; i++)
  {
    // ...
        string message ;
    int hours, minutes, seconds ;
    // no whitespaces allowed in the message string
    cin >> message >> hours >> minutes >> seconds ;
    array[i].message = message ;
    array[i].hours = hours ;
    // etc
  }
// ...
}
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.