I have written a program that determines the frequency of letters in an input file.
It works perfectly but the only problem is that the output file should put the characters in the order that they appear but they are being put in alphabetical order.
Please help.
Here is the code:
#include <fstream>
#include <iostream>
using namespace std;
int main()
{
ofstream out;
out.open("output.txt");
ifstream input("input.txt", ios_base::binary);
size_t count[256];
fill_n(count, 256, 0);
for (char c; input.get(c); ++count[uint8_t(c)])
;
for (size_t i = 0; i < 256; ++i)
{
if (count[i] && isgraph(i))
{
out << char(i) << " " << count[i] << '\n';
}
}
out.close();
}
as it is 256 char and it will have intial value of 0 so always it will start with A as asci value of A is less so it will be in sorted order, so overcome this have used vector of pairs that track char and count
#include <fstream>
#include <vector>
#include <iostream>
using namespace std;
int main()
{
ofstream out;
out.open("output.txt");
ifstream input("input.txt", ios_base::binary);
// vecor of pairs
vector<pair<char, int>> char_map;
char ch;
while (input >> noskipws >> ch)
{
// if valid value
if (isgraph(ch))
{
bool found=false;
// finding in vector if already present
for (pair<char, int> &it:char_map)
{
// if present increasing the count
if (it.first == ch)
{
it.second += 1;
found = true;
break;
}
}
// if first time then pusing the count as 1
if(!found){
pair<char, int> t;
t.first = ch;
t.second = 1;
char_map.push_back(t);
}
}
}
// writing to file
for (pair<char, int> &it:char_map)
{
out<<it.first<<" "<<it.second<<endl;
}
out.close();
}
//SAMPLE FILE input.txt
Fredda
Sulema
Lynette
Maire
Alayna
Wanda
Celia
Long
Genia
Howard
Isadora
Hugh
Jamie
Alethea
Carola
Shea
//OUTPUT output.txt
F 1
r 5
e 11
d 5
a 17
S 2
u 2
l 5
m 2
L 2
y 2
n 5
t 3
M 1
i 4
A 2
W 1
C 2
o 4
g 2
G 1
H 2
w 1
I 1
s 1
h 3
J 1
Please do comment if u have any concern
I have written a program that determines the frequency of letters in an input file. It...
I need help debugging this C++ prgram. What Am i doing wrong? //******************************************************** // This program reads two input files whose lines are //ordered by a key data field. This program should merge //these two files, writing an output file that contains //all lines from both files ordered by the same key field. // //********************************************************* #include <iostream> #include<string> #include<fstream> //prototype void mergeTwoFiles (ifstream&,ifstream&, ofstream&); using namespace std; int main() {string inFile1,inFile2,outFile; // input and output files ifstream in1; ifstream in2;...
C++ Programming question For this exercise, you will receive two string inputs, which are the names of the text files. You need to check if the input file exists. If input file doesn't exist, print out "Input file does not exist." You are required to create 4 functions: void removeSpace (ifstream &inStream, ofstream &outStream): removes white space from a text file and write to a new file. If write is successful, print out "Text with no white space is successfully...
The input file is already there. the file is too long, don't
have to send it.
using virtualbox ubuntu
We want to create a command line terminal where user can run the
snap commands with three different options: -thanos, -ironman and
-holk. The terminal must keep listening to user command.
If the user type:
snap -thanos: all the text line in the text file “input.txt”
that does not contain the word “thanos” must be randomly removed.
The remaining text...
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...
1. What would be the output of the following lines of code: int num = 2; switch(num){ case 1: cout << "1"; case 2: cout << "2"; case 3: cout << "3"; case 4: cout << "4"; } 2. What would be the output of the following code: char op = '*'; switch(op){ case '+': cout << "Addition"; break; case '-': cout << "Subtraction"; break; case '/': cout << "Division"; break; default: cout << "Invalid"; } 3. What would be...
can you please split this program into .h and .cpp file #include <iostream> #include<string> #include<fstream> #define SIZE 100 using namespace std; //declare struct struct word_block { std::string word; int count; }; int getIndex(word_block arr[], int n, string s); int main(int argc, char **argv) { string filename="input.txt"; //declare array of struct word_block word_block arr[SIZE]; int count = 0; if (argc < 2) { cout << "Usage: " << argv[0] << "...
C++
language
utput after the code executes on that data set. Assume the input file opens successfully. #include <fstream> #include «iostream> using namespace std; int main ) int n = 0; int m-0; double d 0.0; char c = '*' , ifstream fin; fin.open ("data.txt"): fin >>n >> m>>c>>d; return 0; 7. Data set: 57 2.5 Output Data set: 5 2.57 Output: Data set: 5 7 2.5 Output . Data set: 57 2.5 * Output:
Header file:
Main file:
Test file:
Need help operator overloading << and >> for my
program. My program reads from a text file, converts it to binary,
ones complement and reverses the number and stores them in a new
file called encrypt1.txt. Then decrypts it back to original txt in
a file decrypt1.txt. Thanks
#includeiost ream» #include<fstream> #include«st ring> using namespace std; class encrypt_decryptt public: encrypt_decrypt); int getascii(char); void convert_binary_flip(int); void reverse bits); void decrypt_ones(); void decrypt_reverse(bitset<8>); string getvalue(); void...
Am I getting this error because i declared 'n' as an int, and then asking it to make it a double? This is the coude: #include "stdafx.h" #include <iostream> #include <fstream> #include <string> #include <algorithm> #include <vector> using namespace std; void sort(double grades[], int size); char calGrade(double); int main() { int n; double avg, sum = 0;; string in_file, out_file; cout << "Please enter the name of the input file: "; cin >> in_file; ...
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...