这是一个具有基于数组的列表函数的数据结构项目,我似乎无法摆脱这个无限循环。

This is a Data Structures Project with array based list functions and I can't seem to get rid of this infinite loop.

本文关键字:无限循环 项目 函数 一个 于数组 列表 数组 数据结构      更新时间:2023-10-16

这个项目得到了一个无限循环,工作了七个小时后我似乎无法绕过它。你能提供一些帮助吗?这是一个使用基于数组的列表功能创建基于菜单的程序的项目。我一直在研究Cplusplus论坛和stackoverflow,但似乎无法弄清楚这一点。眼睛灼热

#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 the number of elements :");
scanf("%d",&n);
for(int i=0;i<n;i++)
{   
    printf("Enter the Array elements one by one : ");
    scanf("%d",&array[i]); // seem to be getting an 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 your choice : ");
    int choice;
    scanf("%d",&choice);
    switch(choice)
    {
        case 1:
        {
        printf("Enter the new element to add at end: ");
        int new_el;
        scanf("%d",&new_el);
        array[n]=new_el;
        n++;
        break;  
        }   

        case 2:
        {
        printf("Enter the position you want to insert : ");
        int pos;
        int new_el;
        scanf("%d",&pos);
        printf("Enter the new element to insert in that 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 the value to be 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 is cleared & initialized");
        for(int i=0;i<n;i++)
        array[i]=0;
        break;
        }   
    case 7:
    {
        printf("Quit"); 
        return(0);
        break;
    }   
}
}
}

Wil,使用我在 Linux 中编写的这段代码,所以删除了 windows.h,在你最后添加回来。问题出在循环中。

#include <iostream>
#include <stdlib.h>
#include<stdio.h>
#define MAX 15
using namespace std;
main()
{
int array[15];
int n;
printf("Enter the number of elements :");
scanf("%d",&n);
for(int i=0;i<n;i++)
{   
    printf("Enter the Array elements one by one : ");
    scanf("%d",&array[i]); // seem to be getting an infinite loop here
}

而(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 your choice : ");
    int choice;
    scanf("%d",&choice);
    switch(choice)
    {
        case 1:
        {
        printf("Enter the new element to add at end: ");
        int new_el;
        scanf("%d",&new_el);
        array[n]=new_el;
        n++;
        break;  
        }   

        case 2:
        {
        printf("Enter the position you want to insert : ");
        int pos;
        int new_el;
        scanf("%d",&pos);
        printf("Enter the new element to insert in that 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 the value to be 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 is cleared & initialized");
        for(int i=0;i<n;i++)
        array[i]=0;
        break;
        }   
    case 7:
    {
        printf("Quit"); 
        return(0);
        break;
    }   
}
}
}

如果有任何澄清,请告诉我!!