Saturday 15 January 2011

C++: Sorting name/age pairs alphabetically, can only manage 5 pairs -



C++: Sorting name/age pairs alphabetically, can only manage 5 pairs -

sorry if post isn't best, i'm brand new site , coding in general. programs take input "sam 16 emily 4 molly 19" , sorts alphabetically, while keeping ages same. works charm, unless come in more 5 pairs. after come in 5, next names sorted fine, ages aren't right ones. i'm not sure code getting numbers it's outputting. code. apologize posting (~30 lines), don't know issue is.

#include"../std_lib_facilities.h" vector<string> name; vector<int> age; void read_pairs() { string n; int v; while (cin >> n >> v && n != "noname") { // read string int pair (int = 0; i<name.size(); ++i) name.push_back(n); age.push_back(v); } } void write_pairs(string label) { cout << label; (int = 0; i<name.size(); ++i) cout << '(' << name[i] << ',' << age[i] << ")\n"; } int find_index(const vector<string>& v, const string& n) // find n's index in v { (int = 0; i<n.size(); ++i) if (n == v[i]) homecoming i; } int main() seek { cout << "please come in name/age pairs. when finished, come in 'no more'\n"; read_pairs(); vector<string> original_names = name; // re-create names vector<int> original_ages = age; // re-create ages sort(name.begin(), name.end()); // sort names (int = 0; i<name.size(); ++i) // update ages age[i] = original_ages[find_index(original_names, name[i])]; write_pairs("\nsorted:\n"); keep_window_open("~"); } grab (runtime_error e) { cout << e.what() << '\n'; keep_window_open("~"); } grab (...) { cout << "exiting\n"; keep_window_open("~"); }

you have many problems in code. couldn't compile while.

in first place, for loop in read_pairs() preventing storing names in name. in sec place, need see if n in read_pairs() break signal before inputting age - otherwise break cin, larn later, know using bjarne stroustrup's book - same 1 learned from. in 3rd place, code breaking in find_index() because calling i < n.size(); rather i < v.size(); in for loop. finally, may want handle multiple instances of same name don't 1 age it. have implemented simple way setting value of v[i] "" in find_index() before returned position of name in array - way, cannot age name more once.

this working code, comments included. sense free inquire if don't understand something, think understand plenty figure out.

#include "stdafx.h" #include"../std_lib_facilities.h" vector<string> name; vector<int> age; void read_pairs() { string n; int v; while (true) { // read string int pair cin >> n; // input name if (n == "no") // if name "no", break not input string age break; cin >> v; // input age // insert name/age vectors name.push_back(n); age.push_back(v); } } void write_pairs(string label) { cout << label; (int = 0; i<name.size(); ++i) cout << '(' << name[i] << ',' << age[i] << ")\n"; // output name/age pairs } int find_index(vector<string>& v, const string n) // find n's index in v { (int = 0; < v.size(); i++) { if (n == v[i]) { v[i] = ""; // remove name in case 2 of same name homecoming i; } } } int main() seek { cout << "please come in name/age pairs. when finished, come in 'no more'\n"; read_pairs(); // read name/age pairs vector<string> original_names = name; // re-create names vector<int> original_ages = age; // re-create ages sort(name.begin(), name.end()); // sort names (int = 0; < name.size(); i++) // update ages age[i] = original_ages[find_index(original_names, name[i])]; write_pairs("\nsorted:\n"); // write name/age pair keep_window_open("~"); } grab (runtime_error e) { cout << e.what() << '\n'; keep_window_open("~"); } grab (...) { cout << "exiting\n"; keep_window_open("~"); }

c++

No comments:

Post a Comment