如何将char存储到数组? [英] How do I store char to array?

查看:229
本文介绍了如何将char存储到数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  char  menuoption; 
double i [ 4 ];
double employeenumber = 0 ,hoursworked = 1 ,payrate = 2 ,tax = 3 ;

do
{

printf( 请从以下菜单中选择一个选项:\ n);
printf( A或a添加员工信息\ n);
printf( D或d显示员工信息\ n);
printf( T或t以显示总工资单\ n);
printf( S或s显示所有员工的信息\ n) ;
printf( Z或z退出程序\ n);

scanf( %c,& menuoption);

switch (menuoption)
// 添加员工信息
案例 ' a' case ' A'
for (i = 0 ; i< ; = 5 ; i ++)
printf( 请输入员工编号:);
scanf( & lf,& employeenumber);
scanf( & lf,& employeenumber);
printf( \ n请输入工作小时数:);
scanf( %lf,& hoursworked);
printf( \ n请输入工作小时数:);
scanf( %lf,& hoursworked);
printf( \ n请输入员工税减免:);
scanf( %lf,& tax);









这是我第一次编程,我不明白如何存储这个信息变成一个数组......任何事情都会有所帮助!

解决方案

有静态数组和动态分配的数组。



静态数组

  //   define和初始化 
#define N 5
char c [N] = {' a'' < span class =code-string> b',' c'' d'' E'};
// 更改值,索引从0开始
c [ 2 ] = ' x';
// 访问价值
char y = c [ 3 ];





动态数组: http://www.cplusplus.com/reference/cstdlib/malloc/ [ ^ ]



  #include   <   stdlib.h  >  
// 使用malloc或calloc
char c [] = ( char *)calloc( 5 sizeof char ));
// 完成后
free(c);
< /stdlib.h>


让我帮您解决一些问题。您可以将数组用作:

  double  employeeArray [ 5 ]; 



这里你可以将变量分配给数组的元素,如:

 employeeArray [ 0 ] = employeeNumber; 





但是,他们是更好的方法来做到这一点。我知道你刚开始编程所以需要时间来达到名为'structs'的结构,但是如果你有很多员工,并且他们每个人都有一些与之相关的细节或信息,那么你可以考虑使用结构


char menuoption;
double i[4];
double employeenumber=0, hoursworked=1, payrate=2, tax=3;

do
{

printf("Please choose an option from the following menu:\n");
printf("A or a to add employee info\n");
printf("D or d to display employee info\n");
printf("T or t to display total payroll\n");
printf("S or s to display the info of all employees\n");
printf("Z or z to exit program\n");

scanf(" %c", &menuoption);

switch (menuoption)
//add employee info
case 'a': case 'A':
for(i=0; i<=5; i++)
	printf("Please enter employee number: ");
	scanf("&lf", &employeenumber);
	scanf("&lf", &employeenumber);
	printf("\nPlease enter hours worked: ");
	scanf("%lf", &hoursworked);
	printf("\nPlease enter hours worked: ");
	scanf("%lf", &hoursworked);
	printf("\nPlease enter the employee tax deduction: ");
	scanf("%lf", &tax);





This is my first time ever programming, and I don't understand how to store this information into an array... Anything will help!

解决方案

There is static arrays and dymanically allocated arrays.

static array

//define and initialise
#define N 5
char c[N] = {'a','b','c','d','e'};
//change a value, index starts at 0
c[2]='x';
//access value
char y=c[3];



dynamic array: http://www.cplusplus.com/reference/cstdlib/malloc/[^]

#include <stdlib.h>
//use malloc or calloc
char c[] = (char*)calloc(5,sizeof(char));
//when finished
free(c);
</stdlib.h>


Well let me help you out with something. You can use the arrays as:

double employeeArray[5];


here you can assign the variables to the elements of the array like:

employeeArray[0] = employeeNumber;



But, their is far better way to do this. I understand that you have just started with programming so it will take time to reach to the constructs called 'structs', but if you have many employee and each of them is having some details or information associated with them then you might consider using the structs.


这篇关于如何将char存储到数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