d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > C++ Help Getting Names From File And Sorting Them
Add Reply New Topic New Poll
Member
Posts: 19,256
Joined: Mar 24 2008
Gold: 710.00
Oct 26 2014 11:26am
I need help, understanding the logic of this program. I need to extract the names of the Vendors, and then sort them alphabetically ( judging by the first letter of their last name). Basically how would i do this?

My intuition is telling me to open the file, and use the getline function, to take line by line. One i have the first line, i use an if statement to test if string[0] == V
( to identify if the line is either a vendor, day or product)


Heres where i get lost :/
After i understand that the line is a Vendor, how would i get the vendors names ( i assume using some function to get the series of characters after the string[8] ( so the line can just read the name, and not the file format "Vendor: ") to the end of the line, then save it as a new string?)
Then somehow compare the first letter of that string and alphabetically sort them .



The Vendors names are going to different, depending on which file i open.
any help would be appreciated.

Vendor: Smith Levi
Product: Dank Cookies
Day01: 1
Day02: 5
Day03: 6
Day04: 14

Vendor: Acorn Jim
Product: slim jims
Day01: 5
Day02: 7
Day03: 1
Day04: 6


Vendor: Quillberstienersmitdh Sarah
Product: Paper
Day01: 6
Day02: 5
Day03: 8
Day04: 1

This post was edited by Pino38 on Oct 26 2014 11:42am
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Oct 26 2014 01:55pm
You're very close. Look up the functions strncmp() or strstr() and strtok().

http://www.cplusplus.com/reference/cstring/strtok/
http://www.cplusplus.com/reference/cstring/strncmp/
http://www.cplusplus.com/reference/cstring/strstr/

strncmp() can compare one string to another limiting the length of searched characters, so you can search for "vendor" within your getline string.

strstr() searches the string for your string and returns a pointer of the first occurrence. This can be used in logic that if(strstr(...) != null) { //process vender string }

The reason behind using these functions would be if the text document is malformed 'V' might not be vender. strncmp() would be the better of the two string searching functions to use as strstr() matches against your string anywhere inside the line where as strncmp() starts at the beginning and ends when you specify.

strtok() takes the string and splits it based on delimiter. You simply will want to split the vender string by ' ' (space) and store it into an array in which you can then parse array[2] (the last name).

This post was edited by AbDuCt on Oct 26 2014 01:57pm
Member
Posts: 19,256
Joined: Mar 24 2008
Gold: 710.00
Oct 26 2014 06:20pm
Code
string getname ( int*** array3d, string vendor1, string vendor2, string vendor3, string vendor4)
{

ifstream entrada("entrada.txt");
string aline;




while( getline(entrada,aline))
{

if (aline[0] == 'V' || aline[0] == 'v') // get the names and puts them into a string
{
if ( aline[7] == '1')
{
cout << "\n Vendor number 1 should be ";
vendor1 = aline.substr(9);
cout <<vendor1;
cout << "\n";
}
else if (aline[7]== '2')
{
cout << " Vendor number 2 is";
vendor2 =aline.substr(9);
cout << vendor2;
cout << " \n";
}
else if (aline[7]== '3')
{
cout << " vendor number 3 is";
vendor3= aline.substr(9);
cout << vendor3 ;
cout << "\n";
}
else if ( aline[7] == '4')
{
cout << "Vendor number 4 is ";
vendor4 = aline.substr(9);
cout << vendor4;
cout << "\n";
}

}
}

entrada.close();

return vendor1,vendor2,vendor3,vendor4;}



I managed to go use this function to get the names of the vendors and assigned them to the strings vendor1,vendor2,vendor3,vendor4

but for some reason the function does not want to return the string names that are now vendor1-4.

How can i get this function to work :/
also ignore that fact that i am passing a 3d array into the function xD, i got to edit that out.
also pretend the text i posted in the first post, has 1 more vendor added to it.
Member
Posts: 19,256
Joined: Mar 24 2008
Gold: 710.00
Oct 26 2014 07:45pm
Quote (AbDuCt @ Oct 26 2014 03:55pm)
You're very close. Look up the functions strncmp() or strstr() and strtok().

http://www.cplusplus.com/reference/cstring/strtok/
http://www.cplusplus.com/reference/cstring/strncmp/
http://www.cplusplus.com/reference/cstring/strstr/

strncmp() can compare one string to another limiting the length of searched characters, so you can search for "vendor" within your getline string.

strstr() searches the string for your string and returns a pointer of the first occurrence. This can be used in logic that if(strstr(...) != null) { //process vender string }

The reason behind using these functions would be if the text document is malformed 'V' might not be vender. strncmp() would be the better of the two string searching functions to use as strstr() matches against your string anywhere inside the line where as strncmp() starts at the beginning and ends when you specify.

strtok() takes the string and splits it based on delimiter. You simply will want to split the vender string by ' ' (space) and store it into an array in which you can then parse array[2] (the last name).


Thanks for the info bro! i have some pseudo code for using the strncomp function, just now debating on how to compare the values returned from that function in order to alphabetize it.
not sure if i should do a shitload of if else's or what xD
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Oct 26 2014 07:50pm
Quote (Pino38 @ Oct 26 2014 09:45pm)
Thanks for the info bro!  i have some pseudo code for using the strncomp function, just now debating on how to compare the  values returned from that function in order to alphabetize it.
not sure if i should do a shitload of if else's or what xD


are you not allowed to use a build in sort?
Member
Posts: 19,256
Joined: Mar 24 2008
Gold: 710.00
Oct 27 2014 10:03am

i can use whatever function, but im new to programming so i dont have knowledge on all of these other functions and libraries.
Does that function work with multiple strings? Like in my program i got multiple strings that each start with a different vendors name.
ex String1 = Smith Levi
string 2 = Lewis Roy
string 3 = Appleseed Johnny

and i need to compare the first letter of each string ( S-mith Levi, L-ewis Roy....etc) and depending on that then send the out put to display the names alphabetically

like the output has to be
AppleSeed johnny ...
Lewis roy...
Smith Levi....

and if i decide to change the names in the file that i am getting the strings from, how would i then display the output based on the new strings entered?
like i can manually just use cout the the first one appleseed, and do the calculation in my head, but i want the program to do it , instead of changing the code each time depending on the file.

how would this code look? like a template

This post was edited by Pino38 on Oct 27 2014 10:05am
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll