我无论如何都不是C程序员,但是已经使用PHP和Perl多年了。正在协助使用结构的C程序的人。
我有以下代码-
#include<stdlib.h>
#include<stdio.h>
#define size 2
#define max 2
struct student //initialising...
{
char coursecode[100];
char coursename[10];
};
main()
{
int i,z;//for the loops
struct student stud[10];
printf("Enter available course name and their corresponding codes\n");
for(z=0;z<max;z++)
{
printf("Course Name: ");
scanf("%s",&stud[z].coursename);
printf("Course Code: ");
scanf("%d",&stud[z].coursecode);
printf("-----INFORMATION RECORDED-----\n");
printf("\n");
}
printf("\n");
printf("SUBJECTS");
printf("\n");
printf("\n");
for(z=0;z<max;z++) // Prints them back to the screen
{
printf("%s %d\n",stud[z].coursename, stud[z].coursecode);
printf("\n");
}
printf("\n");
printf("\n");
}
我得到以下输出
Enter available course name and their corresponding codes
Course Name: Art
Course Code: 101
-----INFORMATION RECORDED-----
Course Name: Biology
Course Code: 102
-----INFORMATION RECORDED-----
SUBJECTS
Art 6485496
Biology 6485704
--------------------------------
Process exited after 7.501 seconds with return value 0
Press any key to continue . . .```
我不确定为什么会给出此结果,并且希望获得任何帮助。
谢谢
史蒂夫
答案 0 :(得分:0)
您正在使用public class ListActivity extends AppCompatActivity implements ListAdapter.ItemClickListener {
ArrayList<Country> countriesList;
ListAdapter adapter;
RecyclerView rv;
protected void onCreate(@Nullable Bundle savedInstanceState) {
adapter = new ListAdapter(context, this);
adapter.setClickListener(this);
//more irrelevant code
}
}
private Country getCurrentCountry(){
return currentCountry;
}
public void setCurrentCountry(Country currentCountry){
this.currentCountry = currentCountry;
}
@Override
public void onItemClick(View view, int position) {
FragmentTransaction ft;
Bundle countryBundle = new Bundle();
countryBundle.putParcelable("countriesList", getCurrentCountry());
Fragment countryFragment = new CountryFragment();
countryFragment.setArguments(countryBundle);
ft = getSupportFragmentManager().beginTransaction();
ft.add(R.id.fragments_container, countryFragment);
Log.d("Monitoring", getCurrentCountry().toString());
ft.commit();
}
存储课程代码,并要求使用public class ListAdapter extends RecyclerView.Adapter<ListAdapter.RowViewHolder> {
// Adapter class code
...
class RowViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
TextView viewName;
ImageView viewFlag;
LinearLayout countryEntry;
RowViewHolder(View view) {
super(view);
viewName = view.findViewById(R.id.name);
viewFlag = view.findViewById(R.id.flag);
countryEntry = view.findViewById(R.id.rowId);
view.setOnClickListener(this);
}
public void onClick(View view) {
if (mClickListener != null) mClickListener.onItemClick(view, getAdapterPosition());
ListActivity.position = getAdapterPosition();
final Country currentCountry = countriesList.get(getAdapterPosition());
listActivity.setCurrentCountry(currentCountry);
}
}
public void setClickListener(ItemClickListener itemClickListener) {
this.mClickListener = itemClickListener;
}
public interface ItemClickListener {
void onItemClick(View view, int position);
}
//more irrelevant code
}
格式化程序提供整数!
您可以按照注释中的建议使用char[100]
或%d
来请求一个字符串,或者如果您的课程代码只是一个整数,则可以将coursecode属性设为一个整数。
答案 1 :(得分:0)
您的课程代码被声明为字符数组char coursecode[100]
。因此,在
scanf("%d",&stud[z].coursecode);
不应使用"%d"
格式说明符,并且您也不需要&
(的地址)运算符。
scanf("%99s",stud[z].coursecode);
相反,其中99
用于限制输入,"%s"
用于将输入读取到char数组中。也对scanf("%s",&stud[z].coursename);
执行此操作,以使其读取scanf("%9s",stud[z].coursename);
。您还应该将printf
更改为使用"%s
格式说明符。
答案 2 :(得分:0)
在第7行,您定义了一个char char coursecode[100]
数组(用作String类型)。
首先,当您读取带有scanf("%d",&stud[z].coursecode)
的输入时,您要输入int变量
然后再次使用%d
类型变量使用的int
打印
根据您想要coursecode
的方式,您有两种可能的解决方案:
1)
您可以在scanf和printf中使用%s
(与coursename []相同),
这样您的代码就会更改:
scanf("%s",&stud[z].coursecode)
printf("%s %s\n",stud[z].coursename, stud[z].coursecode);
(也许使用\t
作为间隔)
通过这种方式,您将课程代码视为“字符串”。
OR
2)您将char coursecode[100]
声明为int coursecode
,并保持scanf和printf相同
答案 3 :(得分:0)
在您的结构中,您表示课程代码是一个字符。但是随后您将其引用为%d,就好像它是整数一样。将其更改为%s,祝您一切顺利