I'm not very good at programming nor do I like it but it's part of my curriculum. I wrote this program from notes given in class but I get an error. The error is C2181: illegal else without matching if on line 27. The code is as follows:
#include <iostream>
using namespace std;
class Elevator
{
public:
int cur_floor;
Elevator (int=1);
void request (int);
};
Elevator::Elevator (int cfloor)
{
cur_floor=cfloor;
}
void Elevator::request (int newfloor)
{
if (newfloor<1||newfloor>15||newfloor==cur_floor)
else if (newfloor>cur_floor) <-----Line 27
{cout<<"Starting at floor"<<cur_floor<<endl;
while (newfloor>cur_floor)
{cur_floor++;
cout<<"Going up-now at floor"<<cur_floor<<endl;
}
cout<<"Stopping at floor"<<cur_floor<<endl;
}
else
{cout<<"Starting at floor"<<cur_floor<<endl;
while (newfloor<cur_floor)
{
cur_floor--;
cout<<"Going down-now at floor"<<cur_floor<<endl;
}
cout<<"Stopping at floor"<<cur_floor<<endl;
}
return;
}
int main()
{
Elevator a;
Elevator b(5);
a.request(6);
a.request(3);
b.request(1);
b.request(12);
return 0;
}
Question
Crackler
I'm not very good at programming nor do I like it but it's part of my curriculum. I wrote this program from notes given in class but I get an error. The error is C2181: illegal else without matching if on line 27. The code is as follows:
#include <iostream> using namespace std; class Elevator { public: int cur_floor; Elevator (int=1); void request (int); }; Elevator::Elevator (int cfloor) { cur_floor=cfloor; } void Elevator::request (int newfloor) { if (newfloor<1||newfloor>15||newfloor==cur_floor) else if (newfloor>cur_floor) <-----Line 27 {cout<<"Starting at floor"<<cur_floor<<endl; while (newfloor>cur_floor) {cur_floor++; cout<<"Going up-now at floor"<<cur_floor<<endl; } cout<<"Stopping at floor"<<cur_floor<<endl; } else {cout<<"Starting at floor"<<cur_floor<<endl; while (newfloor<cur_floor) { cur_floor--; cout<<"Going down-now at floor"<<cur_floor<<endl; } cout<<"Stopping at floor"<<cur_floor<<endl; } return; } int main() { Elevator a; Elevator b(5); a.request(6); a.request(3); b.request(1); b.request(12); return 0; }Link to comment
Share on other sites
2 answers to this question
Recommended Posts