Question

In: Computer Science

Write a C++ or program that parses (and stores into a variable of the correct type)...

Write a C++ or program that parses (and stores into a variable of the correct type) a string into 4 fields separated by a colon (:):

Field 1: A non-empty string value that does not contain a colon
Filed 2: An odd integer, or empty
Filed 3: One of these values: red, orange, yellow, green, blue, indigo, violet
Field 4: Another string value (may contain colon characters)

output:

Accept: sample:556989:green:Longer example string :)
Reject (bad color): sample:556989:purple:Longer example string :)

A sample string is:
sample:556989:green:Longer example string :)
The main method should read sample strings, one line at a time, from a file specified on the command line (i.e., passed in via argv)

Solutions

Expert Solution

Code in C++

#include<iostream>
#include<math.h>
#include <fstream>
using namespace std;

int main(int argc, char const *argv[])
{
        //handle less arguments
        if(argc<2){
                cout<<"Please enter the input file in command line arguments"<<endl;
                return 0;
        }
        
        //use ifstream to parse the file
        ifstream infile(argv[1]);

        string line;
        //read line by line form the file
        while (std::getline(infile, line))
        {
                string non_empty_string="", color="", final_string;
                int val=0;
                int i=0;
                //read non_empty_string
                while(line[i]!=':'){
                        non_empty_string+=line[i];
                        i++;
                }
                i++;
                //check for empty integer
                if(line[i]==':'){
                        val=-1;//we set this to -1 to indicate empty value
                }
                //read the integer
                while(line[i]!=':'){
                        val=val*10+(line[i]-'0');
                        i++;
                }
                i++;
                //read the color
                while(line[i]!=':'){
                        color+=line[i];
                        i++;
                }
                i++;
                //final string is the remaing part of the line
                final_string = line.substr(i);
                //chaeck for accept and reject
                if(non_empty_string.size()>0 && (val==-1 || val%2==1) && 
                        (color=="red" || color=="orange" || color=="yellow" || color=="green" || color=="blue" || color=="indigo" || color=="violet")){
                                printf("Accept: %s\n", line.c_str());
                }else{
                        if(non_empty_string.size()==0){
                                printf("Reject: (first field should be non-empty string without colon): %s\n", line.c_str());
                        }else if(val!=-1 && val%2==0){
                                printf("Reject: (integer should be odd or empty): %s\n", line.c_str());
                        }else{
                                printf("Reject: (bad color): %s\n", line.c_str());
                        }
                }
        }
        return 0;
}

Sample Input/Output

Content in input.txt

Console Input/output


Related Solutions

C++ Program: Write a program that prompts the user for two numbers and stores them in...
C++ Program: Write a program that prompts the user for two numbers and stores them in signed integers. The program should then add those two numbers together and store the result in a signed integer and display the result. Your program should then multiply them by each other and store the result in another integer and display the result. Then do the same but with dividing the first number by the second. Display an error message to the screen if...
C Programming Write a program in C that reads in a file, stores its contents as...
C Programming Write a program in C that reads in a file, stores its contents as a character array/pointer (char*) into an unsigned character array/pointer (unsigned char* message). Note: the input file can have one line or multiple lines and vary in length
Write a program that reads and parses the CSV file, and can sort and filter data...
Write a program that reads and parses the CSV file, and can sort and filter data according to the user input and print the results. Your program should be able to do the following according to the user input: 1. Show results from a specific country 2. Sort the data according to any column 3. Filter the results (for example, >10000 cases) 4. Print the top or bottom n results You get bonus points if your program • Can do...
Write a C program that has a local and a global variable. The program uses a...
Write a C program that has a local and a global variable. The program uses a fork to create a child process. The parent process modifies both variables to be 10 and 20 and prints out the values. Then the child process modifies both variables to be 100 and 200 and prints out the values? Explain the program output?
C++ Language Write a program that reads the numbers.txt file and stores it into an array...
C++ Language Write a program that reads the numbers.txt file and stores it into an array of integers. Use the sample functions in the text and in the notes from chapter 8 to sort the array in ascending order (low to high). You can use either method (bubble or selection sort) to accomplish this. Then store the sorted array into an output file sorted Numbers.txt. There are 100 values both positive and negative integers in this file.
Write a program in c that reads the content from the file and stores each line...
Write a program in c that reads the content from the file and stores each line in an int array in heap(using dynamic memory allocation). For example, let the file has elements following (we do not know the size of files, it could be above 100,000 and contents of the file and make sure to convert file elements to int): 10067 26789 6789 3467
Write a C++ program which prompts the user to enter an integer value, stores it into...
Write a C++ program which prompts the user to enter an integer value, stores it into a variable called ‘num’, evaluates the following expressions and displays results on screen. num+5, num-3, (num+3) – 2, ((num+5)*2 / (num+3)) For performing addition and subtraction, you are allowed to use ONLY the increment and decrement operators (both prefixing and postfixing are allowed). You must remember that using increment/decrement operators changes the original value of a number. Indent your code and include comments for...
C++ Write a program that prompts the user to enter 50 integers and stores them in...
C++ Write a program that prompts the user to enter 50 integers and stores them in an array. The program then determines and outputs which numbers in the array are sum of two other array elements. If an array element is the sum of two other array elements, then for this array element, the program should output all such pairs separated by a ';'. An example of the program is shown below: list[0] = 15 is the sum of: ----------------------...
In C++ Write a program which asks the user his/ her major, stores it in a...
In C++ Write a program which asks the user his/ her major, stores it in a variable called major, then asks What is your GPA? and stores it in a variable called gpa. Next, ask a question based on the answers from the previous two questions. For example: What is your major? computer science What is your gpa? 3.52 computer science is very hard; why do you think you have a gpa of 3.52?
Write a C++ program that checks if the password is correct. The password is a 4-digit...
Write a C++ program that checks if the password is correct. The password is a 4-digit number combination. The program repeats to ask the password until the password is correct or you enter -1 to exit. Input: The password is set to ‘1123’. A user input the password to proceed, or -1 to exit. Sample Output: The program should display the following output. (The red text is a user input.) Test case 1: when you enter the correct password. Enter...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT