![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#11 |
|
Expert Programmer
|
Still not working, the if statement is going false, i don't know why.
here is my code: #include <iostream>
#include <cstdlib>
using namespace std;
class cobra{
public:
cobra();
~cobra();
protected:
int checkcommand(string n);
string command;
};
cobra::cobra(){
string command;
while(1){
cout<<">>> ";
getline(cin, command);
checkcommand(command);
}
}
cobra::~cobra(){
}
int cobra::checkcommand(string n){
if(command == "print hello"){
cout<<"Hello";
exit(0);
}else{
cout<<"Error";
exit(1);
}
}
int main(){
cobra cobra1;
return 0;
}
__________________
Join us at #programmingforums @ irc.freenode.net! My software never has bugs. It just develops random features.
|
|
|
|
|
|
#12 |
|
Expert Programmer
Join Date: Aug 2005
Location: Rotterdam, the Netherlands
Posts: 942
Rep Power: 4
![]() |
what's going false exactly? also remember, comparing strings is case sensitive.
EDIT: noticed your mistake ![]() int cobra::checkcommand(string n){
if(n == "print hello"){
cout<<"Hello";
exit(0);
}else{
cout<<"Error";
exit(1);
}
} |
|
|
|
|
|
#13 |
|
Expert Programmer
|
Waheeeey it worked.
It wouldv'e taken me a while to notice that as i'm tired. Also welcome to the forums ![]()
__________________
Join us at #programmingforums @ irc.freenode.net! My software never has bugs. It just develops random features.
|
|
|
|
|
|
#14 | |
|
Expert Programmer
Join Date: Aug 2005
Location: Rotterdam, the Netherlands
Posts: 942
Rep Power: 4
![]() |
Quote:
![]() |
|
|
|
|
|
|
#15 |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5
![]() |
I'll show you the problem. It has a lot to do with scoping; you assign your getline string to a variable local to the function, rather than the object:
cobra::cobra(){
string command;
while(1){
cout<<">>> ";
getline(cin, command);
checkcommand(command);
}
} |
|
|
|
|
|
#16 |
|
Expert Programmer
|
ok thank you, Welcome to the forums to you aswell
![]()
__________________
Join us at #programmingforums @ irc.freenode.net! My software never has bugs. It just develops random features.
|
|
|
|
|
|
#17 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
To whoever recommended the exit function (I cannot be bothered to look up): don't use it. Using exit as opposed to exiting an application normally is akin to stopping a car by driving it into a brick wall. While it does exit the application, that's all it does. Any deconstructors that should be called at the end of the function will not be. If you've allocated memory, it won't be deallocated. Use exit as a last resort.
(the car analogy shamelessly stolen from Rick Rothstein on comp.lang.basic.visual) |
|
|
|
|
|
#18 | |
|
Expert Programmer
Join Date: May 2005
Location: East Lansing, MI
Posts: 663
Rep Power: 4
![]() |
Quote:
. |
|
|
|
|
|
|
#19 |
|
Expert Programmer
|
lol, thanks i was wondering how to make that button.
What sort of alternative can i use to exit();?
__________________
Join us at #programmingforums @ irc.freenode.net! My software never has bugs. It just develops random features.
|
|
|
|
|
|
#20 | |
|
Expert Programmer
Join Date: Aug 2005
Location: Rotterdam, the Netherlands
Posts: 942
Rep Power: 4
![]() |
Quote:
|
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|