简体   繁体   English

为什么 <null> 每次我调用不同的功能时都会消失吗?

[英]Why <null> apears every time I call a different function?

Being a newbie at programming I make many mistakes but I cannot understand perfect pointers.I wrote a code in which into the function Add I add data about food schedule.After with function Modify I want my code to print the last added data.But here is the problem,it prints me everytime .Do you know how to fix that? 作为编程的新手,我会犯很多错误,但是我无法理解完美的指针。我编写了一个代码,在函数Add中添加了有关食物计划的数据。使用Modify函数后,我希望我的代码打印最后添加的数据。是问题,它每次都会打印出来。您知道该如何解决吗?

int login ( char name[50] );
void autoPass( char username[50] );
char pass[50];
int n = -1; //global counter
char *table[100][4];
void Add( int n );
void Modify( int n );

int main()
{
    char selection[7];
    printf( "\n\nChoose between: Add,Modify,View,Search,Sort,Exit\nSelection: " );
    scanf ( "%s",&selection );

    while ( ( strcmp( selection,"Exit" )  == 0 ) == 0 )
    {
        if (strcmp( selection,"Add" ) == 0 )
        {
            Add( n );
            printf( "Press any character to continue or Exit to finish.\n" );
            scanf ( "%s",&selection );
            printf( "check1" );
        }else if ( strcmp( selection,"Modify" ) == 0 )
        {
            Modify( n );
            printf( "Press any character to continue or Exit to finish.\n" );
            scanf ( "%s", &selection );
            printf( "check2" );
        }
    }
}



void Add (int n)
{
    int i,j;
    n++;
    for ( j = 0 ; j < 4 ; j++ )
    {
        if ( j == 0 )
        {
            printf ( "Enter a food:\n" );
            table[n][j] = (char*) malloc( 30 );
            scanf( "%s",table[n][j] );
        }else if (j == 1 )
        {
            printf("Enter calories:\n");
            table[n][j]=(char*) malloc(30);
            scanf("%s",table[n][j]);
        }else if ( j == 2 )
        {
            printf( "Enter the time you ate:\n" );
            table[n][j] = (char*) malloc( 30 );
            scanf ( "%s",table[n][j] );
        }else if ( j == 3 )
        {
            if (atof( table[n][j-1]) >= 5.00 && atof( table[n][j-1] ) <= 11.59 )
            {
                table[n][j] = "prwino";
            }else if( atof( table[n][j-1] ) >= 12.00 && atof( table[n][j-1] ) <= 19.59 )
            {
                table[n][j] = "mesimeriano";
            }else if ( atof( table[n][j-1] ) >= 20.00 && atof( table[n][j-1] ) <= 4.59 )
            {
                table[n][j] = "vradino";
            }
        }
    }
}

void Modify( int n )
{
    int i,j;
    for ( j=0 ; j < 4 ; j++ )
    {
       printf ( "%s",table[n][j]," " );
     }
     printf( "\n" );
 }

I expected my Modify function to print the data I added before with function Add.Instead of this everytime it is printing me . 我希望我的Modify函数能够打印我之前使用Add函数添加的数据,而不是每次打印时都打印此数据。

Well your code seems to have some problems. 那么您的代码似乎有一些问题。

you should replace all scanf calls: 您应该替换所有scanf调用:

scanf ( "%s",&selection );

and change them with: 并通过以下方式更改它们:

scanf ( "%s",selection ); /// Drop the &

You need to drop the & because the format %s expects argument of type char* but when you use &selection the argument becomes char (*)[7] which is not char* like you declare it. 您需要删除&因为格式%s期望使用char*类型的参数,但是当您使用&selection该参数将成为char (*)[7] ,而不像您声明的那样是char*

Also you should ALWAYS check the return value of scanf : 另外,您应该始终检查scanf的返回值:

if ( scanf ( "%6s",selection ) != 1 ) ///notice 6 there?
{
    printf("Error, scanf()\n" );///Is there to read only 6...
    exit( EXIT_FAILURE );
}

Another issue is inside void Add ( int n ) , there you increment n : 另一个问题是在void Add ( int n ) ,在其中递增n

n++;

But you have declared also a n as global: int n = -1; //global counter 但是您还声明了n为全局变量: int n = -1; //global counter int n = -1; //global counter

Which n are you expecting to increment? 您希望增加哪个n

Inside the modify() functions you have too many arguments: modify()函数内部,您有太多参数:

printf ( "%s",table[n][j]," " );

Drop the last " " : 删除最后一个" "

printf ( "%s",table[n][j] );

As a suggestion you can use strcasecmp instead of strcmp . 建议您可以使用strcasecmp代替strcmp You need to include strings.h for that. 您需要为此添加strings.h

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 指向结构的指针在每个函数调用时都为NULL - pointer to a struct becomes NULL at every function call 每次运行时显示不同的结果 - Showing different results every time I run it 为什么每次在 GDB 中构建 + 反汇编函数时都会得到相同的地址? - Why do I get the same address every time I build + disassemble a function inside GDB? 为什么每次我调整终端大小时函数tgetnum(“ co”)都不更新? - Why the function tgetnum(“co”) does not update every time I resize the terminal? 每次调用stdio函数都会导致系统调用吗? - Does calling a stdio function result in a system call every time? 为什么以下程序的输出每次都不同(随机值)? - why the output of the following program different (random value) every time? 为什么我第二次调用我自己的 function 时 rand() function 不起作用? - Why doesn't rand() function work the second time i call my own function? 为什么这个OpenMP计划每次给我不同的答案? - Why is this OpenMP program giving me different answers every time? 为什么局部变量的地址每次都不一样? - Why the addresses of local variables can be different every time? 每次我使用 fopen 时调试断言表达式流!=null - debug assertion expression stream!=null every time i use fopen
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM