Question

C++ Can someone please help me with this problem- commenting each line of code so I...

C++

Can someone please help me with this problem- commenting each line of code so I can understand how to solve this problem using the C++ programming language?

I really need help understanding how to create a file for the program to read. Do I create the file in Visual basic or create a text file? I have the code, just need to know how to create the file for it to read.

#include<fstream>
#include<iostream>
using namespace std;
int main()
{
ifstream fin;
ofstream fout;
char ch;
int arr[26];
int no_of_lines=1;//declare all variables
fin.open("abc.txt");//open file for reading
fout.open("demo.txt");//open file for writing
for(int i=0;i<26;i++)//keep all arrray values zero at first you can also use memset instead of this
arr[i]=0;
while(!fin.eof())//while the file end comes
{
fin.get(ch);//read from source
fout.put(ch);//put it in output file
if(ch=='\n')
{
//if it is a new line increment this
no_of_lines++;
continue;
}
if(ch>='a'&&ch<='z')//all values are recorded as capitals
ch=ch-32;//convert it into capitals by reducing ascii value
int p=ch-65;//it will take A position 0 b as 1 etc..
if(ch>='A'&&ch<='Z')//if it is a alphabet only we wiil increment else not
arr[p]++;
  
}
cout<<"\nData written successfully..."<<endl;
cout<<"The number of lines in the input file is "<<no_of_lines<<endl;//print the information
for(int i=0;i<26;i++)
{
if(arr[i]==0)
continue;
char ch=65+i;
cout<<ch<<" appears "<<arr[i]<<" times"<<"\n";
}
fout.close();
fin.close();


}
output

0 0
Add a comment Improve this question Transcribed image text
Answer #1

This problem is related to file operations in c++, where we read data from file and after some manipulation we put it in another file.

You need to create a "abc.txt" file and write contents to it such that when you open it in read mode you can read the data from it, then you can directly write the content to "demo.txt", you dont need to create it, program wil automatically create it. In windows you can create a txt file by giving it a name and selecting the correct extensions like .txt.

Feel free to ask if you have any doubts in comment section.

HOPE YOU LIKE THE SOLUTION!!!

