2 commands seperated by a freespace?:P

This is a discussion on 2 commands seperated by a freespace?:P within the C++ Programming forums, part of the General Programming Boards category; what I cant make work is having a commandline which can take 2 commands seperated by a space.. like "get ...

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    25

    2 commands seperated by a freespace?:P

    what I cant make work is having a commandline which can take 2 commands seperated by a space.. like "get potion" or "wear shirt".. its probebly as easy as anything but cant make my code work.. if anyone have a few tips about what Im doing wrong it would be lovely:P here is my code as it is now:

    Code:
    #include <iostream.h>
    #include <conio.h>
    #include <dos.h>
    #include <stdlib.h>
    
    char first[10], second[10];
    
    int wear()
    {
    	if (second == "shirt") 	{cout << "\nYou wear the ragget old shirt";}
       if (second == "pants") 	{cout << "\nYou wear the pants";}
       if (second == "caps") 	{cout << "\nYou wear the red caps";}
       if (second == "boots") 	{cout << "\nYou wear the smooth, black boots";}
       else {cout << "\nYou dont have that to wear!";}
       return (0);
    }
    
    int get()
    {
       if (second == "shirt") 	{cout << "\nYou get a ragget old shirt";}
       if (second == "pants") 	{cout << "\nYou get tha pants";}
       if (second == "caps") 	{cout << "\nYou get the red caps";}
       if (second == "boots") 	{cout << "\nYou get a pair of smooth, black boots.";}
       else 							{cout << "\nYou cant see that item anywhere";}
    	return (0);
    }
    
    
    int main(void)
    {
    while(1)
    	{
    		cout 	<< "\nYour command? ";
          cin >> first >> second;
    
    					if (first == wear)	{wear();}
             else 	if (first == get)		{get();}
             else 	if (first == exit)   {exit(1);}
             else 								{cout << "\n What? Who? When!?";}
    
       }
    }
    I would be gratefull for any hints:P

  2. #2
    essence of digital xddxogm3's Avatar
    Join Date
    Sep 2003
    Posts
    589
    you could enter it as one string then tokenize the string into seperate strings.
    "Hence to fight and conquer in all your battles is not supreme excellence;
    supreme excellence consists in breaking the enemy's resistance without fighting."
    Art of War Sun Tzu

  3. #3
    Registered User
    Join Date
    Oct 2004
    Posts
    25
    You have any example of that? a code or something I can look at? or some link to som place where I can more clearly understand what you mean:P thanks

  4. #4
    C Programmer Stack Overflow's Avatar
    Join Date
    Apr 2004
    Posts
    477
    Hello,

    When comparing two strings, you could use strcmp(). strcmp() is a function included in the C library that Compares two strings.

    It works like the following:
    > Compares string1 to string2 character by character.

    Code example:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main() {
    	char str[] = "Hello";
    
    	if (!strcmp(str, "Hello"))
    		printf("It matched!\n");
    
    	return 0;
    }
    You may be asking why I asked if strcmp() failed, or equalled zero. Though maybe this will help:

    • <0 string1 is less than string2
    • 0 string1 is the same as string2
    • >0 string1 is greater than string2


    I apologize for representing C code in the C++ board. There are ways to compare strings in C++.

    Comparing strings is inherently different than comparing numbers. Numbers have constant, universally meaningful values. To evaluate the relationship between the magnitude of two strings, you must make a lexical comparison. Lexical comparison means that when you test a character to see if it is "greater than" or "less than" another character, you are actually comparing the numeric representation of those characters as specified in the collating sequence of the character set being used.

    C++ provides several ways to compare strings, and each has their advantages. The simplest to use are the non member overloaded operator functions operator ==, operator != operator >, operator <, operator >=, and operator <=.
    Code:
    #include <string>
    #include <iostream>
    using namespace std;
    
    int main() {
    	// Strings to compare
    	string s1("This ");
    	string s2("That ");
    	for (int i = 0; i < s1.size() && i < s2.size(); i++) {
    		// See if the string elements are the same:
    		if (s1[i] == s2[i]) {
    			cout << s1[i] << "  " << i << endl;
    			// Use the string inequality operators
    			if (s1 != s2) { 
    				cout << "Strings aren't the same:" << " ";
    				if (s1 > s2)
    					cout << "s1 is > s2" << endl;
    				else
    					cout << "s2 is > s1" << endl;
    			}
    		}
    	}
    
    	return 0;
    }
    Most often, this will be the ASCII collating sequence, which assigns the printable characters for the English language numbers in the range from 32 to 127 decimal. In the ASCII collating sequence, the first "character" in the list is the space, followed by several common punctuation marks, and then uppercase and lowercase letters. With respect to the alphabet, this means that the letters nearer the front have lower ASCII values than those nearer the end.


    - Stack Overflow
    Last edited by Stack Overflow; 11-27-2004 at 06:25 PM. Reason: Forgot part of code.
    Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

  5. #5
    essence of digital xddxogm3's Avatar
    Join Date
    Sep 2003
    Posts
    589
    stackoverflow has the ticket.

    check out these sites for more info.
    string class
    http://www.msoe.edu/eecs/ce/courseinfo/stl/string.htm
    and
    token
    http://www.cppreference.com/stdstrin...ls.html#strtok
    Last edited by xviddivxoggmp3; 11-27-2004 at 07:10 PM.
    "Hence to fight and conquer in all your battles is not supreme excellence;
    supreme excellence consists in breaking the enemy's resistance without fighting."
    Art of War Sun Tzu

  6. #6
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,806
    What stack said.

    But also since you are reading user input and you do not want it to end on a space you should use
    cin.getline with an array of chars or you can use just getline with strings.

    cin.getline example:
    Code:
    #include <iostream>
    
    int main(void)
    {
      
      char input[80];
      std::cin.getline(input,80,'\n');
      std::cout<<input;
      
      return 0;
    
    }
    If your wondering how cin.getline works this is how its declared.

    cin.getline(char *buffer, streamsize num, char delim);

    char *buffer, is the char array or pointer you want to input to.

    streamsize num, is how many characters you want to read. Basically the size of your array

    char delim, is the delimiter which basically means what you want to stop reading input on for your case a newline('\n')
    This is optional for you since getline will automatcally stop at a newline but I think it is still good to put the delimiter
    in there


    getline example:
    Code:
    #include <iostream>
    #include <string>
    
    int main(void)
    {
      
      std::string input;
      std::getline(std::cin,input,'\n');
      std::cout<<input;
    
      return 0;
      
    }
    getline works similar to cin.getline but instead of the char *buffer being what you want to write to.

    This is where you get the input from, for your case this is std::cin.

    The second one is the variable that your inputing to.

    Also agian the third is optional for your case but it is the delimiter
    Woop?

  7. #7
    Registered User
    Join Date
    Oct 2004
    Posts
    25
    Thank you:P Ill try some of it out:P here is the mudgame tast Im working on, if you like to take a look. what I was trying to get from this thread was making all the getches into cin`s.
    if you look at the code.. make sure to give me a pointer or two on how to improve it:P
    Attached Files Attached Files

  8. #8
    Registered User
    Join Date
    Oct 2004
    Posts
    25
    ok.. I made this work ok.. but now I have the problem, I cant enter one word without 2 spaces.. like "exit" or "quit" "stat" etc.. my code is now:


    Code:
    #include <iostream.h>
    #include <conio.h>
    #include <dos.h>
    #include <stdlib.h>
    
    char first[10], second[10];
    
    void wear()
    {
    	if 		(!strcmp(second, "shirt")) {cout << "\nYou wear the ragget old shirt";}
       else if 	(!strcmp(second, "pants")) {cout << "\nYou wear the pants";}
       else if 	(!strcmp(second, "caps")) 	{cout << "\nYou wear the red caps";}
       else if 	(!strcmp(second, "boots")) {cout << "\nYou wear the smooth, black boots";}
       else 											{cout << "\nYou dont have that to wear!";}
    }
    
    void get()
    {
       if 		(!strcmp(second, "shirt"))	{cout << "\nYou get a ragget old shirt";}
       else if 	(!strcmp(second, "pants")) {cout << "\nYou get tha pants";}
       else if 	(!strcmp(second, "caps")) 	{cout << "\nYou get the red caps";}
       else if 	(!strcmp(second, "boots")) {cout << "\nYou get a pair of smooth, black boots.";}
       else 										  	{cout << "\nYou cant see that item anywhere";}
    }
    
    
    int main(void)
    {
    while(1)
    	{
    		cout 	<< "\nYour command? ";
          cin.getline(first,10, ' ');   textbackground
          cin.getline(second,10, '\n');
    
                   if (!strcmp(first, "wear")) 	{wear();}
             else 	if (!strcmp(first, "get"))	  	{get();}
             else  if (!strcmp(first, "exit"))  	{exit(1);}
             else 											{cout << "\n What? Who? When!?";}
    
       }
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Get user commands from text file.
    By Ironic in forum C Programming
    Replies: 4
    Last Post: 12-08-2008, 10:38 PM
  2. Executing DOS commands from inside an image
    By Moony in forum C Programming
    Replies: 6
    Last Post: 03-16-2008, 12:40 PM
  3. Replies: 2
    Last Post: 07-27-2007, 12:48 PM
  4. Disable ALT key commands
    By Lionmane in forum Windows Programming
    Replies: 9
    Last Post: 09-23-2005, 10:41 AM
  5. Dos commands hehe
    By Carp in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 01-17-2003, 01:51 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21