string help/errors

I'm working on a project for my programming class and I cannot figure out where I went wrong. The project is to write a program that demonstrates how strcpy, strncpy, strcmp, strncmp, strcat, strncat, and strlen work. We could find example programs and explanations online. I used many from this website. My problem is I get errors on lines 68, 80, 93, 106, 111, 122, 126, 133, 143, and 150. Even help with one error would be fine. Thank you.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#include <cstdlib>
#include <iostream>
#include <string.h>

using namespace std;

void strcpy(void);
void strncpy(void);
void strcmp(void);
void strncmp(void);
void strcat(void);
void strncat(void);
void strlen(void);

int main()
{
    int w=1, choice;
    do
    {
        cout<<"You have entered the string demostration program."<<endl;
        cout<<"Please enter 1 for strcpy, 2 for strncpy, 3 for strcmp, 4 for"<<endl<<"strncmp, 5 for strcat, 6 for strncat, or 7 for strlen>> ";
        cin>>choice;
        switch(choice)
        {
                      case 1:
                           {
                                  strcpy();
                              }
                      case 2:
                           {
                                  strncpy();
                              }
                      case 3:
                           {
                                  strcmp();
                              }
                      case 4:
                           {
                                  strncmp();
                              }
                      case 5:
                           {
                                  strcat();
                              }
                      case 6:
                           {
                                  strncat();
                              }
                      case 7:
                           {
                                  strlen();
                              }
                      default:
                              {
                                  cout<<"Please enter 1, 2, 3, 4, 5, 6, or 7."<<endl;
                                  }
               do
               {
                 cout<<"Enter 1 to continue or 2 to quit";
                 cin>>w;
               }while((w!=1)&&(w!=2));
	}while((w==1)||(w!=2));
    
    system("PAUSE");
    return EXIT_SUCCESS;
}

void strcpy(void)
{
     char str1[]="Sample string";
     char str2[40];
     char str3[40];
     cout<<"This function gives an example of strcpy."<<endl;
     cout<<"strcpy puts characters and integers into arrays in the iostream"<<endl;
     strcpy (str2,str1);
     strcpy (str3,"copy successful");
     printf ("str1: %s\nstr2: %s\nstr3: %s\n",str1,str2,str3);
}
     
void strncpy(void)
{
     char str1[]= "To be or not to be";
     char str2[40];
     char str3[40];
     cout<<"This function gives an example of strncpy."<<endl;
     cout<<"strncpy takes the first number for the source(array)and pushes it"<<endl<<"to the destination. If it finds a null character before a number"<<endl<<"it will copy a zero."<<endl;
     strncpy ( str2, str1, sizeof(str2) );
     strncpy ( str3, str2, 5 );
     str3[5] = '\0'; 
     puts (str1);
     puts (str2);
     }
void strcmp(void)
{
     char szKey[] = "apple\n";
     char szInput[80];
     cout<<"This function gives an example of strcmp."<<endl;
     cout<<"strcmp compares the first character of each string. If they are "<<endl<<"equal to each other, it continues with the following pairs until"<<endl<<"the characters differ or until a terminating null-character is found"<<endl;
     do
     {
     printf ("Guess my favorite fruit? ");
     fgets (szInput,80,stdin);
     } while (strcmp (szKey,szInput) != 0);
     cout<<"Correct."
     }
void strncmp(void)
{
     char str[][5] = { "R2D2" , "C3PO" , "R2A6" };
     int n;
     cout<<"This function gives an example of strncmp."<<endl;
     cout<<"strncmp compares the first character of each string. If they are"<<endl<<"equal to each other, it continues with the following pairs until"<<endl<<"the characters differ, until a terminating null-character is "<<endl<<"reached, or until num characters match in both strings, whichever happens first.
      
       cout<<"Looking for R2 astromech droids..."
       for (n=0 ; n<3 ; n++)
       {
        if (strncmp (str[n],"R2xx",2) == 0)
        {
          printf ("found %s\n",str[n]);
          }
          }
          }
void strcat(void)
{
     char str[80];
     cout<<"This function gives an example of strcat."<<endl;
     cout<<"strcat adds a copy of the source to the destination. If the source"<<endl<<"was  and the destination was  after strcat the "<<endl<<"destination would read  with a terminating null character.
     strcpy (str,"these ");
     strcat (str,"strings ");
     strcat (str,"are ");
     strcat (str,"concatenated.");
     puts (str);
      }
void strncat(void)
{
     char str1[20];
     char str2[20];
     cout<<"This function gives an example of strncat."<<endl;
     strcpy (str1,"To be ");
     strcpy (str2,"or not to be");
     strncat (str1, str2, 6);
     puts (str1);
     }
void strlen(void)
{
     char szInput[256];
     cout<<"This function gives an example of strlen."<<endl;
     cout<<"strlen determines the length of a string by counting from the"<<endl<<"forst character to the terminating null character (count does not "<<endl<<"include the terminating character."<<endl;
     printf ("Enter a sentence: ");
     gets (szInput);
     printf ("The sentence entered is %u characters long.\n",(unsigned)strlen(szInput));
     }
     }

