how would i be able to make sure the user inputs the correct data. for example, if i wanted a command in my code to be run (filename). how can i display a message if the user forgets to add the name of the file or the 'filename'. i hope this question makes sense, if it fails to do so, i will be more than happy to explain more. the instructions in my assignment read the following:
"If a command requires an argument and it is missing, the interpreter should tell the user that an argument is required and then output the prompt and wait for a command."

all i have at this point is a basic cout command that displays depending on what the user inputs. in other words, it doesnt check whether an argument is missing.
any help would be appreciated.
thanks

You should be able to do something like this with basic string-manipulation functions. But, the information here is a little lacking. I think a better description of the command format, how the command is interpreted, and some sample code are in order.

Are you attempting to do this from a command line or running the program and then picking apart the input from there? Running from a command line is easy. Picking a string apart inputed after the program is ran is also easy, but a bit more involved I think.

Are you attempting to do this from a command line or running the program and then picking apart the input from there? Running from a command line is easy. Picking a string apart inputed after the program is ran is also easy, but a bit more involved I think.

Assuming I understand your statement correctly. The level of involvement related to analyzing the inputs is about the same. The only real difference is whether you or the OS has to handle the input functionality.

The level of involvement related to analyzing the inputs is about the same. The only real difference is whether you or the OS has to handle the input functionality.

I think what you said and what I meant are the same. By easy I was refering to

int main (int argc, char *argv[])

it's already done for you, argv is the array of strings, and argc is the number of words

Where as the other way you'd need a deliminator to seperate the words, usually a space, put each word into an array of strings, and get the length of the array.

The first way just needs error catching. The second way needs to be formated into what you want it, and then error catching.

Ok, so this is the code that i have implemented. for the debug and execute commands, the task is simple, i just display a certain message. now when it comes to the load command, i have to be able to make sure the user inputs which file to load. at this point i only have to display a dummy message stating the file was loaded.
what i need help in is being able to make sure the user inputs a name for the file. if he does not, i should display a message. i hope this is a bit more clear.

#include<iostream>
#include<string>
using namespace std;

int main()
{
    
    
       string command; 

       cout << "Welcome to the Intrepeter!" << endl;
       cout << "Please feel free to enter a command." << endl;
   do
   {
 
    getline(cin, command);  
    
    
    if (command == "load filename")
      {
          cout << "will call the load function to load the specified file\n";
      }
    else if (command == "execute")
      {
         cout << "will call the computer simulation program to execute the program\n";
         cout << "that was previously loaded in memory\n";
      }
    else if (command == "debug")
      {
         cout << "will allow you to execute in debug mode\n";
      } 
     
         
    else if (command == "help")
      {
         cout << "You have accesed the help menu.\n";
         cout << "Here are the available commands.\n";
         cout << "execute\n";
         cout << "debug\n";
         cout << "help\n";
         cout << "exit\n";     
      }
    else if (command != "load filename" || "execute" || "debug")
       {
             cout << "Invalid Entry\n";
             cout << "Please your correct terms\n";
             command.clear();
       }
       }while(command != "exit");
     
       
       return 0;   
}

If you are using the 'space' character as the delimiter, you could simply check the string's length and compare it to the location of the 'space' character.

if (string.find(' ') < string.length())

The issue is, you will have to detect the "load" part of the input and react to that, then respond to the rest of the command. Using "load filename" as your if condition will only work if the user literally enters "load filename" exactly like that.
Look up string::find(), string::begin(), string::substr(), and string::length().

else if (command != "load filename" || "execute" || "debug")

You need a comparison after each ||.

You could also just get rid of that last if statement completely.

The only thing I'm unsure of is the case comparison. If someone enters Debug instead of debug, I don't think the debug portion of your if statement will catch it.

I think if you use strcmp(string1, string2) it will work out better but I'm not 100% sure.

else if (command != "load filename" || "execute" || "debug")

You need a comparison after each ||.

You could also just get rid of that last if statement completely.

The only thing I'm unsure of is the case comparison. If someone enters Debug instead of debug, I don't think the debug portion of your if statement will catch it.

I think if you use strcmp(string1, string2) it will work out better but I'm not 100% sure.

strcmp() is case-sensitive, also it doesn't work with C++ style std::strings unless you call the string::c_str() method. For example, if you have:

#include <iostream>
#include <cstring>
using namespace std;
 
int main()
{
  char string1[] = "String1";
  char string2[] = "string1";

  int result = strcmp(string1, string2);

  cout << "The result is: " << result << endl;

  cin.get();
  return 0;
}

The output will be -1 because "String1" is less than "string1". The strings are not equal because a capital/upper-case character has a lower ASCII value than a lower-case character. Refer to the ASCII table for specifics.

What the OP would have to do is normalize the input using a loop and either the tolower() or toupper() function.

You could use the string::compare() function, but normalization would still be advisable.

i wanted to use the strcmp command, but the assignment addresses us not to do so.
is there another way to do that or do i have to go and specify something like

//would i have to do this with every possible way of debug if i dont use the strcmp command?

 else if (command == "debug" || command == 'Debug' || command == 'DEBUG')   
   {
         cout << "will allow you to execute in debug mode\n";  
    }

i wanted to use the strcmp command, but the assignment addresses us not to do so.
is there another way to do that or do i have to go and specify something like

//would i have to do this with every possible way of debug if i dont use the strcmp command?

 else if (command == "debug" || command == 'Debug' || command == 'DEBUG')   
   {
         cout << "will allow you to execute in debug mode\n";  
    }

Do what FBody suggested, and use the toupper() or tolower(), loop through the command and make all the characters in the string upper or lower case, then you can compare command to the lowercase (or uppercase) version of the word.

int main (int argc, char *argv[]){
    string command = "DeBuG";
    int i = 0;
    while (i < command.length){
        command[i] = tolower(command[i]);
        i++;
    }
    cout << command;

The output should be debug

thank you very much

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.