1

this is the error
hw4.cpp:16:41: error: cannot convert ‘std::string {aka std::basic_string}’ to ‘std::string* {aka std::basic_string}’ for argument ‘1’ to ‘std::string randpasswords(std::string)’ writepass(randpasswords(readpasswords()), readnames()); '''code'''

#include<iostream>
#include<stdlib.h>
#include<string>
#include<fstream>

using namespace std;

string readnames();
string readpasswords();
string randpasswords(string[]);
int writepass(string[], string[][2]);


int main()
{
        writepass(randpasswords(readpasswords()), readnames());

        return 0;
}

'''functions'''

string readnames()
{
        string names[100][2];
        ifstream indata;
        indata.open("employees.txt");
        int x = 0;

        while(!indata.eof())
        {
                indata >> names[x][0];
                indata >> names[x][1];
                cout << names[x][0] << " " << names[x][1]<< endl;
                x = x+1;
        }
        indata.close();
        return names[100][2];

}

string readpasswords()
{
        string pass[100];
        ifstream indata;
        indata.open("passwords.txt");
        int x = 0;

        while(!indata.eof())
        {
                indata >> pass[x];
                x = x+1;
                cout << pass[x] << endl;
        }
        indata.close();
        return pass[100];
}

string randpasswords(string pass[])
{
        string randpass[100];

        return randpass[100];
}

int writepass(string randpass[], string names[][2])
{


        return 0;
}

i am wondering why in the int main the function chain wont work

1 Answers1

0

Don't use strange types like string[][2]. Use std::vector for arrays and std::pair for a pair of std::strings. This is an example of how readnames() could be declared and implemented:

std::vector<std::pair<std::string, std::string>> readnames() {
    std::vector<std::pair<std::string, std::string>> names;
    std::ifstream indata("employees.txt");

    while (true) {    // not !indata.eof(), see https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons
        std::pair<std::string, std::string> name;
        if (!(indata >> name.first >> name.second))
            break;
        names.push_back(name);
    }

    return names;
}

The signatures of other functions might be:

std::vector<string> readpasswords();
std::vector<string> randpasswords(const std::vector<string>&);

void writepass(const std::vector<string>&, 
    const std::vector<std::pair<std::string, std::string>>&);
Evg
  • 20,870
  • 4
  • 34
  • 69