Question

In: Computer Science

C++ Assignment Hi, I need to create a program that: 1.Reads a source file (.txt) with...

C++ Assignment

Hi, I need to create a program that:

1.Reads a source file (.txt) with following information:

1,2,3,4,5

red,blue,green,yellow,orange

left, right,front, back

2. After having program read the .txt file, output the above information in categories of Symbol, Token Type, and Count :

Example:

Symbol---Token Type (data type)----Count (how many times symbol appeared in .txt file)

===========================================================================

1 ----digit ----1

2 ----digit ----1

red ----color ----1

blue ----color ----1

left ----direction ----1

right ----direction    ----1

Solutions

Expert Solution

Please give thumbs up, thaanks

#include<iostream>
#include<fstream>
#include<vector>
using namespace std;
int main()
{
   int digit[6]={0,0,0,0,0,0};
   int color[6]={0,0,0,0,0,0};
   int direction[5]={0,0,0,0,0};
   ifstream infile;
   string line;
   infile.open("filename1.txt");
   if(!infile)
   {
       cout<<"Unable to open file"<<endl;
       return 0;
   }
   string Element;
while( infile>>Element )
{
if(Element=="1")
{
   digit[1]+=1;
                   }
                   else if(Element=="2")
{
   digit[2]+=1;
                   }
                       else if(Element=="3")
{
   digit[3]+=1;
                   }
                       else if(Element=="4")
{
   digit[4]+=1;
                   }
                       else if(Element=="5")
{
   digit[5]+=1;
                   }        
                   else if(Element=="red")
{
   color[1]+=1;
                   }
                   else if(Element=="blue")
{
   color[2]+=1;
                   }
                       else if(Element=="green")
{
   color[3]+=1;
                   }
                       else if(Element=="yellow")
{
   color[4]+=1;
                   }
                       else if(Element=="orange")
{
   color[5]+=1;
                   }
                   else if(Element=="left")
{
   direction[1]+=1;
                   }
                   else if(Element=="right")
{
   direction[2]+=1;
                   }
                       else if(Element=="front")
{
   direction[3]+=1;
                   }
                       else if(Element=="back")
{
   direction[4]+=1;
                   }
}
  
for(int i=1; i<6; i++)
{
   if(digit[i]!=0)
   {
       cout<<i<<"\t"<<"digit\t"<<digit[i]<<endl;
               }
           }
for(int i=1; i<6; i++)
{
   if(color[i]!=0)
   {
                   if(i==1)
                   cout<<"red\tcolor\t";
                   else if(i==2)
                   cout<<"blue\tcolor\t";
                   else if(i==3)
                   cout<<"green\tcolor\t";
                   else if(i==4)
                   cout<<"yellow\tcolor\t";
                   else if(i==5)
                   cout<<"orange\tcolor\t";
                   cout<<color[i]<<endl;
               }
           }
           for(int i=1; i<5; i++)
{
   if(direction[i]!=0)
   {
                   if(i==1)
                   cout<<"left\tdirection\t";
                   else if(i==2)
                   cout<<"right\tdirection\t";
                   else if(i==3)
                   cout<<"frontt\tdirection\t";
                   else if(i==4)
                   cout<<"back\tdirection\t";
                   cout<<direction[i]<<endl;
               }
           }
           infile.close();
}


Related Solutions

