for loop problem with compiler

Apr 29, 2009 at 12:30pm
hi,

ive written string reverse program, which looks like this - just an example for my problem:

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
#include <iostream>
using namespace std;

int main()
{
	char s[256];
	cout << "Enter a string: ";
	cin >> s;         

	cout << "Reversed string = ";

        int length = strlen(s);

        int cnt1 = 0;
	for ( int cnt2 = (length - 1); cnt2 > (length - 1)/2 ; cnt2--)  
	{
		int temp = s[cnt1];    
		s[cnt1] = s[cnt2];     
		s[cnt2] = temp;        

		cnt1++;
	}

	for(int i = 0; i < length; ++i)
              cout << s[i];
	cout << endl;
}


but if i replace int cnt1 = 0; and cnt1++; with:

 
for ( int cnt1 = 0, int cnt2 = (length - 1); cnt2 > (length - 1)/2 ; cnt1++, cnt2--)


i get these errors:
- error C2062: type 'int' unexpected
- error C2065: 'cnt2' : undeclared identifier
- error C2065: 'cnt2' : undeclared identifier
- error C2143: syntax error : missing ';' before ')'
- error C2143: syntax error : missing ';' before ')'
- error C2143: syntax error : missing ';' before '{'
- error C2065: 'cnt2' : undeclared identifier
- error C2065: 'cnt2' : undeclared identifier


but this is just an example, it does similer with any other for loop, if there is more then one argument specified in the 1. and 3. argument "space" of a for loop

ty in advance!
Apr 29, 2009 at 12:46pm
It's for ( int cnt1 = 0, cnt2 = (length - 1); //... .
Apr 29, 2009 at 12:47pm
The problem is this int code1, int code2 ; In C/C++ if you want to declare multiple variable of same type then you just have to write the datatype's name once. For ex. int i, j;

edit: Obviously for function parameters you have to write it the way you wrote it i.e. int foo (int a, int b);
Last edited on Apr 29, 2009 at 1:05pm
Apr 29, 2009 at 1:05pm
well then, i guess the book was wrong xD

tyvm!
Topic archived. No new replies allowed.