Add a comment
Know the answer?
Add Answer to:
C++ Can someone please help me with this problem- commenting each line of code so I...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Can someone help me put the string removee the return to an outfile and fix?? prompt: CS 575 LabEx14: no e’s Let the user specify an input file and an output file (text files). Read the input file and...

    Can someone help me put the string removee the return to an outfile and fix?? prompt: CS 575 LabEx14: no e’s Let the user specify an input file and an output file (text files). Read the input file and write the output file; the output file should be the same as the input file, except that it has no e’s. For example, if the input file had the line. Streets meet in the deep. Then the output file would have...

  • The following is a sample inventory in C++, i want to ask the user to input a item number for removing from inventory. //CPP #include <iostream> #include <fstream> #include <cstdlib>...

    The following is a sample inventory in C++, i want to ask the user to input a item number for removing from inventory. //CPP #include <iostream> #include <fstream> #include <cstdlib> #include <iomanip> #define MAX 1000 using namespace std; //Function to Add a new inventory item to the data into the array in memory void addItem(string desc[],string idNum[], float prices[], int qty[],int &num) { cout<<"Enter the names:"; cin>>desc[num]; cout<<"Enter the item number:"; cin>>idNum[num]; cout<<"Enter the price of item:"; cin>>prices[num]; cout<<"Enter the...

  • Hi there! I need to fix the errors that this code is giving me and also...

    Hi there! I need to fix the errors that this code is giving me and also I neet to make it look better. thank you! #include <iostream> #include <windows.h> #include <ctime> #include <cstdio> #include <fstream> // file stream #include <string> #include <cstring> using namespace std; const int N=10000; int *freq =new int [N]; int *duration=new int [N]; char const*filename="filename.txt"; int songLength(140); char MENU(); // SHOWS USER CHOICE void execute(const char command); void About(); void Save( int freq[],int duration[],int songLength); void...

  • In c++ please How do I get my printVector function to actually print the vector out?...

    In c++ please How do I get my printVector function to actually print the vector out? I was able to fill the vector with the text file of names, but I can't get it to print it out. Please help #include <iostream> #include <string> #include <fstream> #include <algorithm> #include <vector> using namespace std; void openifile(string filename, ifstream &ifile){    ifile.open(filename.c_str(), ios::in);    if (!ifile){ cerr<<"Error opening input file " << filename << "... Exiting Program."<<endl; exit(1); }    } void...

  • Keeping this code written as close to this as possible and only using these headers. How...

    Keeping this code written as close to this as possible and only using these headers. How do I turn this in to a code that can read from 3 files of passwords then sort them into two files. One for valid passwords and the other for invalid passwords and then send a message to the user that says how many are valid and how many are invalid. Only using std::ifstream fin; fin.open("data.txt"); fin.close(); std::ofstream fout; fout.open("data.txt"); fout.close(); #include <iostream> #include...

  • I need help fixing my code: In C++ *************** 1) I want to sum the digits...

    I need help fixing my code: In C++ *************** 1) I want to sum the digits of an n*n matrix 2) find the average I have completed the rest ****Do not use C++ standard library. You must use pointers and pointer arithmetic to represent the matrix and to navigate through it. MY CODE: (I have indicated at which point I need help) #include <iostream> using namespace std; void swap(int *xp, int *yp) { int temp = *xp; *xp = *yp;...

  • (Coding done in c++, Virtual Studio) Having a problem with the line trying to compare the...

    (Coding done in c++, Virtual Studio) Having a problem with the line trying to compare the date of birth.            **fall.sort(compare_DOB); //Here lies your error** #include <fstream> #include <iostream> #include <string> #include <list> #include <cctype> using namespace std; const char THIS_TERM[] = "fall.txt"; const string HEADER = "\nWhat do you want to do ?\n\n" "\t. Add a new friend name and DOB ? \n" "\t. Edit/Erase a friend record ? \n" "\t. Sort a Friend's record ? \n"...

  • C++ problem where should I do overflow part? in this code do not write a new...

    C++ problem where should I do overflow part? in this code do not write a new code for me please /////////////////// // this program read two number from the user // and display the sum of the number #include <iostream> #include <string> using namespace std; const int MAX_DIGITS = 10; //10 digits void input_number(char num[MAX_DIGITS]); void output_number(char num[MAX_DIGITS]); void add(char num1[MAX_DIGITS], char num2[MAX_DIGITS], char result[MAX_DIGITS], int &base); int main() { // declare the array = {'0'} char num1[MAX_DIGITS] ={'0'}; char...

  • In C++: Please help me correct this code .... All parts with (FIX ME) #include <algorithm> #include <climits&gt...

    In C++: Please help me correct this code .... All parts with (FIX ME) #include <algorithm> #include <climits> #include <iostream> #include <string> // atoi #include <time.h> #include "CSVparser.hpp" using namespace std; //============================================================================ // Global definitions visible to all methods and classes //============================================================================ const unsigned int DEFAULT_SIZE = 179; // forward declarations double strToDouble(string str, char ch); // define a structure to hold bid information struct Bid { string bidId; // unique identifier string title; string fund; double amount; Bid() {...

  • can someone help me fix this. The function that finds the minimum and maximum values of...

    can someone help me fix this. The function that finds the minimum and maximum values of the array and outputs the value and index doesnt find the maximum value of the array. #include <iostream> #include <fstream> #include<iomanip> using namespace std; void readArray(ifstream& inF, int arr[], int&ArrSize); void writeArray(ofstream & outF, int arr[], int & ArrSize); void MaxAndMin(ofstream & outF, int arr[], int & ArrSize, int &high, int &low); int main() {    int arr[100];    int i = 0;   ...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT