Reputation: 261
In my C++ project, I have 3 files which are main.cpp, functions.cpp and functions.h.
functions.cpp:
#include <functions.h>
using namespace std;
int ascii(string text)
{
vector<int> tab;
for( int i=0; i<text.length(); i++ )
{
char x = text.at(i);
tab.push_back(x);
}
for(int k=0; k<tab.size(); k++)
{
cout << tab[k] << endl;
}
}
functions.h:
#ifndef FUNCTIONS_H_INCLUDED
#define FUNCTIONS_H_INCLUDED
#include <iostream>
#include <string>
#include <vector>
int ascii(std::string text);
#endif // FUNCTIONS_H_INCLUDED
main.cpp:
#include <functions.h>
using namespace std;
int main()
{
string plaintext;
cout << "Enter text : ";
ws(cin);
getline(cin, plaintext);
ascii(plaintext);
return 0;
}
As you can see, the value are stored in an array in the functions.cpp file.
How do I "move" the array from functions.cpp to main.cpp so I can manipulate these data?
Upvotes: 0
Views: 57
Reputation:
One way is to do following
functions.cpp
using namespace std;
vector<int> ascii(string text) // original: int ascii(string text)
{
vector<int> tab;
for( int i=0; i<text.length(); i++ )
{
char x = text.at(i);
tab.push_back(x);
}
for(int k=0; k<tab.size(); k++)
{
cout << tab[k] << endl;
}
return tab; // original: no return
}
functions.h
#ifndef FUNCTIONS_H_INCLUDED
#define FUNCTIONS_H_INCLUDED
#include <iostream>
#include <string>
#include <vector>
std::vector<int> ascii(std::string text); // original int ascii(std::string text)
#endif // FUNCTIONS_H_INCLUDED
main.cpp
#include <functions.h>
using namespace std;
int main()
{
string plaintext;
cout << "Enter text : ";
ws(cin);
getline(cin, plaintext);
vector<int> returning = ascii(plaintext); // original: ascii(plaintext)
return 0;
}
Upvotes: 1