ive been able to make this simple else if statement work, the problem is, i keep getting the same message when i call the showcase. i want the user to enter a name or object after the word 'show', if they dont, i want a mesage to display stating so.
here is the code im using so far:

#include<iostream>
#include<string>


using namespace std;

int main()
{
    
    
    string command; ;
	cout <<endl;

       cout << "Please enter a command." << endl;
   do
   {
        

   
    getline(cin, command); 
          
        //event that happens if assemble filename is used as command
    if (command == "build")
      {
         cout << "will attempt to build specified object\n";
        
      }


    //event that happens if load is used as command
    else if (command == "show")
      {
                //checks to see what the length of the command is. if it is
                //lower than 4 characters, message is shown. otherwise, another
                //message gets shown.
                if(command.length() > 4)
                {
                  cout<< "will call the show function to load the specified file\n";
                }
				else 
				{
                  cout << "must add object name\n";
				}
            }
       }while(command != "exit");
       
     
       
       
       return 0;   
}

all i get is the 'must add object name' message, or nothing gets displayed.
any help or suggestions?

Recommended Answers

All 10 Replies

Lines 36 through 39 can be deleted. They have no impact. Line 36's condition will ALWAYS evaluate to false. If you have a command like "show automobile", then line 31 will be false since "show" is not the same as "show automobile".

I imagine you perhaps don't want to use the == command, but rather the substr command.

http://www.cplusplus.com/reference/string/string/substr/

Perhaps you want line 31 to be:

else if (command.substr(0, 4) == "show")

I agree with VernonDozier except in that you should delete lines 36 - 39. They do serve a purpose in checking whether there is a command after 'show', although maybe you should increase the size to 5 to include a check for the inevitable [space] after 'show'.

I suspect this is a variation on your previous question.

This variant has the same issue as your previous one. If you are going to use a multi-word command, you need to use string manipulation functions to normalize it then break it up and analyze the individual parts of the command. You can't compare "show filename" with "show" it will never be equal, unless you break it up into it's component parts.

Also, I would check my comments to make sure they match up with your code, I can see a few that don't. That makes me question the accuracy and/or validity of your code. An input of "assemble filename" will never trigger a condition based on a "build" command.

The real way to do this is to split the line into arguments using white space as a delimiter and throw away the white space. Then test the number of arguments in the line. Since you are using C++, create a vector of strings, then create a stringstream from the command string, and extract the separate words with the >> operator.

vector <string> args;
stringstream ss(command, stringstream::in);
string arg;
while(ss >> arg)
{
    args.push_back(arg);
}

Now your line should be parsed and stored in the args vector, with white space removed.

It is a variation of the code i posted up earlier. i simply changed some of the variable names. i apologize if that somehow offended anyone. i was simply trying to get the code to work before the due time. the problem with these excellent suggestions, is that the assignment does not allow us to use some of those methods for the time being. i tried to change it to count the spaces but my second message never displays. it counts the spaces correctly,but faild to read the trigger the second event.

Another variation on VernonDozier code is to use strtok.
This would allow you to take tokens of the input string split by using a character deliminator, in this case a space.
I only suggest this as you said you can't use portions of VernonDozier's code.

char* word = strtok(command," "); //get first word
if (word){ //if not NULL
   if (!strcmp(word,"build")){
      //build
   }
   else if (!strcmp(word,"show")){
      //get next word
      word = strtok(NULL," ");
      //if word == NULL
      if (!word) {
         cout << "no command" << endl;
      }
      else {
         //show
      }
   }
}

I hope this helps and I hope that strtok is not another method you are not allowed to use lol

Colezy

i apologize if that somehow offended anyone.

I doubt anyone was offended. FBody was likely pointing the other thread so people who had not seen it (like me) would know what had already been discussed so people don't write up solutions that have already been offered. If you have two related threads, it's useful to mark the original as solved, then post a link to the old thread in the new one.

the problem with these excellent suggestions, is that the assignment does not allow us to use some of those methods for the time being. i tried to change it to count the spaces but my second message never displays. it counts the spaces correctly,but faild to read the trigger the second event.

I think that if you change line 31 to what I put in my first post, your 2nd message will display. As far as how to do it, if you can't use stringstreams, fine. How about "find" from the string library or strtok from the cstring library or something else? What are you allowed to use and what are you not? However you do it, you need to take a large string "show someFile.txt" and turn it into two strings ("show" and "someFile.txt"). There are any number of methods of doing this, most involving the substr command. You should write this function:

void SplitString(string line, string& firstWord, string& secondWord)

[edit]
It doesn't necessarily need to be a function. You can just stick the code into your main function. The point is you need to write code that splits a string containing two words into the separate words, using whatever methods you are allowed to use. You'll have to tell us what's allowe and what isn't if all of the proposed solutions are not allowed.
[/edit]

the assignment simply states to not use any precompiled routines to break up the line into pieces. we are allowed to use loops and comparisons. so it restricts us from using strtok or any of its variants for now.

the assignment simply states to not use any precompiled routines to break up the line into pieces. we are allowed to use loops and comparisons. so it restricts us from using strtok or any of its variants for now.

The you'll need to write your own versions of these functions. Initialize two strings to empty strings, then go through the big string character by character. If it's not white space, add the character to the first string. When you hit white space, start adding any non-white-space character to the second string.

Hmmm.... Apparently your instructor wants you to use a loop to copy characters out of the original string and into char arrays. Something like this would get you headed in the right direction:

initialize your original string (or get from input)
declare a second string, or an array of char
declare an index variable to keep track of your progress
loop through the original string, check if char is whitespace
  if not whitespace, copy the char from the original string to the copy
  increment i
  restart loop/next iteration

NOTE: This only grabs the first part of a multi-part string...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.