473,327 Members | 2,090 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

Need help on modifying an array to vectors

Hi how are you guys? I just wanted to ask a favor if you could find the syntax error and tell me what I am doing wrong. My prof. just taught the class about Vectors and she wanted us to modify an array program into a vector.

The array program I made includes a sort function that sorts an array of strings in alphabetical order.


Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4.  
  5. using namespace std;
  6.  
  7. void sort(string cookie[], int n);
  8. void print(string cookie[], int n);
  9.  
  10.  
  11. int main()
  12. {
  13. int rows=0;
  14. char filename[30];
  15. string names[100];
  16.  
  17.  
  18. ifstream alphabet;
  19. alphabet.open ("letters.txt");
  20.  
  21. if (alphabet.fail())
  22. {
  23. cerr << "Error opening input file." << endl;
  24. exit(1);
  25. }
  26.  
  27. while (getline(alphabet,names[rows])) 
  28. {
  29. rows++;
  30. }
  31.  
  32. sort(names,rows);
  33. print(names,rows);
  34.  
  35.  
  36. alphabet.close();
  37. return 0;
  38. }
  39.  
  40. void sort(string cookie[], int n) 
  41. {
  42. int i, j;
  43. string temp;
  44.  
  45. for(i=1; i<n; i++) 
  46. {
  47. temp = cookie[i];
  48. for(j=i; j>=1 && (temp < cookie[j-1]); j--) 
  49. {
  50. cookie[j] = cookie[j-1];
  51. cookie[j-1] = temp;
  52. }
  53. }
  54. }
  55.  
  56. void print(string cookie[], int n) 
  57. {
  58. for(int i=0; i<n; i++) 
  59. {
  60. cout << cookie[i] << endl;
  61. }
  62. }
  63.  
And this is my version of trying to do it in a vector:

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <vector>
  5.  
  6. using namespace std;
  7.  
  8. void sort(string cookie, int n);
  9. void print(string cookie, int n);
  10. vector<char>filename;
  11. vector<char>names;
  12.  
  13. int main()
  14. {
  15. int rows=0;
  16. string cookie;
  17.  
  18.  
  19. ifstream alphabet;
  20. alphabet.open ("letters.txt");
  21.  
  22. if (alphabet.fail())
  23. {
  24. cerr << "Error opening input file." << endl;
  25. exit(1);
  26. }
  27.  
  28. while (getline(alphabet,names[rows])) 
  29. {
  30. rows++;
  31. }
  32.  
  33. sort(cookie,rows);
  34. print(cookie,rows);
  35.  
  36.  
  37. alphabet.close();
  38. return 0;
  39. }
  40.  
  41. void sort(string cookie, int n) 
  42. {
  43. unsigned int i, j;
  44. unsigned char temp;
  45. unsigned int cookiesize = cookie.size()-1;
  46.  
  47. for(i = 0; i <= cookiesize; i++)
  48. {
  49. names.push_back(cookie[i]);
  50. }
  51.  
  52. for(i=1; i<names.size()-1; i++) 
  53. {
  54. unsigned int totalsize = names.size()-1;
  55.  
  56. temp = cookie[i];
  57. for(j=i; (j>=1 && temp < cookie[j-1]); j--) 
  58. {
  59. cookie[j] = cookie[j-1];
  60. cookie[j-1] = temp;
  61. }
  62. }
  63. }
  64.  
  65. void print(string cookie[], int n) 
  66. {
  67. for(int i=0; i<n; i++) 
  68. {
  69. cout << cookie[i] << endl;
  70. }
  71. }
  72.  
  73.  
Mar 3 '13 #1
4 1325
weaknessforcats
9,208 Expert Mod 8TB
A vector is an array. In your case a vector<string>.

Use vector::push_back() to add your strings to the vector.

Use the std::sort() to sort your strings by passing in iterators set to vector::begin() and vector::end().

To print, run your loop from vector::begin() to
!vector::end(). cout << will work with string::c_str().

That's about it.
Mar 3 '13 #2
divideby0
131 128KB
The functions should also be changed to take the vector as an argument and not the string.

Expand|Select|Wrap|Line Numbers
  1. void mysort(vector<string> &); 
  2. void myprint(const vector<string> &);
  3.  
Perhaps wfc can clarify, but I *think* you'll need to change the file read too. I don't believe you can use an empty vector the same way as you're doing with the string array in getline.

Expand|Select|Wrap|Line Numbers
  1. vector<string> names;
  2. string fbuf;
  3.  
  4. while(getline(alphabet, fbuf))
  5.     names.push_back(fbuf);
  6.  
Mar 3 '13 #3
I am not that good with C++ and my professor just taught us vectors like two days ago so I am still confuse about the syntax that goes with it ..
Mar 4 '13 #4
weaknessforcats
9,208 Expert Mod 8TB
Use the Internet. Seach on vector+ C++ and you will find example code.

This web site will help you with your code but teaching and code writing are outside the scope ofthe site.
Mar 4 '13 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: JackM | last post by:
Let me attempt to explain my problem. I have a crude php script that takes a text list of songs that was generated by an mp3 list program and translates each entry into the form where they can be...
5
by: mkarja | last post by:
Hi, I've got problems with 2D array. I have a program that gets results from sql server 2000 and puts the results in an std::string. Here's a few example lines of code to help explain my...
9
by: matthurne | last post by:
I need to send just an array to a function which then needs to go through each element in the array. I tried using sizeof(array) / sizeof(array) but since the array is passed into the function,...
10
by: mahurshi | last post by:
I've got a gate structure that looks like this /* Defining sGATE structure */ struct sGATE { string name; vector<int> input; int output; };
1
by: Jim H | last post by:
I am on a project where I am supposed to send an XML document to a SQL Server stored procedure. The XML Doc is a list strings. If in my c# function I get a list values as (string psValueList),...
3
by: Jack Addington | last post by:
Quite new to C# but I am getting quite confused with Array's and ArrayLists. This is probably the same old iteration of a basic question but I can't seem to find a clear answer/example of this...
3
by: Terry Olsen | last post by:
I want to have an array of class objects that raise an event when a condition is true. Like so... ------------------------------------------------------- Public Class ClientHandler Public...
13
by: James | last post by:
Is this possible? I want to pass an array into a function that contains txtBox.Text properties... I was thinking something like this, but I know it won't work Dim vendorFields(9) As String ...
2
by: notaspdotnet | last post by:
I have a SQL table called "EXAMPLE" with 2 columns; personId and reportDate. personId consists of a 36 character GUID. reportDate has a date such as 1/1/2005, 2/1/2005,3/1/2005, etc. The date is...
3
by: krunk | last post by:
This is my code. I am to read in numbers from a file which is my nums.txt. Then we are to have the user choose what row or colum they would like to work with. I have two different functions for...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.