Last edited on
It seems to be the fact that you are trying to make your own functions with names of already existing functions. Try changing the names of your functions.
I changes the names of the functions and I'm still having the same issues.
Post your changed code please. And the errors you are getting.
Last edited on
cout<<"strncmp compares the first character of each string. If they are"<<endl<<"equal to each other, it continues with the following pairs until"<<endl<<"the characters differ, until a terminating null-character is "<<endl<<"reached, or until num characters match in both strings, whichever happens first.
This line needs a semicolon. It also needs a closing "

cout<<"Looking for R2 astromech droids..." This line needs a semicolon.
Last edited on
#include <cstdlib>
#include <iostream>
#include <string.h>

using namespace std;

void function1(void);
void function2(void);
void function3(void);
void function4(void);
void function5(void);
void function6(void);
void function7(void);

int main()
{
int w=1, choice;
do
{
cout<<"You have entered the string demostration program."<<endl;
cout<<"Please enter 1 for strcpy, 2 for strncpy, 3 for strcmp, 4 for"<<endl<<"strncmp, 5 for strcat, 6 for strncat, or 7 for strlen>> ";
cin>>choice;
switch(choice)
{
case 1:
{
function1();
}
case 2:
{
function2();
}
case 3:
{
function3();
}
case 4:
{
function4();
}
case 5:
{
function5();
}
case 6:
{
function6();
}
case 7:
{
function7();
}
default:
{
cout<<"Please enter 1, 2, 3, 4, 5, 6, or 7."<<endl;
}
do
{
cout<<"Enter 1 to continue or 2 to quit";
cin>>w;
}while((w!=1)&&(w!=2));
}while((w==1)||(w!=2));

system("PAUSE");
return EXIT_SUCCESS;
}

void function1(void)
{
char str1[]="Sample string";
char str2[40];
char str3[40];
cout<<"This function gives an example of strcpy."<<endl;
cout<<"strcpy puts characters and integers into arrays in the iostream"<<endl;
strcpy (str2,str1);
strcpy (str3,"copy successful");
printf ("str1: %s\nstr2: %s\nstr3: %s\n",str1,str2,str3);
}

void function2(void)
{
char str1[]= "To be or not to be";
char str2[40];
char str3[40];
cout<<"This function gives an example of strncpy."<<endl;
cout<<"strncpy takes the first number for the source(array)and pushes it"<<endl<<"to the destination. If it finds a null character before a number"<<endl<<"it will copy a zero."<<endl;
strncpy ( str2, str1, sizeof(str2) );
strncpy ( str3, str2, 5 );
str3[5] = '\0';
puts (str1);
puts (str2);
}
void function3(void)
{
char szKey[] = "apple\n";
char szInput[80];
cout<<"This function gives an example of strcmp."<<endl;
cout<<"strcmp compares the first character of each string. If they are "<<endl<<"equal to each other, it continues with the following pairs until"<<endl<<"the characters differ or until a terminating null-character is found"<<endl;
do
{
printf ("Guess my favorite fruit? ");
fgets (szInput,80,stdin);
} while (strcmp (szKey,szInput) != 0);
cout<<"Correct."
}
void function4(void)
{
char str[][5] = { "R2D2" , "C3PO" , "R2A6" };
int n;
cout<<"This function gives an example of strncmp."<<endl;
cout<<"strncmp compares the first character of each string. If they are"<<endl<<"equal to each other, it continues with the following pairs until"<<endl<<"the characters differ, until a terminating null-character is "<<endl<<"reached, or until num characters match in both strings, whichever happens first.

cout<<"Looking for R2 astromech droids..."
for (n=0 ; n<3 ; n++)
{
if (strncmp (str[n],"R2xx",2) == 0)
{
printf ("found %s\n",str[n]);
}
}
}
void function5(void)
{
char str[80];
cout<<"This function gives an example of strcat."<<endl;
cout<<"strcat adds a copy of the source to the destination. If the source"<<endl<<"was zanni and the destination was erin after strcat the "<<endl<<"destination would read erinzanni with a terminating null character."<<endl;
strcpy (str,"these ");
strcat (str,"strings ");
strcat (str,"are ");
strcat (str,"concatenated.");
puts (str);
}
void function6(void)
{
char str1[20];
char str2[20];
cout<<"This function gives an example of strncat."<<endl;
strcpy (str1,"To be ");
strcpy (str2,"or not to be");
strncat (str1, str2, 6);
puts (str1);
}
void function7(void);
{
char szInput[256];
cout<<"This function gives an example of strlen."<<endl;
cout<<"strlen determines the length of a string by counting from the"<<endl<<"forst character to the terminating null character (count does not "<<endl<<"include the terminating character."<<endl;
printf ("Enter a sentence: ");
gets (szInput);
printf ("The sentence entered is %u characters long.\n",(unsigned)strlen(szInput));
}
}




