keep getting this error!!

hello everyone. i am new to c++ programming. I've been doing it for bout 2 weeks now. This is the first little program i tried to make from scratch on Microsoft Visual C++ 2008. but i keep getting errors. Not sure why. these are the errors i get.

error C2065: 'i' : undeclared identifier
error C2061: syntax error : identifier 'i'
error C2181: illegal else without matching if
error C2059: syntax error : ';'
error C2065: 'a' : undeclared identifier
error C2061: syntax error : identifier 'a'
error C2181: illegal else without matching if

My code:


#include <iostream>
#include <string>
using namespace std;
int main()
{
cout << ("continue the number pattern")<< endl;
cout << ("1")<< endl;



cin >> i ;

{
if
i = 2
cout << ("3") << ;
else
cout << ("error") << ;


}
{
cin >> a ;

if
a = 4
cout << ("5") << ;
else
cout << ("error") << ;
}

return 0;
}



any help would be very greatly appreciated .
thank you.
You need to declare a variable before using it eg: int i;
1
2
3
4
5
 if
i = 2
cout << ("3") << ;
else
cout << ("error") << ;
Should be
1
2
3
4
if ( i==2 ) 
    cout << "3";// You don't even need the quotes for numbers ( cout << 3;  is fine) 
else
    cout << "error";



These tutorial may help you: http://www.cplusplus.com/doc/tutorial/
Last edited on
Thank you for the help. i managed to fix up my code. The link helped a lot too, thanks for that. But there is one error that is still bugging me.

error C2059: syntax error : 'constant'


my new code :


#include <iostream>
#include <ostream>
#include <string>
using namespace std;
int main()
{
cout << ("continue the number pattern")<< endl;
cout << 1 ; endl;

int i,a ;

cin >> 'i' ;

{
if
'i' == 2
cout << 3
else
cout << ("error") << ;


}
{
cin >> 'a' ;

if
'a' == 4
cout << 5
else
cout << ("error") << ;
}

return 0;
}
cin >> 'i' ; No quotes → cin >> i; cout << 1 ; endl; << operator needed, not ; → cout << 1<< endl;
1
2
if
'i' == 2
if needs parentheses and there shouldn't be quotes → if ( i == 2 ) cout << 3 missing ; → cout << 3; cout << ("error") << ; extra << → cout << ("error");
Last edited on
maaan. now i get these errors. so annoying.

error C2143: syntax error : missing ';' before 'else'

error C2061: syntax error : identifier 'a'


code:



#include <iostream>
#include <ostream>
#include <string>
using namespace std;
int main()
{
cout << ("continue the number pattern")<< endl;
cout << 1 << endl;

int i,a ;

cin >> i ;

{
if
(i == 2)
cout << 3
else
cout << ("error");


}
{
cin >> a ;

if
a == 4
cout << 5
else
cout << ("error");
}

return 0;
}
You didn't add the semicolon after cout << 3
And you need to add parentheses also in if ( a==4 ) and add a semicolon after cout << 5
thankyou for help. i managed to fix everything , but it still doesn't want to compile. im confused. this is what i get.

1>------ Build started: Project: COD4, Configuration: Debug Win32 ------
1>Compiling...
1>pew pew.cpp
1>Linking...
1>MSVCRTD.lib(crtexew.obj) : error LNK2019: unresolved external symbol _WinMain@16 referenced in function ___tmainCRTStartup
1>C:\Documents and Settings\Beslic Family\My Documents\mato stuff\from scratch\Debug\COD4.exe : fatal error LNK1120: 1 unresolved externals
1>Build log was saved at "file://c:\Documents and Settings\Beslic Family\My Documents\mato stuff\from scratch\COD4\Debug\BuildLog.htm"
1>COD4 - 2 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========




and my code is ...

#include <iostream>
#include <string>
using namespace std;
int main()
{
cout << ("continue the number pattern")<< endl;
cout << 1 << endl;

int i,a ;

cin >> i ;

{
if
(i == 2)
cout << 3;
else
cout << ("error");


}
{
cin >> a ;

if
(a == 4)
cout << 5;
else
cout << ("error");
}

return 0;
}
Your code is now OK, is just Visual C++ that is stupid.
Try to create a new project and select the Empty project template (not CLR), then add a new cpp file and copy your code there.
That should compile
...... is just Visual C++ that is stupid


Sorry, bit I think if you are going to get a tool, you should learn how to use it correctly.
really? did u even look at your code before posting it?
Sorry, bit I think if you are going to get a tool, you should learn how to use it correctly.
No, that's definitely one of VC++'s brain farts.
closed account (z05DSL3A)
User sets the project incorrectly, therefore the tool is to blame...that's an interesting concept. ;0)
Why are you using all those bracers?

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

int main()
{
	cout << "continue the number pattern"<< endl;
	cout << 1 << endl;

	int i;
	int a;

	cin >> i;

	if(i == 2)
		cout << 3 << endl;
	else
		cout << "error";

	cin >> a;

	if(a == 4)
		cout << 5 << endl;
	else
		cout << "error";

	return 0;
}

You should only need to use bracers when you got more then 1 task to execute after each check?
@joeb
That doesnt make a difference. Its good practice anyways to put brackets around all your control statements. It makes no difference in run time.
Its good practice anyways to put brackets around all your control statements.
No, it's not. It's a matter of style. On the other hand, it is good practice to use a consistent style.
either way, i always thought it would be safer ( in the sense of accidently hitting enter ) to put brackets around my control statements.
in the sense of accidently hitting enter
What do you mean? Like this?
1
2
3
else

    /*accidental enter above*/statement;



It's only a matter of taste. I've always used the K&R, so my eyes are trained to
look for specific visual cues to find possible syntax errors.
Allman supporters argue that K&R somewhat obscures the opening brace on long
lines, but we counter with "if you can't tell the difference between these two
without looking at the brace, there's something wrong with you":
1
2
3
4
5
6
7
8
9
10
11
12
13
if (/*....................................................................................................................................................................*/)
	statement;
if (/*....................................................................................................................................................................*/){
	statement;
	statement;
	statement;
	statement;
	statement;
	statement;
	statement;
	statement;
	statement;
}
K&R supporters will argue that Allman tends to waste too much vertical
space on unimportant things, particularly during short blocks:
1
2
3
4
5
6
7
8
if (condition)
{
	statement;
}
else
{
	statement;
}
Because we are used to using indentation as the only cue for nesting, we
find the extra line for the braces focuses too much attention on something
redundant.

In reality, any style is fine as long as it's consistent within a project and
doesn't get in the way of understanding the code. I think we can all agree that
this is just Bad:
1
2
if (condition) statement;
while (condition) { statement; statement;}


The position of braces is less important, although people hold passionate
beliefs. We have chosen one of several popular styles. Pick a style that suits
you, then use it consistently.
-Brian Kernighan or Dennis Ritchie (I don't know which), The C Programming language
Last edited on
i got it working. thanks heaps Bazzy.
alright alright, point made
alright alright, point made
Topic archived. No new replies allowed.