2 Replies - 607 Views - Last Post: 27 November 2016 - 05:43 AM Rate Topic: -----

#1 Totte  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 62
  • Joined: 18-January 16

Sorting names

Posted 27 November 2016 - 01:14 AM

Hello! I would like to sort some names. First sorting by the lastname and if those two are the same I would like to compare the firstname. The challenge is I cant use arrays/vector/lists. I am limited to work with strings. You guys got any ideas, because im clueless. I want to avoid doing hundreds of if/elseif/else. Thanks

#include <iostream> 
#include <string>
#include <sstream> 
using namespace std;

int main()
{

	string fullname1, name1_firstname, name1_lastname, 
	fullname2, name2_firstname, name2_lastname, 
	fullname3, name3_firstname, name3_lastname;

	cout << "Please enter the first name: ";
	getline(cin, name1);

	auto loc = fullname1.find(' ');
	name1_firstname = string(fullname1, 0, loc);
	name1_lastname = string(fullname1, loc + 1, string::npos);

	cout << "Please enter the second name: ";
	getline(cin, fullname2);

	loc = fullname2.find(' ');
	name2_firstname = string(fullname2, 0, loc);
	name2_lastname = string(fullname2, loc + 1, string::npos);

	cout << "Please enter the third name: ";
	getline(cin, fullname3);

	loc = fullname3.find(' ');
	name3_firstname = string(fullname3, 0, loc);
	name3_lastname = string(fullname3, loc + 1, string::npos);

	return 0;
}


This post has been edited by Totte: 27 November 2016 - 01:15 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Sorting names

#2 andrewsw  Icon User is online

  • the case is sol-ved
  • member icon

Reputation: 6379
  • View blog
  • Posts: 25,770
  • Joined: 12-December 12

Re: Sorting names

Posted 27 November 2016 - 02:15 AM

Question moved out of Discussion Lounge. You aren't starting a broad discussion, you are seeking specific help about some code.
Was This Post Helpful? 0
  • +
  • -

#3 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 5894
  • View blog
  • Posts: 20,122
  • Joined: 05-May 12

Re: Sorting names

Posted 27 November 2016 - 05:43 AM

Can't use vectors, arrays, or lists? Then use trees or heaps. :)

Since you only have 3 pairs of names, the number of combinations of if-else's will not be in the hundreds. If you are allowed to use functions, then you can reduce it even more.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1