We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,183 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

reversing the output c++

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

int main ()
{
  int i,n;
  int * p;
  cout << "How many numbers would you like to type? ";
  cin >> i;
  p= new (nothrow) int[i];
  if (p == 0)
    cout << "Error: memory could not be allocated";
  else
  {
    for (n=0; n<i; n++)
    {
      cout << "Enter number: ";
      cin >> p[n];
    }
    cout << "You have entered: ";
    for (n=0; n<i; n++)
      cout << p[n] << ", ";
    delete[] p;
  }
  return 0;

for example i want 3 numbers, say the numbers you enter are 1,2,3, it'l will come up ive entered 1,2,3 in that order. i want to know how you reverse that so that it reads what you've typed in,(dynamic memory etc.) then reverses that order. tried about with it but cant get it going? any takers?

2
Contributors
5
Replies
3 Hours
Discussion Span
6 Months Ago
Last Updated
6
Views
pjh-10
Junior Poster in Training
71 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
Infraction Points: 5

Either you insert the numbers in the array in reverse order and print the array normally

 for (n=i-1; n!=-1; n--)
    {
      cout << "Enter number: ";
      cin >> p[n];
    }

or normally insert items in the array, and print them in reverse order

for (n=i-1; n!=-1; n--)
      cout << p[n] << ", ";
Lucaci Andrew
Practically a Master Poster
649 posts since Jan 2012
Reputation Points: 91
Solved Threads: 91
Skill Endorsements: 12

did that second method there and its working si far except for one instance, when i do 1,2,3 it comes up 0,3,2,1?

pjh-10
Junior Poster in Training
71 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
Infraction Points: 5

Yeah, well initially was

 for (n=i; n!=-1; n--)

but I edited in

 for (n=i-1; n!=-1; n--)

Was a little bit quick on typewriting, forgot that -1 near the i. Try it now, it will definately work.

Lucaci Andrew
Practically a Master Poster
649 posts since Jan 2012
Reputation Points: 91
Solved Threads: 91
Skill Endorsements: 12

lifesaver lucaci, as you can probably tell, im a beginner,trying to learn all the time.lol
Thanks again

pjh-10
Junior Poster in Training
71 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
Infraction Points: 5

Glad I could help. Also, don't forget to mark this thread as solved, since the matter at hand is no longer the problem. If you do have further questions, for each question start a new thread explaining the situation.

Lucaci Andrew
Practically a Master Poster
649 posts since Jan 2012
Reputation Points: 91
Solved Threads: 91
Skill Endorsements: 12

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.0851 seconds using 2.67MB