-1

I am trying to create a program that reads what is in a text file and then swap places between two elements in the vector.

The text file has this order: NAME, SURNAME, REGISTRATION, _NUMBER AND CITY_ADRESS.

The list is about 100 people. I wonder if there is a way to not include the whole line and end it in a space.

Here is how my code looks like!

#include <iostream>
#include <fstream>
#include <string>
#include <vector>

using namespace std;

int main() {
  vector<string> line;
  string information;
  ifstream myfile ("Namn.txt");
  if (myfile.is_open()) {
    while (getline(myfile, information)) {
      line.push_back (information);
    }
    myfile.close();
  }else cout << "Filen gick inte att öppna!";

  for (unsigned int i = 0;  i < line.size(); i++) {
    cout << line[i] << endl;
  }
  return 0;
}
Allamo Olsson
  • 299
  • 2
  • 12
  • [`using namespace std;` is a bad practice](https://stackoverflow.com/q/1452721/2176813), never use it. – tambre Dec 03 '17 at 18:36
  • I know i dont actually use it but that is not the biggest problem i have right now :) can just change at the end. – Allamo Olsson Dec 03 '17 at 18:39
  • are all those fields on one line in the text file? if so you can either have each field on a new line then have an extra line separating two separate people, or read the whole line like you are and manually separate them into the vector. – Omid CompSCI Dec 03 '17 at 18:40
  • No they aren't on the same line in the text file, i cant either change the text file... That is my problem i have no idea how to separe them into diferent elements! – Allamo Olsson Dec 03 '17 at 18:44
  • this seems messy as you have a vector of strings and hard to determine what the actual problem is. I would create a struct or class Person and have the fields read in as member variables. Then just create a vector of Person's. It would make things so much easier – Omid CompSCI Dec 03 '17 at 18:45
  • Isnt there a way that if " " then create a new element? I mean cin would be a good one since cin only gets the first word right? but how can i make a for loop with that for my vector? – Allamo Olsson Dec 03 '17 at 18:51
  • You can show us first few lines of your input file. – abdullah Dec 03 '17 at 18:52
  • _it looks like it puts everything in the first element_ what does this mean? –  Dec 03 '17 at 18:54
  • Rune Andersson 12873645 Stockholm Peter Isaksson 12873645 Uppsala Fredrik Gustavsson 12873645 Gothenborg... I want to be able to change to Andersson Rune 12873645 Stockholm... @abdullah – Allamo Olsson Dec 03 '17 at 18:54
  • @manni66 It means that when i try to print out it pringts everything that is in my text file... like i said before if i have Rune Andersson 12873645 Stockholm i want to: myvector[0] = Rune; myvector[1] = Andersson; myvector[2] = 12873645; myvector[3] = Stockholm; – Allamo Olsson Dec 03 '17 at 18:57
  • Make sure that the line-ending sequence in your text file is the standard one for your OS. – Sneftel Dec 03 '17 at 19:24

1 Answers1

1

You can read your file with fstream and parse at the same time, then put all elements of the information in the vector of vectors and then you can mix and match bits of information anyway you like, like this:

#include <fstream>
#include <string>
#include <iostream>
#include <vector>

int main()
{
    std::string name;
    std::string surname;
    std::string registration;
    std::string location;
    std::vector<std::vector<std::string>> information;

    std::ifstream myfile("Namn.txt");

    while (myfile >> name >> surname >> registration >> location)
        information.push_back({name, surname, registration, location});

    // print

    for (auto const &info : information)
    {
        std::cout << info[1] << " " << info[0] << " " << info[2] << " " << info[3] << std::endl;
    }

    return 0;
}

Prints:

Andersson Rune 12873645 Stockholm
Isaksson Peter 12873645 Uppsala
Gustavsson Fredrik 12873645 Gothenborg
Killzone Kid
  • 6,171
  • 3
  • 17
  • 37
  • Why vector> information? and It doesnt work since the numbers are together with the surname. – Allamo Olsson Dec 03 '17 at 19:28
  • first vector is vector of lines in your file, second vector is vector of words in a line. So to access 2nd line surname you go `information[1][1]` or if registration - `information[1][2]`, etc. I thought this is what you asked, being able to swap name and surname around – Killzone Kid Dec 03 '17 at 19:32
  • @AllamoOlsson `>>It doesnt work since the numbers are together with the surname` what do you mean by this? – Killzone Kid Dec 03 '17 at 19:35
  • I just meant that the names were Anderson Rune12385402, but i already fixed this problem and thank you very much for your time! – Allamo Olsson Oct 28 '18 at 21:00