Suppose I have a dynamic array.(trees) The user enters the number of times for the dynamic array to loop based on int num.

He has filled the array, say num=3, so he has entered the age of 3 trees into trees.

Now he wants to display the age of all three trees, in a format a bit like this...

tree 1 is 15
tree 2 is 101
tree 3 is 3

How would this be done?

When outputting, I have created a For loop that loops based on num, and cout<<trees" /n";, yet all I get is would be a single cout statement of the last data that was input. How can I solve this?

Thanks

Recommended Answers

All 5 Replies

Paste your code please.

No offience, but I know everyone in my faculty uses this and will plagarise it, ahhh well I guess that's the price of help here... :(

#include <stdafx.h>
#include <iostream>

using namespace std;

int main()
{
  int num;
 
  cout << "Enter trees = ";
  cin >> num;


  float *trees = new float[treetype]


  for (int i = 0; i < num i++) 
  {
		 
    cout << "\nEnter tree " << (i+1) ;  
	cin >>tree[i];

for(i=0; i<num i++)
(
cout <<tree[i]<<i+1; <<endl;
}

return
}

Hurr, multiple errors.

That code shouldn't compile!

#include <stdafx.h>
#include <iostream>

using namespace std;

int main() {
    int num;

    cout << "Enter trees = ";
    cin >> num;


    float *trees = new float[num]


    for (int i = 0; i < num i++) {

        cout << "\nEnter tree " << (i+1) ;
        cin >>tree[i];
    }

    for (int i=0; i<num i++) {
        cout << tree[i] << i+1 << endl;
    }

    return 0;
}

OK maybe that code doesn't compile, a few missing brackets, whatever, but do you see what I'm trying to ask about how to output the array contents? Example code of this perhaps, please?

OK maybe that code doesn't compile, a few missing brackets, whatever,

Don't be so careless. Programming is a precise task, I fail it often enough, but you're just making a joke of it now.

The code I posted should work and contains your answer. Analyze it, compare it to your code and reply with the differences you notice if you want to learn.

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.