Question

i have a file named players.dat that has these names as their usernames and passwords //players.dat...

i have a file named players.dat that has these names as their usernames and passwords

//players.dat

mickeymouse clubhouse
goofy gawrsh
donaldduck phooey
pluto bloodhound
minniemouse polkadot

I want to read from this and input this into a vector, im new to vectors so can someone please help.

And dont use a sstream.

Homework Answers

Answer #1
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
using namespace std;

int main() {
    ifstream in("players.dat");
    if (in.is_open()) {
        string username, password;
        vector<string> users, passwords;
        while (in >> username >> password) {
            users.push_back(username);
            passwords.push_back(password);
        }
        cout << "User name vector" << endl;
        for (int i = 0; i < users.size(); ++i) {
            cout << users[i] << endl;
        }
        cout << "\nPasswords vector" << endl;
        for (int i = 0; i < passwords.size(); ++i) {
            cout << passwords[i] << endl;
        }
        in.close();
    } else {
        cout << "players.dat does not exists!" << endl;
    }
    return 0;
}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
In matlab how to make it that i can be able to type in the file...
In matlab how to make it that i can be able to type in the file that I would like to test and to be able to test what algorithm i have. This is what i have so far. But I need help fixing the rest. Please help I need this asap. function FunctionAlgorithm() disp("Welcome to ___________ algorithm") filename = file = input('Type in the file to test: '); number = input('Type the number of the algorithm you want to...
Create a file consisting a group of 3 new user name’s and passwords in powershell, 1...
Create a file consisting a group of 3 new user name’s and passwords in powershell, 1 to a line. When you read in the line you’ll have to split the name and the password. a. Write a script in powershell that does the following i. Use get-content to retrieve the names and assign them to an array ii. Write a loop that prints out each of the user names. iii. Have your script create an account using localuser for each...
How would I solve these python problems? A) File Display Download the file from here named...
How would I solve these python problems? A) File Display Download the file from here named numbers.txt . Write a program that displays all of the numbers in the file. numbers.txt contains 22 14 -99 AB B) Error Check Float Input When you want an int input you can check the input using the string isdigit() method. However, there is no comparable check for float. Write a program that asks the user to enter a float and uses a try-except...
Below is the problem that I have and here is the code that I have in...
Below is the problem that I have and here is the code that I have in c++. Can someone help me on what I am doing wrong or the correct code. A teacher has asked all her students to line up according to their first name. For example, in one class Amy will be at the front of the line, and Yolanda will be at the end. Write a program that prompts the user to enter the number of students...
Instructions​: You will find a file named findingNemo.m and another named nemo.txt. The first contains code...
Instructions​: You will find a file named findingNemo.m and another named nemo.txt. The first contains code which will read in strings from nemo.txt, then call Lab08(string) on each line of nemo.txt. Your job is to write Lab08.m. The Lab08 function will take in a string as input and will return an integer n as output. The value of n will be the nth word in the input string, where n is the location of the substring 'Nemo'. Please note that...
Description The word bank system maintains all words in a text file named words.txt. Each line...
Description The word bank system maintains all words in a text file named words.txt. Each line in the text file stores a word while all words are kept in an ascending order. You may assume that the word length is less than 20. The system should support the following three functions: Word lookup: to check whether a given word exists in the word bank. Word insertion: to insert a new word into the word bank. No insertion should be made...
So, i have this code in python that i'm running. The input file is named input2.txt...
So, i have this code in python that i'm running. The input file is named input2.txt and looks like 1.8 4.5 1.1 2.1 9.8 7.6 11.32 3.2 0.5 6.5 The output2.txt is what i'm trying to achieve but when the code runs is comes up blank The output doc is created and the code doesn't error out. it should look like this Sample Program Output 70 - 510, [semester] [year] NAME: [put your name here] PROGRAMMING ASSIGN MENT #2 Enter...
The project CreateDirectoriesDemo is included with the files for this chapter as a zipped file. rewrite...
The project CreateDirectoriesDemo is included with the files for this chapter as a zipped file. rewrite the program so that it asks the user for the location where the new directories are to be created, and then asks the user to enter, one at a time, the relative path names of the directories it should create. Amended additional details to the above abstraction of the requirements. The application should be multiplatform adaptive. This means that it should work on an...
Hello Expert, I am stuck on this assignment and can't get my code to run. I...
Hello Expert, I am stuck on this assignment and can't get my code to run. I would really appreciate it if anyone can help me. Assignment Requirements: Your software development company wants to continue developing and enhancing a software for internal use. For this week's assignment, it is required to read from a JSON application file (data.json) containing employee information. You will not use LINQ for this task. Your tasks include: Convert the previously created data.txt file into a JSON...
Imagine you're named an Economic Advisor to the president of a very poor and backward country...
Imagine you're named an Economic Advisor to the president of a very poor and backward country (Sandokistan).   The President asks your advice on where they should use their very limited resources so that they can help escape the poverty trap. Should they invest in existing, traditional tools (think tractors, computers, known manufacturing techniques) or should they invest in cutting edge research where they might become a market leader? Please explain why you selected the option you did.   Conversely, why didn't...