C++ warning c4700 when compiling

I kinda know what this means, but I dont know how to fix it.. Any ideas?
warning C4700: uninitialized local variable 'counter' used
warning C4700: uninitialized local variable 'matchcount' used

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
// 10 values.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
 
using namespace std;


int _tmain(int argc, _TCHAR* argv[])
{
	return 0;
}

 int main() 
{ 
	
    int numx;
    int matchcount;
	int counter;

	for ( int counter = 1; counter <= 10; counter++ )
 
    cout << "Enter a number for place " << counter << ": ";
    cin >> numx;
    cout << numx << "\n";

    if ( numx == counter )
    matchcount++;
    cout << "There were " << matchcount << " matches\n";
 
}
Yikes!

1
2
3
4
5
6
int counter
for (int counter = 1; counter <= 10; counter++)
    cout << "Enter a number for place " << counter << ": ";
...
if ( numx == counter )
    matchcount++;


I'm not sure if you can see if, but you have a few problems here.

1. You define int counter twice. Once at line 20 and once at line 22. This means that you have a version of counter in your for loop and a version outside of your for loop, they are not the same variable. To fix: change line 22 to: for ( counter = 1; counter <= 10; counter++)

2. You use matchcount++ without ever setting matchcount first. We have no idea what matchcount was during our first iteration and so incrementing it to another value won't do anything for us. To fix: change line 19 to: int matchcount = 0;

3. You are missing {} brackets from your for loop. The only part which is currently looped is the cout in line 24. I think you need a { at line 23, and a } between lines 29 and 30.

4. Remove int _tmain(int argc, _TCHAR* argv[]), or move your entire code to that function and remove int main(). The two functions are supposed to be the same thing.
Last edited on
I agree YIKES!!! Thanks for your help... I hope I get better :-)
Topic archived. No new replies allowed.