我正在编写一个C程序,为动态字符串数组创建交互界面.该程序是菜单驱动的,并遵循以下步骤:
- 询问用户数组的初始长度.
- 将相应的字符串存储到一个用该长度初始化的动态数组中.
- 启动菜单循环,提示用户 Select 以下五个选项之一: (a)将一个字符串添加到数组的末尾, (b)将一个字符串添加到数组的开头, (c)删除数组索引'x'处的元素(作为输入), (d)显示数组的长度, (e)按顺序显示数组的所有元素.
- 添加和删除字符串会导致调整数组的大小.
- 提供第六个选项来关闭菜单驱动的程序,从而终止循环.
代码:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
typedef struct UserStrings
{
char in[100];
} input;
int main(void)
{
int n;
printf("Enter size of Array, please: \n");
scanf(" %d", &n);
input *names = (input *)malloc(n * sizeof(input));
for (int i = 0; i < n; ++i)
{
printf("Enter String Number %d : ", i + 1);
scanf("%s", names[i].in);
}
printf("%d\n",n);
char temp = ' ';
while (temp != 'f')
{
printf("Enter -a- to add an element at the end of the array\n"
"Enter -b- to add a string at the beginning of the array\n"
"Enter -c- to delete an element at an index\n"
"Enter -d- to display the length of the array\n"
"Enter -e- to display all elements\n"
"Enter -f- to terminate this program\n");
scanf("%s", &temp);
if (temp == 'a')
{
names = (input *)realloc(names, (n + 1)*sizeof(input));
printf("Enter the string\n");
scanf("%s", names[n].in);
++n;
printf("%d\n",n);
}
else if (temp == 'b')
{
names = (input *)realloc(names, (n + 1)*sizeof(input));
++n;
printf("Enter the string\n");
for (int i = n - 1; i > 0; --i)
{
strcpy(names[i].in, names[i - 1].in);
}
scanf("%s", names[0].in);
printf("%d\n",n);
}
else if (temp == 'c')
{
printf("Enter the index 0-%d: ", n - 1);
int ind;
scanf(" %d", &ind);
if (ind < 0 || ind >= n)
{
printf("Invalid index. Please enter a valid index.\n");
continue; // Skip the rest of the loop iteration
}
input *new = (input *)malloc((n - 1) * sizeof(input));
int flag = 0;
for (int i = 0; i < n; ++i)
{
if (i == ind)
{
flag = 1;
continue; // Skip the element at the specified index
}
new[i - flag] = names[i];
}
free(names);
names = new;
new = NULL;
--n;
printf("%d\n",n);
}
else if (temp == 'd')
{
printf("Lenght of the array is %d\n", n);
}
else if (temp == 'e')
{
for (int i = 0; i < n; ++i)
{
printf("%d is : %s \n", i + 1, names[i].in);
}
}
else if (temp == 'f')
{
printf("Program terminated!\n");
return 1;
}
else
printf("PLEASE ENTER IN SMALL CAPS AND CORRECT INPUT\n");
}
}
我try 使用printf语句调试代码,但我丢失了n的值.当我初始化数组时,它似乎具有期望值.但当我try 任何操作后,n的值发生了意外的变化.以下是输出.
请帮帮我
Enter size of Array, please:
2
Enter String Number 1 : A
Enter String Number 2 : B
2 // This is the output of printf. So n is fine here.
Enter -a- to add an element at the end of the array
Enter -b- to add a string at the beginning of the array
Enter -c- to delete an element at an index
Enter -d- to display the length of the array
Enter -e- to display all elements
Enter -f- to terminate this program
e // No elements are printing.
Enter -a- to add an element at the end of the array
Enter -b- to add a string at the beginning of the array
Enter -c- to delete an element at an index
Enter -d- to display the length of the array
Enter -e- to display all elements
Enter -f- to terminate this program
d
Lenght of the array is 0 /* Length of array is 0? In the whole code, I cant spot why n will change using these operations. */