Reputation: 31
I am a total novice in C. I am taking this class online and it is very difficult to get help. I know this question has been asked and answered ad nauseam, but I can't seem to figure out what needs to be declared in the function.
Edit: I am using Visual Studio 2008.
The functions here are designed to make an entire string lower case and count the words in said string
The errors occur at the function call in main but I believe the real problem is in the functions themselves.
Examples:
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
and this one as well
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
these are their prototypes
void countWordsLower ( char *stringToLower );
char lowercase ( char *stringToLower );
The entire program in context is the following:
#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
Here are the errors that result from compiling the above code in one C compiler:
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 );
^
changed...again
//function to change to lowercase
lowercase( stringToLower );
//function to count number of words
countWordsLower( stringToLower );
for anyone still paying attention the final code as I finally fixed it is this
#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
by the way, thank you for all the answers and support!
Upvotes: 0
Views: 127
Reputation: 36438
You're (not-quite-)declaring functions in the middle of main, instead of calling them. For instance, when you say:
char lowercase( char &stringToLower );
//function to count number of words
void countWordsLower( char &stringToLower );
Those are (invalid) function prototypes. You want to say:
lowercase( stringToLower );
countWordsLower( stringToLower );
Upvotes: 1
Reputation: 64682
In this code snippet, you have it wrong:
//function to change to uppercase
char uppercase( char &stringToUpper);
//function to count number of words
void countWordsUpper( char &stringToUpper);
break;
When you call a function, you do not include the return type, nor the types of the parameters. (those are only used when declaring and defining functions, not when using them)
The code should be:
//function to change to uppercase
uppercase(stringToUpper);
//function to count number of words
countWordsUpper(stringToUpper);
break;
Upvotes: 0