c++ - This is a Data Structures Project with array based list functions and I can't seem to get rid of this infinite loop. -
i getting infinite loop project , can't seem around after working 7 hours. please provide help? project create menu based program array based list functions. have been researching cplusplus forums , stackoverflow can't seem figure out. eyesburning
#include <iostream> #include <stdlib.h> #include<stdio.h> #include<windows.h> #define max 15 using namespace std; main() { int array[15]; int n; printf("enter number of elements :"); scanf("%d",&n); for(int i=0;i<n;i++) { printf("enter array elements 1 one : "); scanf("%d",&array[i]); // seem getting infinite loop here } while(n > 1) { printf("\n1. add"); printf("\n2. insert"); printf("\n3. delete"); printf("\n4. show"); printf("\n5. count"); printf("\n6. clear"); printf("\n7. exit"); printf("\nenter choice : "); int choice; scanf("%d",&choice); switch(choice) { case 1: { printf("enter new element add @ end: "); int new_el; scanf("%d",&new_el); array[n]=new_el; n++; break; } case 2: { printf("enter position want insert : "); int pos; int new_el; scanf("%d",&pos); printf("enter new element insert in position : "); scanf("%d",&new_el); pos--; for(int i=n-1;i>=pos;i--) array[i+1]=array[i]; array[pos]=new_el; n++; break; } case 3: { printf("enter value deleted : "); int key,f=0; int pos; scanf("%d",&key); for(pos=0;pos<n;pos++) { if(array[pos]==key) { f=1; break; } } if(pos==n || f==0) { printf("element not found"); break; } if(f==1) { for(int i=pos;i<n;i++) array[i]=array[i+1]; printf("element deleted successfully"); n--; break; } } case 4: { printf("\n array elements are"); for(int i=0; i<n; i++) printf("\n %d",array[i]); break; } case 5: { printf("count is:%d ",n); break; } case 6: { printf("array cleared & initialized"); for(int i=0;i<n;i++) array[i]=0; break; } case 7: { printf("quit"); return(0); break; } } } }
wil, use code programmed in linux, removed windows.h, add @ end. problem while loop.
#include <iostream> #include <stdlib.h> #include<stdio.h> #define max 15 using namespace std; main() { int array[15]; int n; printf("enter number of elements :"); scanf("%d",&n); for(int i=0;i<n;i++) { printf("enter array elements 1 one : "); scanf("%d",&array[i]); // seem getting infinite loop here }
while(n < max)
{ printf("\n1. add"); printf("\n2. insert"); printf("\n3. delete"); printf("\n4. show"); printf("\n5. count"); printf("\n6. clear"); printf("\n7. exit"); printf("\nenter choice : "); int choice; scanf("%d",&choice); switch(choice) { case 1: { printf("enter new element add @ end: "); int new_el; scanf("%d",&new_el); array[n]=new_el; n++; break; } case 2: { printf("enter position want insert : "); int pos; int new_el; scanf("%d",&pos); printf("enter new element insert in position : "); scanf("%d",&new_el); pos--; for(int i=n-1;i>=pos;i--) array[i+1]=array[i]; array[pos]=new_el; n++; break; } case 3: { printf("enter value deleted : "); int key,f=0; int pos; scanf("%d",&key); for(pos=0;pos<n;pos++) { if(array[pos]==key) { f=1; break; } } if(pos==n || f==0) { printf("element not found"); break; } if(f==1) { for(int i=pos;i<n;i++) array[i]=array[i+1]; printf("element deleted successfully"); n--; break; } } case 4: { printf("\n array elements are"); for(int i=0; i<n; i++) printf("\n %d",array[i]); break; } case 5: { printf("count is:%d ",n); break; } case 6: { printf("array cleared & initialized"); for(int i=0;i<n;i++) array[i]=0; break; } case 7: { printf("quit"); return(0); break; } } } }
let me know in case of clarification!!
Comments
Post a Comment