1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Solved: C++ and Functions..

Discussion in 'Software Development' started by Codiah, Oct 4, 2008.

Thread Status:
Not open for further replies.
Advertisement
  1. Codiah

    Codiah Thread Starter

    Joined:
    Sep 27, 2008
    Messages:
    116
    Ok, so i'm having a bit of trouble at the moment.. i understand what functions are and how to set up the bast of the function... my problem is when it comes to actually using the function i've set up... i'm writing my first c++ program with numerous functions in it and i want to be able to utalize them all like commands... i know how to do "choice" in c++ but i want to make input in small commands not just single letters... but its a little lost on me at the moment... i'm trying my best to avoid the goto command and keep everything in a modular form, i just need to know how to connect the peices.. any help would be great..
     
  2. mt2002

    mt2002

    Joined:
    Sep 14, 2005
    Messages:
    919
    Just use std::string and input a command using std::cin. Heres an example program I just wrote. It has two commands: print and exit. Typing "print" executes the print command (just displays a string), typing "exit" terminates the program. Hope this gives you some ideas:

    Code:
    #include <iostream>
    #include <string>
    
    std::string commands[] = {
    	"print",
    	"exit"
    };
    
    enum {
    	CMD_PRINT,
    	CMD_EXIT
    };
    
    void cmdPrint () {
    
    	std::cout << "command print executed" << std::endl;
    }
    
    bool cmdTest (std::string& cmd) {
    
    	bool ret=true;
    
    	if (cmd==commands[CMD_PRINT])
    		cmdPrint();
    	else if (cmd==commands[CMD_EXIT])
    		;
    	else
    		ret=false;
    
    	return ret;
    }
    
    int main()
    {
    	std::string strCmd;
    	std::cout << "Type \"exit\" to quit\n" << std::endl;
    
    	do {
    		std::cout << "command >";
    		std::cin >> strCmd;
    
    		if (! cmdTest (strCmd) )
    			std::cout << "unkown command \'" << strCmd << "\', ignored" << std::endl;
    
    	}
    	while ( strCmd != commands[CMD_EXIT]);
    }
     
  3. Codiah

    Codiah Thread Starter

    Joined:
    Sep 27, 2008
    Messages:
    116
    Perfect, exactly what I've been looking for, once again.. thanks... wasn't to sure i was gonna get a replay back on this one...
     
  4. mt2002

    mt2002

    Joined:
    Sep 14, 2005
    Messages:
    919
    I would have replied a little earlier but didnt quite know what you were looking for :) Glad it helps though :D
     
As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/756115

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice