0

correct me a c++ progrm!

#include<iostream> #include<conio.h> #include<stdio.h> #include<stdlib.h> using namespace std; int func(int p,int i,int n,int val) { int a[10]={55,41,12,78,15,69,45,36,105}; cout<<"value of array before insertion\n"; for(i=0;i<=9;i++) { cout<<a[i]<<" "; cout<<endl; } while(1) { cout<<"\n enter position to insert new value ="; cin>>p; n=10; if(p>n||p<0) { cout<<"invalid position\r"; getch(); } else break; } while(n>=p-1) { a[n+1]=a[n]; n--; } cout<<"enter new value to insert="; cin>>val; a[p-1]=val; cout<<"values of array after insertion\n"; for(i=0;i<=10;i++) cout<<a[i]<<" "; getch(); } int main() { int p,n,i; int a[10]={55,41,12,78,15,69,45,47,36,105}; cout<<"value of array before delted \n"; for(i=0;i<=9;i++) cout<<a[i]<<endl; cout<<"enter position to delete value="; cin>>p; if(p>n||p<0) { cout<<"invalid position\r"; getch(); } else while(p<=n) { a(p-1)=a[p]; p++; } int a[n]=NULL; cout<<"\n value of array after deletion\n"; for(i=0;i<=9;i++) cout<<a[i]<<" "; cout<<endl; getch(); } main() { int val,c; cout<<"enter new value to insert="; cin>>val; c=func(p,i,n,val); cout<<func(p,i,n,val); } whats wrong with this code !its two different code which i'm merge into one code plz coorect me ! its cannot work

21st Dec 2016, 3:18 PM
Abrar Hussain
Abrar Hussain - avatar
5 Réponses
+ 6
It's kind of hard to see what you're trying to do (and I'm not really cut out for debugging on a smartphone) but from what I can see: - The two codes you are trying to merge together, one of them is to delete elements inside arrays, the other is to add elements to arrays. - The way you placed main() in int main() is invalid. For your code to at least compile, you will have to move everything inside main() to int main().
24th Dec 2016, 1:59 AM
Hatsy Rei
Hatsy Rei - avatar
+ 6
Let user decide on which function to run. E.g. int user_choice; cout << "Please input user choice."; cout << " 1. Add array" << endl; cout << " 2. Delete array" << endl; cin >> user_choice; switch (user_choice) { case 1: // codes needed to add array break; case 2: // codes needed to delete array break; default: // input validation } cout << "Press any key to continue"; getch();
24th Dec 2016, 7:05 AM
Hatsy Rei
Hatsy Rei - avatar
+ 1
thanks buddy I make my day
24th Dec 2016, 7:23 AM
Abrar Hussain
Abrar Hussain - avatar
0
ok got it! but where I'm used getch means how I can manage one code over another code in only int main()! I'm confused
24th Dec 2016, 6:19 AM
Abrar Hussain
Abrar Hussain - avatar
0
how it's work?
24th Dec 2016, 6:20 AM
Abrar Hussain
Abrar Hussain - avatar