我正在为班级做作业:
创建一个程序,允许用户输入最多10个朋友地址。使用二维数组来存储朋友的地址。输入每个地址后,用户应该可以选择输入另一个地址或打印出一个报告,显示到目前为止输入的每个地址。
我已经创建了一个没有错误的代码,但是我没有得到理想的结果。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main ()
{
char name[10][10] = {0};
char address[10][10] = {0};
int choice;
printf("\n\nWelcome to the address book!\n\n");
printf("Please enter a name...\n");
scanf("%s",name);
printf("Please enter an address...\n");
scanf("%s",address);
printf("Would you like to (1)Enter another address, or (2)Print the address book?\n");
scanf("%i",&choice);
switch (choice)
{
case 1:
for (int i=0;i<10;i++)
{
printf("Please enter a name...\n");
scanf("%s",name[i]);
printf("Please enter an address...\n");
scanf("%s",address[i]);
printf("Would you like to (1) Enter another address, or (2)Print the address book?\n");
scanf("%i",&choice);
if (choice == 1)
{
for (int i=0;i<10;i++)
{
printf("Please enter a name...\n");
scanf("%s",name[i]);
printf("Please enter an address...\n");
scanf("%s",address[i]);
printf("Would you like to (1) Enter another address, or (2)Print the address book?\n");
scanf("%i",&choice);
}
} else if (choice == 2)
{
for (int i = 0; i<10; i ++)
{
printf("%s\n", name[i]);
printf("%s\n", address[i]);
}
}
}
break;
case 2:
for (int i = 0; i<10; i ++)
{
printf("%s\n", name[i]);
printf("%s\n", address[i]);
}
break;
}
return (0);
}
我的麻烦来自输出。我能够填充阵列,但我无法打印出我想要的结果。我在哪里丢失它?
答案 0 :(得分:0)
您应该像其他人一样编写代码,并尝试弄清楚。如果您发现错误,请修复它。
例如:代码通过询问用户是否要打印地址簿或添加到地址簿来开始。 如果用户选择添加到地址簿,则会有一个循环执行10次。在循环中有两个部分要求地址 - 这似乎很复杂,为什么不只问一次? (此时修复看似错误的内容)。
您还有两个地址可以打印地址,简化它也只打印一次。
最后,这不是你想要的:
char name[10][10] = {0};
char address[10][10] = {0};
要么你想要这个:
char name[10] = {0};
char address[10] = {0};
或者你想要:
char address[10][2] = {0}; /* address[x][0] is name, address[x][1] is address */
或者你想要10个结构的数组。
编辑: 学习编程起初需要大量的反复试验。我找到了一个很好的方法来帮助理解程序正在做什么,正在使用以下程序:
printf("1 -\n");
...
printf("2 -\n");
...
etc.
这样一来,当出现问题时,你知道从哪里开始寻找。并且请记住,计算机将完全按照您的要求进行操作,因此如果计算机无法满足您的要求,那么您的解释就不够了。
答案 1 :(得分:0)
你的程序似乎有些混乱,但作为你的C新手,它是学习过程的一部分。
<强>问题: - 强>
1)当你的程序第一次启动时,你要求用户输入名称和地址并将它们存储到数组中,好吧没关系。但是当你再问同样的事情时(你想做什么...(1)(2)),你正在使用来自i=0
索引的相同数组,这将覆盖你在程序开始时输入的名称和地址。你已经完成了每个循环都是一样的。它总是会覆盖数组的内容。
2)实际上你的程序叫做MENU DRIVEN程序,菜单总会引导用户做他想做的事情,所以你应该这样想,&#34; 应该只有一个菜单指导用户并应尝试重复使用此菜单 ,&#34;和菜单的每个模块应该只做一种类型的事情。
在你的情况下: -
选择(1)应该要求用户输入用于存储的名称和地址
选择(2)应该要求用户打印地址簿。
(但是在你的程序中,你在选择1中编写了相同的选择代码2,如果你想在选择1中做出选择2,为什么在第一个地方写出选择2 ...... ???我的意思是如果你想执行选择2,你必须向用户显示相同的菜单。)
执行选择后,如果要再次重新执行,则必须保留该选项 一个循环。 (正如我在下面的例子中做的那样选择。因为我想再次询问用户是否要输入其他名称和地址)。
int main ()
{
char name[10][10] = {0};
char address[10][10] = {0};
int choice;
int i=0;
char another; //Variable used to store a another choice...
while(1) //This is infinite loop....
{
printf("Would you like to \n(1) Enter Name and address.\n(2) Print the address book? \n(3) Exit\n");
scanf("%i",&choice);
switch (choice) //start of switch.....
{
case 1: //start of case 1...
if(i<10) //executes the below code only if i<10 (if array still has empty cells.)
{
do
{
printf("Please enter a name...\n");
scanf("%s",name[i]);
printf("Please enter an address...\n");
scanf("%s",address[i]);
printf("\nWant 2 Enter Another name and address...[y/n]:");
scanf(" %c",&another);
i++;
}while((another=='y'||another=='Y'));
}
break; //end of case 1...
case 2: //start of case 2...
for ( i = 0; i<10; i ++)
{
printf("Name: %s\n", name[i]);
printf("Address: %s\n\n", address[i]);
}
break; //End of case 2...
case 3:
return 0; //This ends program when users enter choice 3..
} //End of switch...
}//End of while loop..
}//End of Main Function....