can anyone help me with this error?

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
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include<cstdio>
#include <stdint.h>
#define length 20
#include <iostream>
using namespace std;
int main()
{
     
    char s[length];
    char adad[10];
    char alamat[10];
    int dec;
 
    cout << "Enter a string" << endl;
 
    //read in the users data from the input stream and ends by enter
 
    cin.get(s,'\n');
 
 
    //Display a message made up of two strings and the user data
    cout << "ha ha! you entered the string "  << s << endl;
 
     
     
    /*************initializing arrays************/
 
    for (int j=0 ; j<length ; j++)
	{
        if (ispunct(s[j]))
		{  
            for (int k=0;k<10;k++)
                adad[k]=s[j];
        }
        else 
		{
            for (int l=0;l<10;l++)
                alamat[l]=s[j];
        }
    }
 
    /**********preview arrays*************/
 
    for (int m=0;m<10; m++)
        printf("%c" , adad[m]);
    for (int n=0; n<10; n++)
        printf("%c" , alamat[n]);
 
    scanf("%d" , dec);
    printf("finally %d" , dec);
 
 
    return 0;
}

the error is error C2334: unexpected token(s) preceding '{'; skipping apparent function body
1
2
#define length 20
#include <iostream> 


This macro is screwing you.

iostream likely uses the identifier "length" for many other things. Possibly function and/or variable names. By defining a macro you are unwittingly replacing everything in iostream.

Do not use #define to declare constants

The solution here is to get rid of the define and make it a constant:

1
2
//#define length 20 // bad
constr int length = 20; // good 
Why on earth do you use scanf and printf? If it's your choice then I pass but if it's not consider other ways.

How old is your compiler anyway? You are using string.h for example. Why? Headers in C++ don't use .h extension. Check it out. You also include many unnecessary header files. Like #include <stdio.h> and also #include<cstdio> . These are the same thing. The first is used in C the second in C++.

Your line 52 won't work. Use
scanf("%d" , &dec);

When you use const defined in preprocessor declare them with capital letters. There is probable another length issue in your system.
Use #define LENGTH 20 instead
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
// Length210120120048.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include<cstdio>
//#include <stdint.h>
#include <iostream>

#define LENGHT 20

using namespace std;


int _tmain(int argc, _TCHAR* argv[])
{
	  char s[LENGHT];
    char adad[10];
    char alamat[10];
    int dec = 0 ;
 
    cout << "Enter a string" << endl;
 
    //read in the users data from the input stream and ends by enter
 
    cin.getline(s,'\n');
 
 
    //Display a message made up of two strings and the user data
    cout << "ha ha! you entered the string "  << s << endl;
 
     
     
    /*************initializing arrays************/
 
    for (int j=0 ; j<LENGHT ; j++)
	{
        if (ispunct(s[j]))
		{  
            for (int k=0;k<10;k++)
                adad[k]=s[j];
        }
        else 
		{
            for (int l=0;l<10;l++)
                alamat[l]=s[j];
        }
    }
 
    /**********preview arrays*************/
 
    for (int m=0;m<10; m++)
    cout <<adad[m];
    for (int n=0; n<10; n++)
        cout<<alamat[n];
 
   cin>>dec;
   cout<<"\nfinally " <<dec;
	return 0;
}
eypros wrote:
Use #define LENGTH 20 instead


Just to reiterate, I recommend that you don't #define ANYTHING.

#define is an extremely poor choice for declaring constants. Use the const keyword, that's what it's there for.
Last edited on
Topic archived. No new replies allowed.