Thread: ARG! If statements with cin NOT WORKING!

  1. #1
    Registered User
    Join Date
    Nov 2004
    Posts
    69

    ARG! If statements with cin NOT WORKING!

    Ok, well this is really aggrivating me. Im making a simple Command Prompt programs, where I enter commands, and it does them, but it doesn't work at all. It just doesn't recognize anything I give it. Whatever I put in it just spits it back out with "Unknown command". WHY??.

    Also, can anyone give me feedback on if my code looks organized, and at the var name's look good. Im trying to make my code more "proffessional" looking.

    Thanks!

    main.cpp
    Code:
    #include <stdlib.h>
    #include <iostream>
    #include <fstream>
    #include "textf.h"
    //#include "main.h"
    CTextFunc CTextC;
    
    int main()
    {	
    	CTextC.init();
    	return 0;
    }
    textf.cpp
    Code:
    #include <stdlib.h>
    #include <iostream>
    #include <fstream>
    #include <string>
    #include "textf.h"
    using std::cout;
    using std::cin;
    using std::endl;
    
    void CTextFunc::init()
    {	
    	//Assign <<: to harrow before its used
    	charArrows = "<< : ";
    	startUp();
    }
    void CTextFunc::startUp()
    {
    	system ("cls");
    	ban("Blade Tools By: Jonathan Bastnagel");
    	getInput();
    }
    void CTextFunc::getInput()
    {
    	char charInput[256];
    	ba("");
    	cin.getline(charInput,256);
    	exec(charInput);
    }
    
    void CTextFunc::exec(char* charString)
    {
    
    	if(charString=="")
    	{
    	ba("Nothing executed");
    	bn(charString);
    	getInput();
    	} else
    	if(charString=="q"){
    	system("clear");
    	getInput();
    	} else {
    	ba("Unknown command \'");
    	b(charString);
    	bn("\'");
    	getInput();
    	}
    
    /*
    //SWITCH DOESN'T TAKE CHAR* UGH!
    switch(textString){
    	default: 	ba("Unknown command \'");b(textString);bn("\'");
    	}
    */
    }
    void CTextFunc::ban(char* charString)
    {
    	cout<<charArrows<<charString<<endl;
    }
    void CTextFunc::bn(char* charString)
    {
    	cout<<charString<<endl;
    }
    void CTextFunc::ba(char* charString)
    {
    	cout<<charArrows<<charString;
    }
    void CTextFunc::b(char* charString)
    {
    	cout<<charString;
    }
    textf.h
    Code:
    #ifndef TEXTF_H
    #define TEXTF_H
    
    class CTextFunc
    {
    public:
    		//Assigns default values to variables
    	void init();
    		//Starts up, shows about info
    	void startUp();
    		//Grabs keyboard input, send's to exec to be checked, and executed
    	void getInput();
    		//Checks inputed characters, and does required functions
    	void exec(char* charString);
    		//Outputs text with charArrows and new line
    	void ban(char* charString);
    		//Outputs text with charArrows
    	void ba(char* charString);
    		//Outputs text with new line
    	void bn(char* charString);
    		//Outputs text
    	void b(char* charString);
    
    private:
    	char* charArrows;
    };
    
    #endif
    Last edited by bladerunner627; 09-26-2005 at 09:54 PM.

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    You can't use the equality operator (==) to compare C-style strings with string constants. Remember that in a line of code like that, you're actually testing the equality of the memory address pointed to by the char * and the memory address at which the string constant is stored - which is never going to match. Instead, use STL strings or the strcmp() function in <cstring>.

    Which reminds me - you're mixing old and new-style C++. There's no such thing as stdlib.h - it should be cstdlib.

  3. #3
    Registered User
    Join Date
    Nov 2004
    Posts
    69
    Quote Originally Posted by sean_mackrory
    You can't use the equality operator (==) to compare C-style strings with string constants. Remember that in a line of code like that, you're actually testing the equality of the memory address pointed to by the char * and the memory address at which the string constant is stored - which is never going to match. Instead, use STL strings or the strcmp() function in <cstring>.

    Which reminds me - you're mixing old and new-style C++. There's no such thing as stdlib.h - it should be cstdlib.
    Thanks, man! I owe you one! I wish I knew this at the get-go. Iv been learning my stuff from a book so newstyle is hard to get used to.

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    For a reference, go to cppreference.com. If it's in either standard library - drop the .h, if it's in the C standard library - add the c, if it's neither, but came with your compiler, you keep the .h and use the < > 's, if it's your own file, use the quotes like you are (and the .h). Hope that helps.

  5. #5
    Registered User
    Join Date
    Nov 2004
    Posts
    69
    Ok, thanks. Also another thing thats bugging me about this program is that since I set charInput to 256 characters then someone can't type in more then 256 characters. Only problem is if they enter more then 256 it will crash with an overflow. If I define charInput like this
    Code:
    char* charInput
    it will crash no matter how big it is. Any ideas?

  6. #6
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    STL strings (the string class) grow as you need them. And in any case - what kinda idiot inputs more than 256 characters at a prompt? There are ways to safeguard against this happening - but they're more advanced console I/O - so don't worry about it too much for now.

  7. #7
    Registered User
    Join Date
    Nov 2004
    Posts
    69
    Quote Originally Posted by sean_mackrory
    STL strings (the string class) grow as you need them. And in any case - what kinda idiot inputs more than 256 characters at a prompt? There are ways to safeguard against this happening - but they're more advanced console I/O - so don't worry about it too much for now.
    EDIT:
    Also, maybe if the idiot was running something in a series of directories. EX: exec "C:\blah\omg\this\is\a\really\long\string\for\a\fi le\location.exe". This isn't 256 characters, but you still get the idea.
    Last edited by bladerunner627; 09-26-2005 at 11:50 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 05-07-2009, 11:31 AM
  2. Max/min function not working correctly
    By En-Motion in forum C++ Programming
    Replies: 6
    Last Post: 03-19-2009, 12:28 AM
  3. If statements not working
    By leroyjenkins in forum C Programming
    Replies: 13
    Last Post: 02-25-2009, 11:46 AM
  4. Switch statements for strings
    By cxs00u in forum C++ Programming
    Replies: 5
    Last Post: 04-17-2002, 03:38 PM
  5. Why isn't my program working?
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 01-21-2002, 02:10 PM