what wrong with this problem?? m begineer

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

using namespace std;

class counter{
private :
int *myarray;
public:
void count(int *myarray, int n);
int readdisplay(int *myarray, int n);
};

void counter::count(int myarray[], int n)
{
for( int i = 0; i < n; i++ )
cin >> myarray[i];
}

int counter::readdisplay(int myarray[], int n)
{
for ( int i = 0; i < n; i++ )
cout << myarray[i]+1;
}

int main()
{
counter c;
const int N = 10;
int *myarray;

myarray = new int[N];

c.count( myarray, N );
c.readdisplay( myarray, N );

getch();

return ( 0 );
}
Once I ripped out the getch and the conio.h is was fine. What doesn't it do that you think it should do?
how to passs array to class n main file? :(
Pass a pointer to the first element. Which is what you're already doing.
can u describe what i have done with this prgram step by step i have done it by chance not with accurate logic
Did you really write this?
#include<iostream>

using namespace std;

class counter{
private :
int *myarray;
public:
void count(int *myarray, int n);
void readdisplay(int *myarray, int n);
};

void counter::count(int myarray[], int n)
{
for( int i = 0; i < n; i++ )
cin >> myarray[i];
}

void counter::readdisplay(int myarray[], int n)
{
for ( int i = 0; i < n; i++ )
cout << myarray[i];
}

void main()
{
counter c;
const int N = 10;
int *myarray;
myarray = new int[N];
c.count( myarray, N );
c.readdisplay( myarray, N );

system ("pause");
}
When I run the program....
1 2 3 4 5 6 7 8 9 10
12345678910


Is there something still wrong? Seems to be working to me.
yeah its working but howwww???
At first I thought it was stuck on something but it was just waiting for input. How were you inputting to the program before?

I ran it again and only put in
1 2 3 4 5

and it waited, like a good little program. For me to finish

6 7 8 9 10

This is because inside of the loop in your count function it will loop through 10 "cin" commands. Since cin only reads up until a blank space the stream will read all 10 consecutive inputs. If you did:

1 2 3 4 5 6 7 8 9 10 11

It would still only print out 1 - 10.

On a side note: If you wanted to read the entire line and not just up until a blank character you'd use this command instead:

getline(cin, input);

That'll read until the end of the line in the stream and parse it into input.
Topic archived. No new replies allowed.