2013-05-10 70 views
-3

我对c语言的功能有疑问。我有一个插入数字调用函数的菜单,但问题是我不想使用for,我想在每次按菜单中的选项a时逐个插入数字。我也只想打印当我选择b选项时输入的数字。我只是不知道如何解决柜台问题。对不起,如果有语法错误,英语不是我的第一语言。带功能的菜单

#include<stdio.h> 
//Functions 
char menu(); 
void insert (int[],int); 
void print (int[],int); 

//****************************** 
//CUERPO 
int main(){ 
    int lenght=5; 
    int num [lenght]; 
    char option; 

    while((option=menu())!='x'){ 

     switch (option){ 

     case 'a': 
      insert(num,largo); 
      break; 

     case 'b': 
      print (num,largo); 
      break; 
     } 
    } 
    system ("pause"); 
    return 0; 
} 

/* Codes ************************************************************** */ 

char menu(){ 
    char option; 
    printf("\nInsert an option :"); 
    printf("\nA. insert :"); 
    printf("\nB. print :"); 

    scanf("%c", &option); 
    fflush (stdin); 
    return option; 
} 

void insert (int a[], int lenght){ // Here i have the problem 
    int x=0; 
    printf("\nInsert your number %d: ", x); 
    scanf("%d", &a[x]); 
    x++; 
} 

void print (int a[], int lenght){ 
    int y; 
    for(y=0; y<largo; y++){ 
     printf("\nThe numer you have entered are %d: ", a[y]); 
    } 
} 
+0

这是您使用C90或C99标准?在插入x中是没有用的。可能是你错过了一个循环或是静态的?插入功能中还没有使用长度?是'largo'应该是'长度?' – 2013-05-10 16:49:25

+0

我很确定我明白你想要完成什么以及哪里出错,但最好不要让它变得模糊不清。你能解释一下你的输入是什么,你期望的结果是什么,结果是什么? – jerry 2013-05-10 16:54:51

回答

0

您应该在主函数中声明一个“x”变量。

int x = 0; 

添加用户后,选择选项“插入” X

insert(num,largo); 
x++; 

的增量和最终改变你的“插入”功能,接受“X”,而不是申报。

void insert (int a[], int lenght, int x){ 
    printf("\nInsert your number %d: ", x); 
    scanf("%d", &a[x]); 
} 
+0

是的,我明白了!非常感谢!! – esteban 2013-05-10 18:15:50

+0

如果它帮助你,投票。 – 2013-05-10 18:57:04

0
#include <stdio.h> 
#include <ctype.h> 
//Functions 
char menu(); 
void insert (int[],int); 
void print (int[],int); 
//****************************** 
//CUERPO 
int main(){ 
    int lenght=5; 
    int num [lenght]; 
    int largo=-1; 
    char option; 

    while(tolower((option=menu()))!='x'){ 

     switch (option){ 

     case 'a': 
      if(largo == 4){ 
       printf("\nalready full inputted!!\n"); 
       break; 
      } 
      insert(num,++largo); 
      break; 

     case 'b': 
      print (num,largo); 
      break; 
     } 
    } 
    system ("pause"); 
    return 0; 
} 

/* Codes ************************************************************** */ 

char menu(){ 
    char option; 
    printf("\nInsert an option :"); 
    printf("\nA. insert :"); 
    printf("\nB. print :"); 
    printf("\nX. quit. >"); 

    scanf(" %c", &option); 
    while(fgetc(stdin)!='\n');//skip over input 
// fflush (stdin); 
    return option; 
} 

void insert (int a[], int lenght){ // Here i have the problem 
    printf("\nInsert your number %d: ", lenght); 
    scanf("%d%*c", &a[lenght]); 
} 

void print (int a[], int lenght){ 
    printf("\nThe numer you have entered are %d: ", a[lenght]); 
}