Question

In: Computer Science

An issue that we have been constantly facing is determining whether valid characters exist in the...

An issue that we have been constantly facing is determining whether valid characters exist in the command line arguments for the laboratory tests. Many solutions rely on stepping through the argument list determining whether the current character is valid via a series of 'if' statements. Whilst this approach is certainly effective it is often prone to error. Consider an array of suitable characters for an application as follows:

const char validCharacters[] = ".,-+eE0123456789";

Utilising nested 'for' loops, the characters in argv[1] could be examined to see whether it was one in the list provided by the array validCharacters. A Boolean flag (true or false) can be utilised to determine whether an invalid character exists and an error message output. Set the command-line arguments to ’34.0’. Based on the discussed concepts, create a small C++ application that uses nested loops to confirm that the characters in argv[1] are valid. If an invalid digit is found, display an ‘X’ on the console window and immediately exit. Change the input argument to ‘3,512t’ and determine confirm that your application functions correctly. Hint: Consider using the function strlen() to determine the length of the input argument.

Solutions

Expert Solution

validchar.cpp

#include <iostream>

using namespace std;

int main(int argc, char **argv)

{

    const char validCharacters[] = ".,-+eE0123456789";

    // checking if word provided for validating

    if (argc < 2)

    {

        cout << "no word provides" << endl;

        exit(-1);

    }

    string input_word = argv[1];

    // looping through word

    for (int i = 0; i < input_word.length(); i++)

    {

        bool found = false;

        // looping through valida char

        for (int j = 0; j < 17; j++)

        {

            if (input_word[i] == validCharacters[j])

            {

                // if word found making boolean variable as true and

                // breaking the loop

                found = true;

                break;

            }

        }

        // if char not in valid char then printing the error message

        if (!found)

        {

            cout << "Error: invalid character " << input_word[i] << endl;

            return -1;

        }

    }

    // if no error then printing X only

    cout << "X" << endl;

    return 0;

}

// OUT

Please do let me know if u have any concern...


Related Solutions

C++ program to accept characters as a polynomial and then output whether they're valid or not....
C++ program to accept characters as a polynomial and then output whether they're valid or not. Types of polynomials that don't work: n^5n, n^9.7, 8n, n^7-7*n
Give an example of a public policy issue that you have been involved with, whether at...
Give an example of a public policy issue that you have been involved with, whether at the federal, state or local area. How did you participate from agenda setting onto policy implementation?
A major issue facing many organizations today is whether they can identify the "right" talent for...
A major issue facing many organizations today is whether they can identify the "right" talent for their organizations. Talent often resides not just in what people know, but also in the intangibles such as personality traits and values. Take a couple of paragraphs to compare and contrast any of the following personality and values inventories (e.g., The BIG FIVE, Myers-Briggs, Schwartz, or Hofstede) from class and/or as discussed in the book. Explain how you might use them to identify talent...
Costs of determining whether defects exist are called: Select one: a. internal failure costs. b. prevention...
Costs of determining whether defects exist are called: Select one: a. internal failure costs. b. prevention costs. c. defects costs. d. external failure costs. e. appraisal costs.
In this chapter, we have noted how businesses are dynamic and constantly looking to exploit new...
In this chapter, we have noted how businesses are dynamic and constantly looking to exploit new opportunities that involve changing the way they operate production. What might not have been a success for some firms does not mean to say that there are no other firms that will be able to benefit. This article shows how problems faced by one firm in making sufficient profits are not necessarily shared by other firms as the use of factor inputs is changed.    ...
n this chapter, we have noted how businesses are dynamic and constantly looking to exploit new...
n this chapter, we have noted how businesses are dynamic and constantly looking to exploit new opportunities that involve changing the way they operate production. What might not have been a success for some firms does not mean to say that there are no other firms that will be able to benefit. This article shows how problems faced by one firm in making sufficient profits are not necessarily shared by other firms as the use of factor inputs is changed.    ...
n this chapter, we have noted how businesses are dynamic and constantly looking to exploit new...
n this chapter, we have noted how businesses are dynamic and constantly looking to exploit new opportunities that involve changing the way they operate production. What might not have been a success for some firms does not mean to say that there are no other firms that will be able to benefit. This article shows how problems faced by one firm in making sufficient profits are not necessarily shared by other firms as the use of factor inputs is changed.    ...
n this chapter, we have noted how businesses are dynamic and constantly looking to exploit new...
n this chapter, we have noted how businesses are dynamic and constantly looking to exploit new opportunities that involve changing the way they operate production. What might not have been a success for some firms does not mean to say that there are no other firms that will be able to benefit. This article shows how problems faced by one firm in making sufficient profits are not necessarily shared by other firms as the use of factor inputs is changed.    ...
n this chapter, we have noted how businesses are dynamic and constantly looking to exploit new...
n this chapter, we have noted how businesses are dynamic and constantly looking to exploit new opportunities that involve changing the way they operate production. What might not have been a success for some firms does not mean to say that there are no other firms that will be able to benefit. This article shows how problems faced by one firm in making sufficient profits are not necessarily shared by other firms as the use of factor inputs is changed.    ...
We have to write an Issue Negligence statement. The instructions- " In writing an issue statement...
We have to write an Issue Negligence statement. The instructions- " In writing an issue statement the question should identify the general legal theory linked to a fact which gives rise to the claim. Remember your reply must address the substantive writing not the format. Suggest improvements along with an explanation of why and then offer the improvement by rewriting the issue of the post. If in fact you see no room for improvement identify the parts of it that...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT