Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++
 
#include <iostream.h>
#include <conio.h>
void main()
 
{
 
clrscr();
int *p1;
int n,i;
cout<<"Enter size of array ";
cin>>n;
p1=new int[n];
for(i=0;i<n;i++)
cin>>*p1++;
cout<<"output values "<<endl;
for(i=0;i<n;i++)
cout<<*p1++;
 
getch();
}
 

my program not give the correct output where is the mistake in logic
Posted 18-Apr-13 5:42am
ALIWAZ407
Edited 18-Apr-13 5:46am
v2
Comments
joshrduncan2012 at 18-Apr-13 10:44am
   
You need to tell us what you expect the output to be and what you are getting. We cannot guess where it is based on what we have here right now.
ALIWAZ at 18-Apr-13 10:54am
   
i enter two values 1 and 2 in array but resut is 22 and 24
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Reset the p1 pointer back to begin of the array before you start your output loop!
  Permalink  
Comments
ALIWAZ at 18-Apr-13 10:58am
   
i can not please elobarate
nv3 at 18-Apr-13 11:16am
   
By looking at your last ten questions I'd say: Do yourself a favor and read a C++ book from cover to cover (or a good website might do as well). Many of the problems you are asking help about arise just because you never got the basics down.
Sergey Alexandrovich Kryukov at 18-Apr-13 11:02am
   
Good catch, a 5.
 
I'll add a couple things. The loop variable i is declared above the loops, which is always bad. A loop variable for "for" statement should always be declared inside the "for" statement. And it's bad to give variables so short names.
 
—SA
nv3 at 18-Apr-13 11:14am
   
Thanks, Sergey!
CPallini at 19-Apr-13 3:25am
   
I (politely :-) ) disagree on both points:
- Sometimes it is useful having a not-scoped for variable (e.g. when your loop contains a break).
- Often short variable names makes code more clean.
Sergey Alexandrovich Kryukov at 19-Apr-13 11:55am
   
Thank you, Carlo.
#1 why? when, for example? do your mean only the loop variable of something else? maybe, but I'm just interested to know the example;
#2 it depends on what do you call short; do you think "i" or "n" is good enough?
—SA
CPallini at 19-Apr-13 12:20pm
   
Aaaarg! it ate up almost all my long comment!
Sooner or later I am going to rewrite it.
Sergey Alexandrovich Kryukov at 19-Apr-13 12:24pm
   
It happens and is very frustrating... :-(
—SA
CPallini at 19-Apr-13 13:11pm
   
I wrote it in my blog page:
http://www.codeproject.com/Members/CPallini?msg=4545579#xx4545579xx
Sergey Alexandrovich Kryukov at 19-Apr-13 13:56pm
   
Thank you, Carlo.
—SA
CPallini at 19-Apr-13 3:19am
   
5.
nv3 at 19-Apr-13 3:46am
   
Thanks!
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

1. Problem with your output, you've changed p1 in your input, after your cin, p1 has pointed to the end of the array.
 
2. why still using
iostream.h
It's too old... Try
#include <iostream>
using namespace std;
 
3. remember to delete your pointer.
  Permalink  
v5
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

You need to save the pointer at teh start, and restore it when you are startign the second loop:
int *p1, *pStart;
int n,i;
cout<<"Enter size of array ";
cin>>n;
pStart=new int[n];
p1 = pStart;
for(i=0;i<n;i++)
   cin>>*p1++; 
cout<<"output values "<<endl;
p1 = pStart;
for(i=0;i<n;i++)
   cout<<*p1++;
delete [] (pStart);
Note that you should also get into the habit of deleting anything you allocate when you are finished with it!
  Permalink  
v2
Comments
nv3 at 18-Apr-13 11:14am
   
You make it way to easy for these guys. I had hoped he'd get that idea himself :-)
CPallini at 19-Apr-13 3:20am
   
5.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 245
1 Kamal Rocks 184
2 Sergey Alexandrovich Kryukov 175
3 BillWoodruff 173
4 PIEBALDconsult 160
0 OriginalGriff 5,655
1 DamithSL 4,506
2 Maciej Los 3,997
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,180


Advertise | Privacy | Mobile
Web04 | 2.8.141216.1 | Last Updated 19 Apr 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100