Errors:

C:In function `int main()':

68 C: expected `while' before "function1"

68 C: expected `(' before "function1"

68 C:expected primary-expression before "void"

68 C:expected primary-expression before "void"

68 C:expected primary-expression before "void"

80 C:expected `;' before "void"

80 C:expected primary-expression before "void"

93 C:expected primary-expression before "void"

93 C:expected `;' before "void"

106 C:expected primary-expression before "void"

106 C:expected `;' before "void"

111 C:missing terminating " character

122 C:expected primary-expression before "void"

122 C:expected `;' before "void"

133 C:expected primary-expression before "void"

133 C:expected `;' before "void"
added codeblocks

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#include <cstdlib>
#include <iostream>
#include <string.h>

using namespace std;

void function1(void);
void function2(void);
void function3(void);
void function4(void);
void function5(void);
void function6(void);
void function7(void);

int main()
{
int w=1, choice;
do
{
cout<<"You have entered the string demostration program."<<endl;
cout<<"Please enter 1 for strcpy, 2 for strncpy, 3 for strcmp, 4 for"<<endl<<"strncmp, 5 for strcat, 6 for strncat, or 7 for strlen>> ";
cin>>choice;
switch(choice)
{
case 1:
{
function1();
}
case 2:
{
function2();
}
case 3:
{
function3();
}
case 4:
{
function4();
}
case 5:
{
function5();
}
case 6:
{
function6();
}
case 7:
{
function7();
}
default:
{
cout<<"Please enter 1, 2, 3, 4, 5, 6, or 7."<<endl;
}
do
{
cout<<"Enter 1 to continue or 2 to quit";
cin>>w;
}while((w!=1)&&(w!=2));
}while((w==1)||(w!=2));

system("PAUSE");
return EXIT_SUCCESS;
}

void function1(void)
{
char str1[]="Sample string";
char str2[40];
char str3[40];
cout<<"This function gives an example of strcpy."<<endl;
cout<<"strcpy puts characters and integers into arrays in the iostream"<<endl;
strcpy (str2,str1);
strcpy (str3,"copy successful");
printf ("str1: %s\nstr2: %s\nstr3: %s\n",str1,str2,str3);
}

void function2(void)
{
char str1[]= "To be or not to be";
char str2[40];
char str3[40];
cout<<"This function gives an example of strncpy."<<endl;
cout<<"strncpy takes the first number for the source(array)and pushes it"<<endl<<"to the destination. If it finds a null character before a number"<<endl<<"it will copy a zero."<<endl;
strncpy ( str2, str1, sizeof(str2) );
strncpy ( str3, str2, 5 );
str3[5] = '\0';
puts (str1);
puts (str2);
}
void function3(void)
{
char szKey[] = "apple\n";
char szInput[80];
cout<<"This function gives an example of strcmp."<<endl;
cout<<"strcmp compares the first character of each string. If they are "<<endl<<"equal to each other, it continues with the following pairs until"<<endl<<"the characters differ or until a terminating null-character is found"<<endl;
do
{
printf ("Guess my favorite fruit? ");
fgets (szInput,80,stdin);
} while (strcmp (szKey,szInput) != 0);
cout<<"Correct."
}
void function4(void)
{
char str[][5] = { "R2D2" , "C3PO" , "R2A6" };
int n;
cout<<"This function gives an example of strncmp."<<endl;
cout<<"strncmp compares the first character of each string. If they are"<<endl<<"equal to each other, it continues with the following pairs until"<<endl<<"the characters differ, until a terminating null-character is "<<endl<<"reached, or until num characters match in both strings, whichever happens first.

cout<<"Looking for R2 astromech droids..."
for (n=0 ; n<3 ; n++)
{
if (strncmp (str[n],"R2xx",2) == 0)
{
printf ("found %s\n",str[n]);
}
}
}
void function5(void)
{
char str[80];
cout<<"This function gives an example of strcat."<<endl;
cout<<"strcat adds a copy of the source to the destination. If the source"<<endl<<"was zanni and the destination was erin after strcat the "<<endl<<"destination would read erinzanni with a terminating null character."<<endl;
strcpy (str,"these ");
strcat (str,"strings ");
strcat (str,"are ");
strcat (str,"concatenated.");
puts (str);
}
void function6(void)
{
char str1[20];
char str2[20];
cout<<"This function gives an example of strncat."<<endl;
strcpy (str1,"To be ");
strcpy (str2,"or not to be");
strncat (str1, str2, 6);
puts (str1);
}
void function7(void);
{
char szInput[256];
cout<<"This function gives an example of strlen."<<endl;
cout<<"strlen determines the length of a string by counting from the"<<endl<<"forst character to the terminating null character (count does not "<<endl<<"include the terminating character."<<endl;
printf ("Enter a sentence: ");
gets (szInput);
printf ("The sentence entered is %u characters long.\n",(unsigned)strlen(szInput));
}
} 


