C 在开关盒中使用int数组
我对C相当陌生,我正在尝试使用int数组作为这样一个开关的选项号C 在开关盒中使用int数组,c,arrays,switch-statement,C,Arrays,Switch Statement,我对C相当陌生,我正在尝试使用int数组作为这样一个开关的选项号 void totalEntered(float * total, float * inputFigure) { *total = *total + *inputFigure; } int main() { float amountEntered = 0.00; float tot = 0.00; int option_numbers[3] = {1,2,3,4}; float a_food
void totalEntered(float * total, float * inputFigure)
{
*total = *total + *inputFigure;
}
int main()
{
float amountEntered = 0.00;
float tot = 0.00;
int option_numbers[3] = {1,2,3,4};
float a_food = 0.00;
float a_tab = 0.00;
float a_trav = 0.00;
float a_bill = 0.00;
totalEntered(&tot, &amountEntered);
printf("Please enter option number from below \n1.food\n2.tabacco\n3.travel\n4.bills\n");
scanf("%i", option_numbers);
switch(option_numbers)
{
case 1:
printf("Please Enter an amount: ");
scanf("%f", amountEntered);
a_food = a_food + amountEntered;
totalEntered(&tot, &amountEntered);
printf("You have spent %f on food", a_food);
break;
case 2: /*ignore below haven't finish that*/
a_tab = a_tab + amountEntered;
printf("You have spent %.2f on tabacco", a_tab);
break;
case 3:
a_trav = a_trav + amountEntered;
printf("You have spent %.2f on travel", a_trav);
break;
case 4:
a_bill = a_bill + amountEntered;
printf("You have spent %.2f on travel", a_bill);
break;
default:
printf("You have not input a category!");
break;
}
return 0;
}
有谁能建议怎么做。。。我有点卡住了!:D我试图让用户选择一个数字,如果用户想在食物下输入一个数量,他/她必须选择选项1,然后选择一个数量。希望这比以前更有意义!
谢谢您不能将数组传递到
开关()
您需要传递数组中的一个元素:
int item = 2; // or whatever
switch(option_numbers[item])
{
编辑:
根据您的评论,如果您想让用户输入并让他们从4个选项中选择一个,这非常简单,完全不需要数组
int selection = -1;
printf("Which option do you want? (1-4): ");
scanf("%d", &selection);
switch(selection)
{
旁注:
int option_numbers[3] = {1,2,3,4}; // that's not size 3, it's got 4 elements should
// have been:
// int option_numbers[4] = {1,2,3,4};
您试图打开一个包含所有数字的数字数组。您应该在指定了特定编号的int变量上进行切换。
int option_numbers[]={1,2,3,4};
int option_numbers[] = {1,2,3,4};
int i;
for (i = 0; i < sizeof(option_numbers)/sizeof(int); ++i)
{
switch(option_numbers[i])
{
....
}
}
int i;
对于(i=0;i
您可以根据需要更改索引上限。您的问题不清楚,所以我将回答您想问的问题。首先,使用switch语句的更好方法是使用enum。我不善于解释,所以这里有一个代码示例:(这里有一个链接:) 大致(非常非常大致)相当于:
int Food = 0;
int Tobacco = 1;
int Travel = 3;
int Bills = 4;
因此,在switch语句中使用它的一种非常简单的方法是:
在你的主要任务()之前的某个地方:
然后在您的主要()中:
---编辑——(2013年3月14日……哇,今天是pi日!)
好的,当我今天早上打开电脑时,我突然意识到还有另一种方法可以做到这一点。。。您可以使用这样一个事实,即字符只是伪装的1字节数字;)。。。它更好,因为它不依赖scanf(我真的不喜欢scanflol):
你想用这个达到什么目的?还不清楚…你希望数组中的哪个值在开关中测试?@Jalcock501-检查对我答案的编辑,如果我正确理解你所说的,数组在这里完全没有意义,你只需要在单个变量中获得用户的输入,然后你就可以使用
开关()
。注意:我跳过了错误检查,但您不应该;)谢谢,这真的很有帮助。我会试一试,看看我的进展如何。检查我的新编辑,可能会容易一点。它避免了enum和scanf。
int Food = 0;
int Tobacco = 1;
int Travel = 3;
int Bills = 4;
typedef enum { Food, Tobacco, Travel, Bills } Options;
Options myOption; // just as an example
printf("Pick your expense thingy (0, 1, 2, 3): ");
scanf("%d", &myOption);
switch (myOption) { // Now remember that Food = 0, Tobacco = 1 ...
case Food: // food == 0
// do food stuff
break;
case Tobacco:
// do Tobacco stuff
break;
case Travel:
// do Travel stuff
break;
case Bills:
// do Bills stuff
break;
}
char input;
printf("Pick your expense thingy (_f_ood, _t_obacco, t_r_avel, _b_ills): ");
input = getchar(); // this function just gets a char off of stdin and returns it
switch(input) {
case 'f': case 'F': // note the single quotes, this tells the compiler that it is a single character and not a null terminated string
// do food stuff
break;
case 't': case 'T': // we use both letters so that our comparisons are case-insensitive
// do tobacco stuff
break;
case 'r': case 'R': // use r for travel because its the easiest way to differentiate the input
// do travel stuff
break;
case 'b': case 'B': // so yeah...
// do bills stuff
break;
default:
printf("That was not a valid option :(");
}