Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Aug 21st, 2005, 5:36 PM   #11
coldDeath
Expert Programmer
 
coldDeath's Avatar
 
Join Date: Aug 2005
Location: UK
Posts: 862
Rep Power: 4 coldDeath is on a distinguished road
Send a message via AIM to coldDeath Send a message via Yahoo to coldDeath
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.
coldDeath is offline   Reply With Quote
Old Aug 21st, 2005, 5:38 PM   #12
Polyphemus_
Expert Programmer
 
Polyphemus_'s Avatar
 
Join Date: Aug 2005
Location: Rotterdam, the Netherlands
Posts: 942
Rep Power: 4 Polyphemus_ is on a distinguished road
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);
	}
}
Polyphemus_ is offline   Reply With Quote
Old Aug 21st, 2005, 5:41 PM   #13
coldDeath
Expert Programmer
 
coldDeath's Avatar
 
Join Date: Aug 2005
Location: UK
Posts: 862
Rep Power: 4 coldDeath is on a distinguished road
Send a message via AIM to coldDeath Send a message via Yahoo to coldDeath
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.
coldDeath is offline   Reply With Quote
Old Aug 21st, 2005, 5:42 PM   #14
Polyphemus_
Expert Programmer
 
Polyphemus_'s Avatar
 
Join Date: Aug 2005
Location: Rotterdam, the Netherlands
Posts: 942
Rep Power: 4 Polyphemus_ is on a distinguished road
Quote:
Originally Posted by coldDeath
Also welcome to the forums
Thanks
Polyphemus_ is offline   Reply With Quote
Old Aug 21st, 2005, 5:45 PM   #15
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
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);
	}
}
Remove the "string command" line in this function.
Arevos is offline   Reply With Quote
Old Aug 21st, 2005, 5:49 PM   #16
coldDeath
Expert Programmer
 
coldDeath's Avatar
 
Join Date: Aug 2005
Location: UK
Posts: 862
Rep Power: 4 coldDeath is on a distinguished road
Send a message via AIM to coldDeath Send a message via Yahoo to coldDeath
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.
coldDeath is offline   Reply With Quote
Old Aug 22nd, 2005, 1:10 PM   #17
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
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)
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Aug 22nd, 2005, 1:18 PM   #18
OpenLoop
Expert Programmer
 
OpenLoop's Avatar
 
Join Date: May 2005
Location: East Lansing, MI
Posts: 663
Rep Power: 4 OpenLoop is on a distinguished road
Quote:
Originally Posted by Ooble
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. ...
I knew Ooble would want to comment on the exit() function. I agree with you, never use exit(1) unless you're implementing an emergency shutdown button for a WMD missle launcher .
OpenLoop is offline   Reply With Quote
Old Aug 22nd, 2005, 1:41 PM   #19
coldDeath
Expert Programmer
 
coldDeath's Avatar
 
Join Date: Aug 2005
Location: UK
Posts: 862
Rep Power: 4 coldDeath is on a distinguished road
Send a message via AIM to coldDeath Send a message via Yahoo to coldDeath
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.
coldDeath is offline   Reply With Quote
Old Aug 22nd, 2005, 1:48 PM   #20
Polyphemus_
Expert Programmer
 
Polyphemus_'s Avatar
 
Join Date: Aug 2005
Location: Rotterdam, the Netherlands
Posts: 942
Rep Power: 4 Polyphemus_ is on a distinguished road
Quote:
Originally Posted by coldDeath
lol, thanks i was wondering how to make that button.

What sort of alternative can i use to exit();?
i'd say to break all the loops. You can define a variable, which you set to false when there is an error. All the loops check this variable, and if it is false, you break (or return).
Polyphemus_ is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 8:21 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC