So I am working on a program, I'm a net admin and terrible at programming, and need some help with structures, arrays, and possibly pointers. I am trying to write a program using struct, I know I need arrays but I am not sure how to properly tie them in with the struct. All info needs to be user input and needs to break on command to either skip to end or Loop back to certain point to enter more info. I need the user to input the employee ID#, then be able input multiple review scores, like 100, 90, 80, then break that sequence and either go back and enter another employee# and keep going, or skip to the end and print out all info entered.
所以我正在研究一个程序,我是一个网络管理员,编程很糟糕,需要一些结构,数组和指针的帮助。我正在尝试使用struct编写程序,我知道我需要数组,但我不确定如何正确地将它们与结构绑定。所有信息都需要是用户输入,并且需要按命令中断以跳转到结束或循环回到某个点以输入更多信息。我需要用户输入员工ID#,然后输入多个评论分数,如100,90,80,然后打破该序列,然后返回并输入另一名员工#并继续,或跳到最后并打印输出所有信息。
The employee ID number and score entry seems to work fine when entering but when I print it out it does not look right so I am obviously doing something wrong with how the data is being stored and then printed. code and results below.
输入时员工ID号和分数条目似乎工作正常但是当我打印出来时它看起来不正确所以我显然在如何存储和打印数据方面做错了。代码和结果如下。
#include <stdio.h>
struct help{
int empID;
int marks[100];
};
int main(){
struct help s[100];
int i, input, empNUM;
NEWENTRY: printf("Enter employee ID#: ");
scanf("%d", &empNUM);
for(i=0;i<10;++i){
s[i].empID = empNUM;
printf("\nFor employee ID# %d\n",s[i].empID);
while (i <= 100) {
printf("Enter score:");
if (scanf("%d", &input) == 1) {
if (input >= 0 && input <= 100) {
s[i].marks[100] = input;
i++;
}
else if (input == 101) {
printf("\n\nExiting entry.\n");
i = i - 1;
goto NEWENTRY;
}
else if (input == 102) {
printf("\n\nExiting entry.\n");
i = i - 1;
goto EXIT;
}
}
}
}
EXIT:
for(i=0;i<10;++i) {
printf("\nInformation for employee ID number %d:\n",s[i].empID);
printf("Marks: %.1f",s[i].marks);
}
return 0;
}
I would like it to look remotely like this if possible.
如果可能的话,我希望它看起来像这样。
info for emp id 12345:
100
90
80
info for emp id 67890:
80
90
60
1 个解决方案
#1
2
There are many problems in your code :
您的代码中存在许多问题:
Problem 1 :
问题1:
Here in your code,
在你的代码中
you are using the same parameter
i
for the outerfor
loop and innerwhile
loop :你使用相同的参数i为外部for循环和内部while循环:
for(i=0;i<10;++i) //you are using i here
{
s[i].empID = empNUM;
printf("\nFor employee ID# %d\n",s[i].empID);
while (i <= 100) //and even here
{
printf("Enter score:");
Problem 2 :
问题2:
apart from that every time you goto
NEWENTRY:
and then again enter the for
loop, i
value is again set to 0
,
除此之外每次你转到NEWENTRY:然后再次进入for循环,我的值再次设置为0,
so no matter how many entries you may enter, you will only populate the first element of struct array
s
i.e,s[0]
所以无论你输入多少个条目,你只会填充struct array s的第一个元素,即s [0]
Problem 3:
问题3:
you are using wrong arguments while printing your array here :
在这里打印数组时使用错误的参数:
printf("Marks: %.1f",s[i].marks);
here
s[i].marks
is of the typeint*
, you are using it to print adouble
data这里的s [i] .marks是int *类型,你用它来打印双数据
Solution :
方案:
The simple solution I can give is that :
我能给出的简单解决方案是:
never use
goto
(click to see why :) )永远不用usegoto(点击查看原因:))
as it makes your code very complex to understand and more reasons can be known by clicking it.
因为它使您的代码理解起来非常复杂,单击它可以了解更多原因。
you can instead achieve what you are trying to do without using goto
this way:
你可以在不使用goto的情况下实现你想要做的事情:
#include <stdio.h>
struct help
{
int empID;
int marks[100];
};
int main()
{
struct help s[100];
int i, j; //useful for indices of array
int val; //useful for taking in user input
int flag=0; //useful for exiting the program
for(i=0;i<10;)
{
printf("Enter employee ID#: ");
scanf("%d",&s[i].empID);
printf("\nFor employee ID# %d\n",s[i].empID);
for(j=0;j<100;j++)
{
printf("Enter score : ");
scanf("%d",&val); //taking in score input
if(val>=0 && val<=100)
{
s[i].marks[j]=val;
}
else if(val==101)
{
s[i].marks[j]=-1; //to mark the end of entries I used -1
break;
}
else if(val==102)
{
s[i].marks[j]=-1;
flag=1; //to know whether user wants to exit
break;
}
else
{
printf("\ninvalid entry\n");
j--;
}
}
printf("\n\n----------\n\n");
i++;
if(flag==1) //to exit
break;
}
int num=i;
for(i=0; i<num ; i++)
{
printf("\nInformation for employee ID number %d:\n",s[i].empID);
for(j=0; s[i].marks[j]!=-1; j++)
printf("Marks: %d\n",s[i].marks[j]);
}
printf("\nenter any key to exit!\n");
scanf("%d",&i);
return 0;
}
This logic is quite simple to understand :
这个逻辑很容易理解:
- the nested
for
loop I used is just like any other nestedfor
loop used for populating a 2D array - 我使用的嵌套for循环就像用于填充2D数组的任何其他嵌套for循环一样
-
in the inner
for
loop,val
takes in the user's input and goes through an else if ladder在内部for循环中,val接受用户的输入并通过else if梯形图
- if
val
is between0
and100
, it gets accepted and stored inmarks
array ofs[i]
- 如果val介于0和100之间,它将被接受并存储在s [i]的标记数组中
- else-if
val
is101
, then-1
is inserted to mark the end ofmarks
array ofs[i]
and you break out of innerfor
loop andi
value is incremented - else-if如果val为101,则插入-1以标记s [i]的标记数组的结尾,并且您将跳出内部for循环并且i值递增
-
else-if
val
is102
, then similarly否则 - 如果val是102,那么类似
-
-1
is inserted to mark the end ofmarks
array ofs[i]
. - 插入-1以标记s [i]的标记数组的结尾。
- additionally flag which has been
0
till now is assigned to1
and this helps in exiting the outerfor
loop - 另外将0到现在为止的标志分配给1,这有助于退出外部for循环
-
- else (that is for all other cases), the number is not accepted and
j
value is decremented to retake the value - else(对于所有其他情况),不接受该数字,并且递减j值以重新获得该值
- if
-
and finally you print the scores of employees just as you print values 2D array and also using the fact that we assigned end of each
marks
array ofs[i]
with a-1
.最后你打印员工的分数就像打印值2D数组一样,并且使用我们为每个标记数组s [i]指定了一个-1的事实。
#1
2
There are many problems in your code :
您的代码中存在许多问题:
Problem 1 :
问题1:
Here in your code,
在你的代码中
you are using the same parameter
i
for the outerfor
loop and innerwhile
loop :你使用相同的参数i为外部for循环和内部while循环:
for(i=0;i<10;++i) //you are using i here
{
s[i].empID = empNUM;
printf("\nFor employee ID# %d\n",s[i].empID);
while (i <= 100) //and even here
{
printf("Enter score:");
Problem 2 :
问题2:
apart from that every time you goto
NEWENTRY:
and then again enter the for
loop, i
value is again set to 0
,
除此之外每次你转到NEWENTRY:然后再次进入for循环,我的值再次设置为0,
so no matter how many entries you may enter, you will only populate the first element of struct array
s
i.e,s[0]
所以无论你输入多少个条目,你只会填充struct array s的第一个元素,即s [0]
Problem 3:
问题3:
you are using wrong arguments while printing your array here :
在这里打印数组时使用错误的参数:
printf("Marks: %.1f",s[i].marks);
here
s[i].marks
is of the typeint*
, you are using it to print adouble
data这里的s [i] .marks是int *类型,你用它来打印双数据
Solution :
方案:
The simple solution I can give is that :
我能给出的简单解决方案是:
never use
goto
(click to see why :) )永远不用usegoto(点击查看原因:))
as it makes your code very complex to understand and more reasons can be known by clicking it.
因为它使您的代码理解起来非常复杂,单击它可以了解更多原因。
you can instead achieve what you are trying to do without using goto
this way:
你可以在不使用goto的情况下实现你想要做的事情:
#include <stdio.h>
struct help
{
int empID;
int marks[100];
};
int main()
{
struct help s[100];
int i, j; //useful for indices of array
int val; //useful for taking in user input
int flag=0; //useful for exiting the program
for(i=0;i<10;)
{
printf("Enter employee ID#: ");
scanf("%d",&s[i].empID);
printf("\nFor employee ID# %d\n",s[i].empID);
for(j=0;j<100;j++)
{
printf("Enter score : ");
scanf("%d",&val); //taking in score input
if(val>=0 && val<=100)
{
s[i].marks[j]=val;
}
else if(val==101)
{
s[i].marks[j]=-1; //to mark the end of entries I used -1
break;
}
else if(val==102)
{
s[i].marks[j]=-1;
flag=1; //to know whether user wants to exit
break;
}
else
{
printf("\ninvalid entry\n");
j--;
}
}
printf("\n\n----------\n\n");
i++;
if(flag==1) //to exit
break;
}
int num=i;
for(i=0; i<num ; i++)
{
printf("\nInformation for employee ID number %d:\n",s[i].empID);
for(j=0; s[i].marks[j]!=-1; j++)
printf("Marks: %d\n",s[i].marks[j]);
}
printf("\nenter any key to exit!\n");
scanf("%d",&i);
return 0;
}
This logic is quite simple to understand :
这个逻辑很容易理解:
- the nested
for
loop I used is just like any other nestedfor
loop used for populating a 2D array - 我使用的嵌套for循环就像用于填充2D数组的任何其他嵌套for循环一样
-
in the inner
for
loop,val
takes in the user's input and goes through an else if ladder在内部for循环中,val接受用户的输入并通过else if梯形图
- if
val
is between0
and100
, it gets accepted and stored inmarks
array ofs[i]
- 如果val介于0和100之间,它将被接受并存储在s [i]的标记数组中
- else-if
val
is101
, then-1
is inserted to mark the end ofmarks
array ofs[i]
and you break out of innerfor
loop andi
value is incremented - else-if如果val为101,则插入-1以标记s [i]的标记数组的结尾,并且您将跳出内部for循环并且i值递增
-
else-if
val
is102
, then similarly否则 - 如果val是102,那么类似
-
-1
is inserted to mark the end ofmarks
array ofs[i]
. - 插入-1以标记s [i]的标记数组的结尾。
- additionally flag which has been
0
till now is assigned to1
and this helps in exiting the outerfor
loop - 另外将0到现在为止的标志分配给1,这有助于退出外部for循环
-
- else (that is for all other cases), the number is not accepted and
j
value is decremented to retake the value - else(对于所有其他情况),不接受该数字,并且递减j值以重新获得该值
- if
-
and finally you print the scores of employees just as you print values 2D array and also using the fact that we assigned end of each
marks
array ofs[i]
with a-1
.最后你打印员工的分数就像打印值2D数组一样,并且使用我们为每个标记数组s [i]指定了一个-1的事实。