Errors:

C:In function `int main()':

68 C: expected `while' before "function1"

68 C: expected `(' before "function1"

68 C:expected primary-expression before "void"

68 C:expected primary-expression before "void"

68 C:expected primary-expression before "void"

80 C:expected `;' before "void"

80 C:expected primary-expression before "void"

93 C:expected primary-expression before "void"

93 C:expected `;' before "void"

106 C:expected primary-expression before "void"

106 C:expected `;' before "void"

111 C:missing terminating " character

122 C:expected primary-expression before "void"

122 C:expected `;' before "void"

133 C:expected primary-expression before "void"

133 C:expected `;' before "void"
Ok thanks
Looks like you're missing a closing } for the switch statement?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#include <cstdlib>
#include <iostream>
#include <string.h>

using namespace std;

void function1(void);
void function2(void);
void function3(void);
void function4(void);
void function5(void);
void function6(void);
void function7(void);

int main()
{
	int w = 1, choice;
	do
	{
		cout << "You have entered the string demostration program." << endl;
		cout << "Please enter 1 for strcpy, 2 for strncpy, 3 for strcmp, 4 for" << endl << "strncmp, 5 for strcat, 6 for strncat, or 7 for strlen>> ";
		cin >> choice;
		switch (choice)
		{
			case 1:
			{
					  function1();
			}
			case 2:
			{
					  function2();
			}
			case 3:
			{
					  function3();
			}
			case 4:
			{
					  function4();
			}
			case 5:
			{
					  function5();
			}
			case 6:
			{
					  function6();
			}
			case 7:
			{
					  function7();
			}
			default:
			{
					   cout << "Please enter 1, 2, 3, 4, 5, 6, or 7." << endl;
			}
			do
			{
				cout << "Enter 1 to continue or 2 to quit";
				cin >> w;
			}
			while ((w != 1) && (w != 2));
		} // <--you were mismatched on your brackets and the while was attached to the switch block
	}
	while ((w == 1) || (w != 2));
		system("PAUSE");
		return EXIT_SUCCESS;
	}

	void function1(void)
	{
		char str1[] = "Sample string";
		char str2[40];
		char str3[40];
		cout << "This function gives an example of strcpy." << endl;
		cout << "strcpy puts characters and integers into arrays in the iostream" << endl;
		strcpy(str2, str1);
		strcpy(str3, "copy successful");
		printf("str1: %s\nstr2: %s\nstr3: %s\n", str1, str2, str3);
	}

	void function2(void)
	{
		char str1[] = "To be or not to be";
		char str2[40];
		char str3[40];
		cout << "This function gives an example of strncpy." << endl;
		cout << "strncpy takes the first number for the source(array)and pushes it" << endl << "to the destination. If it finds a null character before a number" << endl << "it will copy a zero." << endl;
		strncpy(str2, str1, sizeof(str2));
		strncpy(str3, str2, 5);
		str3[5] = '\0';
		puts(str1);
		puts(str2);
	}
	void function3(void)
	{
		char szKey[] = "apple\n";
		char szInput[80];
		cout << "This function gives an example of strcmp." << endl;
		cout << "strcmp compares the first character of each string. If they are " << endl << "equal to each other, it continues with the following pairs until" << endl << "the characters differ or until a terminating null-character is found" << endl;
		do
		{
			printf("Guess my favorite fruit? ");
			fgets(szInput, 80, stdin);
		}
		while (strcmp(szKey, szInput) != 0);
		cout << "Correct."; // <-- added semicolon
	}
	void function4(void)
	{
		char str[][5] = {"R2D2", "C3PO", "R2A6"};
		int n;
		cout << "This function gives an example of strncmp." << endl;
		cout << "strncmp compares the first character of each string. If they are" << endl << "equal to each other, it continues with the following pairs until" << endl << "the characters differ, until a terminating null-character is " << endl << "reached, or until num characters match in both strings, whichever happens first.";

		cout << "Looking for R2 astromech droids...";
		for (n = 0; n<3; n++)
		{
			if (strncmp(str[n], "R2xx", 2) == 0)
			{
				printf("found %s\n", str[n]);
			}
		}
	}

	void function5(void)
	{
		char str[80];
		cout << "This function gives an example of strcat." << endl;
		cout << "strcat adds a copy of the source to the destination. If the source" << endl << "was zanni and the destination was erin after strcat the " << endl << "destination would read erinzanni with a terminating null character." << endl;
		strcpy(str, "these ");
		strcat(str, "strings ");
		strcat(str, "are ");
		strcat(str, "concatenated.");
		puts(str);
	}

	void function6(void)
	{
		char str1[20];
		char str2[20];
		cout << "This function gives an example of strncat." << endl;
		strcpy(str1, "To be ");
		strcpy(str2, "or not to be");
		strncat(str1, str2, 6);
		puts(str1);
	}

	void function7(void) // <--removed semicolon
	{
		char szInput[256];
		cout << "This function gives an example of strlen." << endl;
		cout << "strlen determines the length of a string by counting from the" << endl << "forst character to the terminating null character (count does not " << endl << "include the terminating character." << endl;
		printf("Enter a sentence: ");
		gets(szInput);
		printf("The sentence entered is %u characters long.\n", (unsigned) strlen(szInput));
	}
 // <-- removed closing brace as it was mismatched 


take note of comments and get yourself a IDE with syntax highlighting
Topic archived. No new replies allowed.