[英]Missing “;” before “type” with pointers in C
我是C語言的新手。我正在在線上這堂課,很難獲得幫助。 我知道這個問題已經被問到並回答了我的惡心,但我似乎無法弄清楚該函數中需要聲明什么。
編輯:我正在使用Visual Studio 2008。
此處的功能旨在使整個字符串變為小寫並計算所述字符串中的單詞
錯誤發生在main函數調用中,但我認為真正的問題出在函數本身上。
例子:
char lowercase( char *stringToLower)
{
int i; //local counter i
for (i = 0; i < 100; i++)
stringToLower[i] = toupper(stringToLower[i]);
return i;
}// end make LOWERcase
還有這個
void countWordsLower( char *stringToLower)
{
// define local variables
char *tokenPtr;
int i;
int counter = 0;
printf( "\nThe string to be tokenized is:\n");
puts( stringToLower );
tokenPtr = strtok( stringToLower, " \n");
// tokenize until tokenPtr becomes NULL
while ( tokenPtr )
{
++counter;
//increment counter
tokenPtr = strtok( NULL, " \n" );
// gets next token
} // end while
printf( "\nThe total number of words is %d\n\n", counter );
} // end countWords LOWER
這些是他們的原型
void countWordsLower ( char *stringToLower );
char lowercase ( char *stringToLower );
上下文中的整個程序如下:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
// Function prototypes
void concatenate ( char *stringFirst, char *stringSecond );
char lowercase ( char *stringToLower );
char uppercase ( char *stringToUpper );
void countWordsLower ( char *stringToLower );
void countWordsUpper ( char *stringToUpper );
void twoStrings ( char *stringFirst, char *stringSecond );
int main (void)
{
// initialize variables
int menuOption; // user choice
int tokens; // holds return value from tokenize
char stringToLower [100]; // 99 character string
char stringToUpper [100]; // 99 character string
char stringFirst [100]; //first string able to hold 99 chars
char stringSecond [100];//second string able to hold 99 chars
do
{
printf ( "\n\nWelcome!\n" );
printf ( "\nEnter 1 for Concatenate Two Strings\n" );
printf ( "\nEnter 2 for Change to LOWER Case with Word Count" );
printf ( "\nEnter 3 for Change to UPPER Case with Word Count" );
printf ( "\nEnter 4 to Terminate program" );
printf ( "\n\nPlease enter your selection: " );
scanf( "%d", &menuOption );
// list menu options
switch (menuOption)
{
case 1:
//Concatenate Two Strings
printf( "\nThis function will concatenate two strings into a third\n");
printf( "Enter first string:\n\n\t");
gets( stringFirst );
printf( "Enter second string:\n\n\t" );
gets( stringSecond );
//function to concatenate
twoStrings( stringFirst, stringSecond );
break;
//end choice one
case 2:
//Change to LOWER Case with Word Count
printf( "\nThis function will change an entire string to LOWER case\n");
printf( "\nand count the number of words!\n");
printf( "Enter string:\n\n\t");
gets( stringToLower );
//function to change to lowercase
lowercase( char &stringToLower );
//function to count number of words
countWordsLower( char &stringToLower );
break;
case 3:
//Change to UPPER Case with Word Count
printf( "\nThis function will change an entire string to UPPER case\n");
printf( "\nand count the number of words!\n");
printf( "Enter string:\n\n\t");
gets( stringToUpper );
//function to change to uppercase
uppercase( char &stringToUpper);
//function to count number of words
countWordsUpper( char &stringToUpper);
break;
case 4:
//End Program
printf( "\nThank You!\n Come Again!\n\n" );
break;
default:
printf ( "\nWhoops! Try a number 1,2,3 or 4!\n" );
}//end switch
}// end do
while (menuOption != 4);
}// end main
void concatenate ( char *stringFirst, char *stringSecond)
{
char stringThird[200];
printf( "\nString ois %s\nString two is %s\n", stringFirst, stringSecond );
// concatenate stringFirst to stringSecond with space
strcpy(stringThird, stringFirst);
strcat(stringThird, " ");
strcat(stringThird, stringSecond);
printf( "\nTogether ... %s\n\n", stringThird );
} // end concatenate
//function to make LOWERcase
char lowercase( char *stringToLower)
{
int i; //local counter i
for (i = 0; i < 100; i++)
stringToLower[i] = toupper(stringToLower[i]);
return i;
}// end make LOWERcase
// function to count number of words
// in string that was made LOWERcase
void countWordsLower( char *stringToLower)
{
// define local variables
char *tokenPtr;
int i;
int counter = 0;
printf( "\nThe string to be tokenized is:\n");
puts( stringToLower );
tokenPtr = strtok( stringToLower, " \n");
// tokenize until tokenPtr becomes NULL
while ( tokenPtr )
{
++counter;
//increment counter
tokenPtr = strtok( NULL, " \n" );
// gets next token
} // end while
printf( "\nThe total number of words is %d\n\n", counter );
} // end countWords LOWER
//function to make UPPERcase
char uppercase( char *stringToUpper )
{
int i; //local counter i
for (i = 0; i < 100; i++)
stringToUpper[i] = toupper(stringToUpper[i]);
return i;
}// end make UPPERcase
// function to count number of words
// in string that was made UPPERcase
void countWordsUpper( char *stringToUpper )
{
// define local variables
char *tokenPtr;
int i;
int counter = 0;
printf( "\nThe string to be tokenized is:\n");
puts( stringToUpper );
tokenPtr = strtok( stringToUpper, " \n");
// tokenize until tokenPtr becomes NULL
while ( tokenPtr )
{
++counter;
//increment counter
tokenPtr = strtok( NULL, " \n" );
// gets next token
} // end while
printf( "\nThe total number of words is %d\n\n", counter );
} // end countWords UPPER
以下是在一個C編譯器中編譯以上代碼所導致的錯誤:
main.c:58:38: error: expected ')'
char lowercase( char &stringToLower );
^
main.c:58:31: note: to match this '('
char lowercase( char &stringToLower );
^
main.c:58:22: error: conflicting types for 'lowercase'
char lowercase( char &stringToLower );
^
main.c:7:6: note: previous declaration is here
char lowercase ( char *stringToLower );
^
main.c:61:44: error: expected ')'
void countWordsLower( char &stringToLower );
^
main.c:61:37: note: to match this '('
void countWordsLower( char &stringToLower );
^
main.c:61:22: error: conflicting types for 'countWordsLower'
void countWordsLower( char &stringToLower );
^
main.c:9:6: note: previous declaration is here
void countWordsLower ( char *stringToLower );
^
main.c:73:38: error: expected ')'
char uppercase( char &stringToUpper);
^
main.c:73:31: note: to match this '('
char uppercase( char &stringToUpper);
^
main.c:73:22: error: conflicting types for 'uppercase'
char uppercase( char &stringToUpper);
^
main.c:8:6: note: previous declaration is here
char uppercase ( char *stringToUpper );
^
main.c:76:44: error: expected ')'
void countWordsUpper( char &stringToUpper);
^
main.c:76:37: note: to match this '('
void countWordsUpper( char &stringToUpper);
^
main.c:76:22: error: conflicting types for 'countWordsUpper'
void countWordsUpper( char &stringToUpper);
^
main.c:10:6: note: previous declaration is here
void countWordsUpper ( char *stringToUpper );
^
改變...再次
//function to change to lowercase
lowercase( stringToLower );
//function to count number of words
countWordsLower( stringToLower );
對於任何仍在關注最終代碼的人,因為我最終將其固定為
#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS
#endif
#include <stdio.h>
#include <string.h>
#include <ctype.h>
// Function prototypes
void concatenate ( char *stringFirst, char *stringSecond );
char lowercase ( char *stringToLower );
char uppercase ( char *stringToUpper );
void countWordsLower ( char *stringToLower );
void countWordsUpper ( char *stringToUpper );
int main (void)
{
// initialize variables
int menuOption; // user choice
int tokens = 0; // holds return value from tokenize
char stringToLower [100]; // 99 character string
char stringToUpper [100]; // 99 character string
char stringFirst [100]; //first string able to hold 99 chars
char stringSecond [100];//second string able to hold 99 chars
do
{
printf ( "\n\nWelcome!\n" );
printf ( "\n""*Concatenate*""" );
printf ( "\nEnter 1 for Concatenate Two Strings\n" );
printf ( "\n""*Change Case*""" );
printf ( "\nEnter 2 for Change to LOWER Case with Word Count" );
printf ( "\nEnter 3 for Change to UPPER Case with Word Count\n" );
printf ( "\n""*Exit*""" );
printf ( "\nEnter 4 to Terminate program" );
printf ( "\n\nPlease enter your selection: " );
scanf( "%d", &menuOption );
_flushall();
// list menu options
switch (menuOption)
{
case 1:
//Concatenate Two Strings
printf( "\nThis function will concatenate two strings into a third\n");
printf( "\nEnter first string: ");
fgets( stringFirst, 99, stdin );
strtok( stringFirst, "\n" );
printf( "\nEnter second string: " );
strtok( stringSecond, "\n" );
fgets( stringSecond, 99, stdin );
//function to concatenate
concatenate ( stringFirst, stringSecond );
break;
//end choice one
case 2:
//Change to LOWER Case with Word Count
printf( "\nThis function will change an entire string to LOWER case\n");
printf( "and count the number of words!\n");
printf( "\nEnter string: ");
fgets( stringToLower, 99, stdin );
printf( "\nThe original string is: " );
printf( "%s", stringToLower );
//function to change to lowercase
lowercase( stringToLower);
//function to count number of words
countWordsLower( stringToLower);
break;
case 3:
//Change to UPPER Case with Word Count
printf( "\nThis function will change an entire string to UPPER case\n");
printf( "and count the number of words!\n");
printf( "\nEnter string: ");
fgets( stringToUpper, 99, stdin );
printf( "\nThe original string is: " );
printf( "%s", stringToUpper );
//function to change to uppercase
uppercase( stringToUpper );
//function to count number of words
countWordsUpper( stringToUpper );
break;
case 4:
//End Program
printf( "\nThank You!\n Come Again!\n\n" );
break;
default:
printf ( "\nWhoops! Try a number 1,2,3 or 4!\n" );
}//end switch
}// end do
while (menuOption != 4);
}// end main
void concatenate ( char *stringFirst, char *stringSecond )
{
char stringThird[200];
printf( "\n\nString One is: %s\nString Two is: %s\n", stringFirst, stringSecond );
// concatenate stringFirst to stringSecond with space
strcpy(stringThird, stringFirst);
strcat(stringThird, " / ");
strcat(stringThird, stringSecond);
printf( "Together they read: %s", stringThird );
} // end concatenate
//function to make LOWERcase
char lowercase( char *stringToLower )
{
int i; //local counter i
for (i = 0; i < 100; i++)
stringToLower[i] = tolower(stringToLower[i]);
return i;
}// end make LOWERcase
// function to count number of words
// in string that was made LOWERcase
void countWordsLower( char *stringToLower )
{
// define local variables
char *tokenPtr;
int i = 0;
int counter = 0;
printf( "\nThe adjusted string is: ");
puts( stringToLower );
tokenPtr = strtok( stringToLower, " \n");
// tokenize until tokenPtr becomes NULL
while ( tokenPtr )
{
++counter;
//increment counter
tokenPtr = strtok( NULL, " \n" );
// gets next token
} // end while
printf( "The total number of words is %d\n", counter );
} // end countWords LOWER
//function to make UPPERcase
char uppercase( char *stringToUpper )
{
int i = 0; //local counter i
for (i = 0; i < 100; i++)
stringToUpper[i] = toupper(stringToUpper[i]);
return i;
}// end make UPPERcase
// function to count number of words
// in string that was made UPPERcase
void countWordsUpper( char *stringToUpper )
{
// define local variables
char *tokenPtr;
int i = 0;
int counter = 0;
printf( "\nThe adjusted string is: ");
puts( stringToUpper );
tokenPtr = strtok( stringToUpper, " \n");
// tokenize until tokenPtr becomes NULL
while ( tokenPtr )
{
++counter;
//increment counter
tokenPtr = strtok( NULL, " \n" );
// gets next token
} // end while
printf( "The total number of words is %d\n", counter );
} // end countWords UPPER
順便說一句,感謝您的所有答復和支持!
您(不是很)在main的中間聲明函數,而不是調用它們。 例如,當您說:
char lowercase( char &stringToLower );
//function to count number of words
void countWordsLower( char &stringToLower );
這些是(無效的)函數原型。 你想說:
lowercase( stringToLower );
countWordsLower( stringToLower );
在此代碼段中,您有錯:
//function to change to uppercase
char uppercase( char &stringToUpper);
//function to count number of words
void countWordsUpper( char &stringToUpper);
break;
調用函數時, 既不包含返回類型,也不包含參數的類型。 (那些僅在聲明和定義函數時使用,而在使用它們時不使用)
代碼應為:
//function to change to uppercase
uppercase(stringToUpper);
//function to count number of words
countWordsUpper(stringToUpper);
break;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.