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 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 an assembly language program that corresponds to the following C program ****Please give correct answer...
Write an assembly language program that corresponds to the following C program ****Please give correct answer using Pep/9 machine**** int num1; int num2; ;int main () { scanf("%d", &num1); num2 = -num1; printf("num1 = %d\n", num1); printf("num2 = %d\n", num2); return 0; }
Write a program in c# that declare a variable and store user name jjohn in. Then,...
Write a program in c# that declare a variable and store user name jjohn in. Then, write a statement that will make your program display at the screen the content of that variable, followed by " I would like to know your height in centimeter. Please enter it:". Then, write a statement that will store the value entered by the user, allowing decimal numbers ie some precision, a fraction part. Finally, write statements (more than one can be needed) so...
Using C++, write a code that this program always stores text file output into a text...
Using C++, write a code that this program always stores text file output into a text file named "clean.txt". -The program should read one character at a time from "someNumbers.txt", and do the following. -If it is a letter, print that letter to the screen, AND also store it in the text file. All letters should be converted to lowercase beforehand. -If it is a number, print that number to screen, but do NOT store it in the text file....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT