elseif difficulty

Hi. I was wondering if I could get another small smidge of tech support.
I'm still working my way through Bucky's C++ tutorials on youtube, and I'm trying to use an elseif statement.. the code is below.

When I get to the elseif, the compiler gives me this error message

'elseif was not declared in this scope'

What does that mean and how do I fix it?

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

  int main(){
	
	int answer;
	cout << "How old is Ben" << endl;
	cin >> answer;
	

	if (answer < 35)
	{
		cout << "Too young";
	}
	elseif (answer > 35)
	{
		cout << "Too old";
	}
	else
	{
		cout << "You are correct!";
	}
		
	cin.get();
	return 0;
}
Last edited on
else if needs a space.
A space between elseif and (answer < 35), or a space between the elseif statements and the curly brackets?
A space between "else" and "if". :) Look at my previous post more carefully.
space in between
We often think of else if as a special type of construct but it's really just an else clause that contains nothing more than an if statement.
more importantly, there is no elseif construct in C/C++


you end up with this...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
if (cond)
{
  // blah
}
else
{
   if (cond)
   {
      // blah
   }
   else
   {
      // blah
   }
}


removing the braces leaves...

1
2
3
4
5
6
if (cond)
   // blah
else if (cond)
   // blah
else
   // blah 

Topic archived. No new replies allowed.