c++ - Program Not Outputting Correctly Depending on Standard Input -
I am trying to complete the following:
- Token stream from standard input Reads (using token oriented input)
- Choosing a zipcode, which is assumed to be 5 characters before the token "zip:".
- The program prints the top 10 most frequently zipcode and their frequency in descending order of frequency
My code is being compiled correctly when my standard input looks: zip: 22333 zip: 23423 zip: 22333 etc.
However, we were given some sample code that should have worked for
city: Sherwood park zip: T8C 1C3 $ 20.00
When ordering frequency using frequency code instead of zip in order of frequency, instead of using the sample code, 3 0 0 0 0 0 0 0 (for some reason there is a sign of counting 3 and then no other calculation).
I know that there is an inconsistency between directions and file zip code, meaning files are stored like zip: 39450 with a white space between zip and number. How can I modify it inside of the account (! Cin.fail ()) for it and count pincode a white space away from using "zip:" token oriented input? Note: There are some non-US zip codes which we should not count.
Thank you!
#include & lt; Iostream & gt; # Include & lt; Fstream & gt; #include & lt; String & gt; using namespace std; Int main () {int i = 0, n = 0, l = 0, c = 0, temp2, flag = 1; String zipsstring, zipsstringf, temp1; Strings string [10000]; Int counters [10000] = {0}; Cin & gt; & Gt; Zipsstring; While (cin.get () & amp;; cin.fail ()) {if (zipsstring.find ("zip:", n! = Std :: string :: npos)) {zipsstringf = zipsstring.substr (4, 5); {If (strray [i] == zipsstringf) {countarray [i] + = 1 for (int i = 0; i & lt; = n; i ++) C = 1; break; }} If (c == 0) {stringarray [n] = zipsstringf; Countarray [n] + = 1; N ++; }} Cin & gt; & Gt; Zipsstring; } (Int i = 0; i & lt; n & amp; amp; flag; i ++) {flag = 0; (Int j = 0; j.m. lt; n-1; j ++) for (if (counters [j + 1]> counter ray [j]) {temp1 = stringarray [j]; temp2 = countarray [j ]; Stringarray [j] = stringarray [j + 1]; countarray [j] = countarray [j + 1]; stringarray [j + 1] = temp1; countarray [j + 1] = temp2; flag = 1;}} } (Int x = 0; x & lt; 10; x ++) {cout & lt; stranre [x] & lt; "" & lt; & lt; counterreal [x] & lt; Lt;
If you use this real (The use of the right tools instead of the homework assignment is forbidden) I have a r Egex library:
std :: regex w ("[zz] ip: [\ t \ n] * [0-9] {5}"); while (std :: getline (std :: cin, line)} {std :: copy (std :: sregex_token_iterator (line.begin (), line.end (), w), std :: sregex_token_iterator (), std:: Ostream_iterator & Lt; std :: string & gt; (std :: cout, "\ n");}
Of course, with this it becomes quite clear that what you are doing It's basically nothing more than a special invitation for grep
, but it is sometimes life.
Comments
Post a Comment