-3

I have to make a program in C++ that will read numbers and then arrange them in ascending order. The numbers can be infinite, so the program should read numbers until any particular value is entered to terminate the reading process. I have written below code but is not working and showing undesired output. I will be so thankful if someone will help me.

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>

void main()
{
    int *p,*q,i=1,j,k,temp;

    p=(int*)malloc(sizeof(int));
    cin>>*p;

    while((*p)!=-1) //read until -1 is entered
    {
        i++;
        p=(int*)realloc(p,sizeof(int)*i);
        q=p;
        p=p+(i-1);  //going to next address to read
        cin>>*p;
    }

    p=q;

    for(j=1;j<i;++j)
    {
        for(k=0;k<i-j-1;++k)
        {
            if((*(p+k))>(*(p+k+1)))
            {
                temp=*(p+k);
                *(p+k)=*(p+k+1);
                *(p+k+1)=temp;
            }
        }
    }

    cout<<"\n";

    for(j=0;j<i-1;++j)
    {
        cout<<*(p+j)<<" ";
    }
}
Aleph0
  • 470
  • 2
  • 10
  • 27
Neeraj Mishra
  • 19
  • 1
  • 3
  • 3
    Could you please add the behavior you expect along with the behavior you're actually getting. – Tom Mar 06 '15 at 15:25
  • How would you go about testing an infinite list of numbers? – Scott Hunter Mar 06 '15 at 15:25
  • 2
    You should read up on `std::vector` and `std::sort`. – fredoverflow Mar 06 '15 at 15:26
  • He said, that there is a terminator - which from the code is -1 – Kai Mattern Mar 06 '15 at 15:27
  • You should not use [void main()](http://isocpp.org/wiki/faq/newbie#main-returns-int) – NathanOliver Mar 06 '15 at 15:28
  • 7
    You're writing an ancient dialect of C++, littered with weird and error-prone idioms from C, so it's no surprise that it's difficult to make it work. You might be better off with a [good book](http://stackoverflow.com/questions/388242) about modern C++. – Mike Seymour Mar 06 '15 at 15:28
  • It looks like you've written the world's ugliest bubble sort routine. Learning to make your code self documenting and readable really helps you track down problems in your own work. Trust me on this. – Jeff Watkins Mar 06 '15 at 15:29
  • 2
    @MikeSeymour: This comment is appropriate for surprisingly many questions here. You could in fact copy & paste it 100% with zero changes several times a day... :) – Christian Hackl Mar 06 '15 at 15:45
  • @ChristianHackl: funny, I was thinking the same about this post's title. – Jongware Mar 06 '15 at 16:29

2 Answers2

2

Expanding on my comment, here is what an actual C++ solution might look like:

#include <algorithm>
#include <iostream>
#include <vector>

int main()
{
    std::vector<int> numbers;
    int number = -1;
    std::cin >> number;
    while (number != -1)
    {
        numbers.push_back(number);
        number = -1;
        std::cin >> number;
    }
    std::sort(numbers.begin(), numbers.end());
    for (int x : numbers)
    {
        std::cout << x << ' ';
    }
    std::cout << '\n';
}
fredoverflow
  • 256,549
  • 94
  • 388
  • 662
2

After

p=p+(i-1);

p is no longer a pointer that is valid to realloc.

Replace

p=p+(i-1);
cin>>*p;

with

cin >> p[i-1];

and get rid of q.
(You can use cin >> *(p + i - 1); if you insist on obfuscation.)

Your sorting routine also becomes much more readable if you replace the pointer arithmetic with indexing.

molbdnilo
  • 64,751
  • 3
  • 43
  • 82
  • I have used indexing and that solved all the problems. Earlier it was really hard to debugg and understand the code. Now program is working fine, I was stuck in this from last two days. Thanks a lot for your awesome guide. :) :) – Neeraj Mishra Mar 06 '15 at 16:22