[英]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.