【发布时间】:2019-01-04 16:21:00
【问题描述】:
作为一个编程新手,我犯了很多错误,但我无法理解完美的指针。我写了一个代码,在函数 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" );
}
我希望我的 Modify 函数能够打印我之前使用 Add 函数添加的数据。而不是每次打印我时都这样。
【问题讨论】:
标签: c arrays function pointers