这是一个具有基于数组的列表函数的数据结构项目,我似乎无法摆脱这个无限循环。
This is a Data Structures Project with array based list functions and I can't seem to get rid of this infinite loop.
这个项目得到了一个无限循环,工作了七个小时后我似乎无法绕过它。你能提供一些帮助吗?这是一个使用基于数组的列表功能创建基于菜单的程序的项目。我一直在研究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;
}
}
}
}
如果有任何澄清,请告诉我!!
相关文章:
- 过载'operator new'如何导致无限循环?
- C++正则表达式无限循环
- 程序在尝试猜测它选择的随机数时进入无限循环?
- 遍历链表时的无限循环
- 循环链表:无限循环
- 比较两个字符串后卡在无限循环中
- 在做一段时间内检查字符的无限循环
- 逐字读取文本文件中的每一行并转换为 int(无限循环或崩溃?
- 在这个无限循环中,当输入 0 时终止,当输入 int 范围之外的任何内容时,程序行为不正常
- 可能的无限循环
- 超出 int 的值范围后的无限循环
- 为什么这段代码会导致无限循环?
- 运行无限循环的最小二叉树问题
- 在分配和发布递增循环迭代器时C++无限循环(gcc 错误?
- 连接线程时的无限循环
- 为什么这种方法会进入无限循环?
- 由于QTextStream的无限循环
- Qt 在另一个线程中无限循环
- 这是一个具有基于数组的列表函数的数据结构项目,我似乎无法摆脱这个无限循环。
- Qt Widget项目中的无限循环