C++ Assignment Hi, I need to create a program that: 1.Reads a source file (.txt) with...
C++ Assignment Hi, I need to create a program that: 1.Reads a source file (.txt) with following information: 1,2,3,4,5 red,blue,green,yellow,orange left, right,front, back 2. After having program read the .txt file, output the above information in categories of Symbol, Token Type, and Count : Example: Symbol---Token Type (data type)----Count (how many times symbol appeared in .txt file) =========================================================================== 1 ----digit ----1 2 ----digit ----1 red ----color ----1 blue ----color ----1 left ----direction ----1 right ----direction    ----1
C++ Assignment Hi, I need to create a program that: 1.Reads a source file (.txt) with...
C++ Assignment Hi, I need to create a program that: 1.Reads a source file (.txt) with following information: 1,2,3,4,5 red,blue,green,yellow,orange left, right,front, back 2. After having program read the .txt file, output the above information in categories of Symbol, Token Type, and Count : Example: Symbol---Token Type (data type)----Count (how many times symbol appeared in .txt file) =========================================================================== 1 ----digit ----1 2 ----digit ----1 red ----color ----1 blue ----color ----1 left ----direction ----1 right ----direction    ----1
Create a c++ program with this requirements: Create an input file using notepad ( .txt )...
Create a c++ program with this requirements: Create an input file using notepad ( .txt ) . When testing your program using different input files, you must change the filename inside your program otherwise there will be syntax errors. There are a finite number of lines to be read from the data file. But we can’t assume to know how many before the program executes; so, the standard tactic is to keep reading until you find the “End of File”...
Create a c++ program that: Create an input file using notepad ( .txt ). When testing...
Create a c++ program that: Create an input file using notepad ( .txt ). When testing your program using different input files, you must change the filename inside your program otherwise there will be syntax errors. There are a finite number of lines to be read from the data file. But we can’t assume to know how many before the program executes; so, the standard tactic is to keep reading until you find the “End of File” marker. Input date...
This is C++ Create a program that reads an HTML file and converts it to plain...
This is C++ Create a program that reads an HTML file and converts it to plain text. Console: HTML Converter Grocery List * Eggs * Milk * Butter Specifications: The HTML file named groceries.html contains these HTML tags: <h1>Grocery List</h1> <ul> <li>Eggs</li> <li>Milk</li> <li>Butter</li> </ul> When the program starts, it should read the contents of the file, remove the HTML tags, remove any spaces to the left of the tags, add asterisks (*) before the list items, and display the...
Project 6-1: Email Creator C++ code Create a program that reads a file and creates a...
Project 6-1: Email Creator C++ code Create a program that reads a file and creates a series of emails. Console Email Creator ================================================================ To:      [email protected] From:    [email protected] Subject: Deals! Hi James, We've got some great deals for you. Check our website! ================================================================ To:      [email protected] From:    [email protected] Subject: Deals! Hi Josephine, We've got some great deals for you. Check our website! ================================================================ To:      [email protected] From:    [email protected] Subject: Deals! Hi Art, We've got some great deals for you. Check our website! Specifications...
How to do in C++ HTML Converter Create a program that reads an HTML file and...
How to do in C++ HTML Converter Create a program that reads an HTML file and converts it to plain text. Console HTML Converter Grocery List * Eggs * Milk * Butter Specifications Your instructor should provide an HTML file named groceries.html that contains these HTML tags: <h1>Grocery List</h1> <ul>     <li>Eggs</li>     <li>Milk</li>     <li>Butter</li> </ul> When the program starts, it should read the contents of the file, remove the HTML tags, remove any spaces to the left of the tags, add...
Create a C Program that reads a file and replaces every other letter with an asterisk....
Create a C Program that reads a file and replaces every other letter with an asterisk. The first integer 'num' is the number of lines. Include an "Error" Message and exit the program if:    The wrong name for the input/output file is given    The input/output file can not be opened input.txt 7 Never Have We Ever Played A Game output.txt 7 N*v*r *a*e W* *v*r *l*y*d * G*m*
I am trying to create a program that reads from a csv file and finds the...
I am trying to create a program that reads from a csv file and finds the sum of total volume in liters of liquor sold from the csv file by county and print that list out by county in descending order. Currently my program runs and gives me the right answers but it is not in descending order. I tried this:     for county, volume in sorted(sums_by_volume.items(), key=lambda x: x[1], reverse=True):         index +=1         print("{}. {} {:.2f}".format(county, sums_by_volume[county]))      When I run...
(PYTHON) Write a program that does the following: reads each line from a txt file and...
(PYTHON) Write a program that does the following: reads each line from a txt file and convert it to lowercase counts the number of instances of: the characters 'a', 'e','i','o' and 'u' in the file creates a new file of file type .vowel_profile print outs lines in the file indicating the frequencies of each of these vowels Example input/output files: paragraph_from_wikipedia.txt (sample input) link: https://cs.nyu.edu/courses/fall19/CSCI-UA.0002-007/paragraph_from_wikipedia.txt paragraph_from_wikipedia.vowel_profile (sample output) link: https://cs.nyu.edu/courses/fall19/CSCI-UA.0002-007/paragraph_from_wikipedia.vowel_profile Please help!
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT