0
C++ assignment help (not understanding)
Hello I need help setting up this assignment in c++ Hello, i'm a bit stuck on this array files assignment especially with the array part I'll share my code right now, I explained it clearly, Since I'm having a hard time doing it. If you can give me an example of a code in c++ strictly want arrays no vectors I'm pretty sure getline and for loop https://code.sololearn.com/crDYz30bHVpJ/?ref=app
9 Respostas
+ 1
first, you didn't share your code, you just posted your assignment.
The easiest you could do would be using an array of std::string then convert that to datatype for whatever arithmetic or any other operations you need to perform.
here is an example of getting data from the txt files in an array of some sort. Beware that this is not a complete or exact or perfect solution:
#include <iostream>
#include <fstream>
#include <string>
#include <utility>
std::string* readFiles(const std::string& filename)
{
std::ifstream ifs;
ifs.open(filename);
if (!ifs.is_open())
{
return nullptr;
}
// get file size using std::ifstream or any other way or use a number that is enough to store everything
std::string* arr = new std::string[15]; // this should be the size
//ifs.read(reinterpret_cast<char*>(&arr[0]), 15);
std::string line;
int i = 0;
while (std::getline(ifs, line) && i < 15)
{
arr[i++] = std::move(line);
}
ifs.close();
return arr;
}
int main()
{
std::string* arr = readFiles("files2.txt");
if (arr != nullptr)
{
// do stuffs
for (int i = 0; i < 15; i++)
{
std::cout << arr[i] << '\n';
}
delete[] arr;
}
return 0;
}
+ 1
I dont think I'm supposed to use vectors nor pointers since I'm at that beginner level
+ 1
you read each line from the stream and you populate the array index with that. thatâs all.
0
Flash can you explain the start where it says std::string line i'm a bit lost but this seems like the move
0
Flash, why should we use pointers, when we have containers like std::vector<>? You don't have to know size of file, and it's much easier to implement
0
boba std::string is just regular string variable, std:: tells you that it belongs to std namespace. Same about std::cout etc
0
â..want arrays no vectors..â MichaĆ Doruch
0
But can I get an explanation starting string line all the way to the end of the int main function i'm curious on how you approached this problem Flash
0
I appreciate the help Flash ill send you my code on what I got so far a little later