Question

In: Computer Science

Q) You have been asked to develop a program which processes C++ null-terminated strings to extract...

Q) You have been asked to develop a program which processes C++ null-terminated strings to extract character and numerical data separately. Assume that the users runs the application (examTest.exe) from the command line as follows: examTest.exe X-Axis:10,Y-Axis:17,Z-Axis:-6 Your application should step through the input arguments and extract the values of the XAxis, Y-Axis and Z-Axis commands. The user must always enter the arguments in the specified order, however your application should check they are correct. At the end of your application, the values of X-Axis, Y-Axis and Z-Axis should be stored as separate integers.

Solutions

Expert Solution

Note: Done accordingly. Please comment for any problem. Please Uprate. Thanks

Code:

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

bool startsWith(const char *a, const char *b)
{
if(strncmp(a, b, strlen(b)) == 0) return 1;
return 0;
}

int main(int argc, char* argv[]) {
   if(argc<2){
       cout<<"Usage: examTest.exe X-Axis:#,Y-Axis:#,Z-Axis:# \n";
       return 1;
   }
   try{
       int x,y,z;
       vector<string> vect;
       string s(argv[1]);
       stringstream ss(s);

       while( ss.good() )
       {
           string substr;
           getline( ss, substr, ',' );
           vect.push_back( substr );
       }

       for(int i=0;i<vect.size();i++)
       {
           int n = vect.at(i).length();
  
           // declaring character array
           char char_array[7];
  
           strcpy(char_array, vect.at(i).c_str());
           char *next = strtok(char_array, ":");
           string s(next);
           if(s.compare("X-Axis") == 0){
               next = strtok(NULL, ",");
               if(next!=NULL){
                   x=atoi(next);
               }else{
                   throw exception("Invalid Input");
               }
           }else if(s.compare("Y-Axis") == 0){
               next = strtok(NULL, ":");
               if(next!=NULL){
                   y=atoi(next);
               }else{
                   throw exception("Invalid Input");
               }
           }else if(s.compare("Z-Axis") == 0){
               next = strtok(NULL, ":");
               if(next!=NULL){
                   z=atoi(next);
               }else{
                   throw exception("Invalid Input");
               }
           }else{
               throw exception("Invalid Input");
           }
       }
       cout<<"Value of X-Axis is :"<<x<<endl;
       cout<<"Value of Y-Axis is :"<<y<<endl;
       cout<<"Value of Z-Axis is :"<<z<<endl;
   }catch(exception){
       cout<<"Usage: examTest.exe X-Axis:#,Y-Axis:#,Z-Axis:# \n";
       return 1;
   }
   system("pause");
   return 0;
}

Output:


Related Solutions

C++ program assignment asks to implement the following functions. Each function deals with null terminated C-strings....
C++ program assignment asks to implement the following functions. Each function deals with null terminated C-strings. Assume that any char array passed into the functions will contain valid, null-terminated data. The functions must have the signatures listed below. 1. This function returns the last index where the target char can be found in the string. it returns -1 if the target char does not appear in the string. For example, if s is “Giants” and target is ‘a’ the function...
A C++ question: Implement the following functions. Each function deals with null terminated C-strings. You can...
A C++ question: Implement the following functions. Each function deals with null terminated C-strings. You can assume that any char array passed into the functions will contain valid, null-terminated data. Your functions must have the signatures listed below. 1. This function returns the last index where the target char can be found in the string. it returns -1 if the target char does not appear in the string. For example, if s is “Giants” and target is ‘a’ the function...
A C++ question: Implement the following functions. Each function deals with null terminated C-strings. You can...
A C++ question: Implement the following functions. Each function deals with null terminated C-strings. You can assume that any char array passed into the functions will contain valid, null-terminated data. Your functions must have the signatures listed below. 1. This function returns the last index where the target char can be found in the string. it returns -1 if the target char does not appear in the string. For example, if s is “Giants” and target is ‘a’ the function...
A C++ question: Implement the following functions. Each function deals with null terminated C-strings. You can...
A C++ question: Implement the following functions. Each function deals with null terminated C-strings. You can assume that any char array passed into the functions will contain valid, null-terminated data. Your functions must have the signatures listed below. 1. This function returns the last index where the target char can be found in the string. it returns -1 if the target char does not appear in the string. For example, if s is “Giants” and target is ‘a’ the function...
A C++ question: Implement the following functions. Each function deals with null terminated C-strings. You can...
A C++ question: Implement the following functions. Each function deals with null terminated C-strings. You can assume that any char array passed into the functions will contain valid, null-terminated data. Your functions must have the signatures listed below. 1. This function returns the last index where the target char can be found in the string. it returns -1 if the target char does not appear in the string. For example, if s is “Giants” and target is ‘a’ the function...
Implement the following functions. Each function deals with null terminated C-strings. You can assume that any...
Implement the following functions. Each function deals with null terminated C-strings. You can assume that any char array passed into the functions will contain valid, null-terminated data. Your functions must have the signatures listed below. 1. This function returns the last index where the target char can be found in the string. it returns -1 if the target char does not appear in the string. For example, if s is “Giants” and target is ‘a’ the function returns 2. int...
PLEASE PROVIDE COMMENTS ON STEPS Write a C++ program that modifies a string (null terminated) as...
PLEASE PROVIDE COMMENTS ON STEPS Write a C++ program that modifies a string (null terminated) as follows: Consonants are positioned at the beginning of the string and vowels are moved to the end of the string. Example : Original string : washer New string : wshrae Note: The library string functions cannot be used. You must use pointers and the switch statement to execute this program. Assume that the vowels are a, e, i, o, and u. The modification has...
DEVELOP A C++ PROGRAM IN PROCEDURAL CODE that will recursively alphabetize a set of strings in...
DEVELOP A C++ PROGRAM IN PROCEDURAL CODE that will recursively alphabetize a set of strings in a user-specified file using the tree sort algorithm explained in the lectures while maintaining a balanced binary tree at all times. The following will test your program with an input file containing: Max Hank Jet Frisky Chata Richard Nan Sam Thomas Karen Gerri Ingrid Alan Dana When done print out the contents of the tree with inorder traversal in a tabular format similar to...
As a statistical consultant, you have been asked to develop a linear model which shall be...
As a statistical consultant, you have been asked to develop a linear model which shall be given to first year executive MBA students. Variable           Name                                                   Description X1                   Gender                                                Male or Female X2                   GMAT Score                                      Score on GMAT Test X3                   College Degree Previously Earned     Bachelors, Masters, or PhD Y                     Income                                                            Annual Income in Thousand Dollars Where 1=Male and 2= Female for X1 and B = Bachelors Degree, M=Master’s Degree, and P= PhD for X3. Question Based on above information. Given...
Suppose you have been asked to develop a simple model for the movement of ants in...
Suppose you have been asked to develop a simple model for the movement of ants in the presence of a food source. Discuss how you could simulate this model. Your answer should include any simplifying assumptions, some parameters that you’d need, and the interaction rules you would set up.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT