Any one?

If you have a numbers in columns and rows, and you know just the number of rows it is 4, so how you need to find out every single column and write those numbers to array? I found the way just with string and then get line, but I don't know what to do next.. Any one could help?

3 15 360 8
5 2 1 145 99 300
4 700 600 900 800
2 8 1
First make sure that you're using the string overload for "getline()". Once you have the data loaded into a string, break it up with the "std::string.find()" and "std::string.substr()" member functions:

 
std::string Sub_String = Data_String.substr(Your_Data_String.find(" ", 0), std::string::npos);


Then if you need to use the numerical data convert it with one of the "strto*()" functions in the 'cstdlib' header file. Or string streams, personally I would use one of the functions from the 'cstdlib' header. That part is dealers choice.
Could you show me how the code should look like? Coz I have no idead what did you told me...
Last edited on
#include <fstream>
#include <iomanip>
#include <iostream>
#include <cmath>
#include <string>
#include <sstream>

using namespace std;

const char CDfv[] = "Duom.txt";
const char CRfv[] = "Result.txt";
const int CMax = 15;
//------------------------------------------------------------------------------

void skaityti (int & p, int & n, int kaina[CMax]);
void spausdinti (int p, int n, int kaina[CMax]);

//------------------------------------------------------------------------------
using namespace std;

int main()
{

int p;
int n;
int kaina[CMax];

skaityti (p, n, kaina);
//spausdinti (p, n , kaina);

return 0;
}

void skaityti (int & p, int & n, int kaina[CMax])
{

ifstream fd(CDfv);

fd >> p >> n;
string line;

int a = 0;
int b = 0;
int x = 0;

while (getline(fd,line))
{
cout << line << endl;
}

}
That's my code
What does the data source look like? It doesn't have to be in English, but meaningful names for your variables would be helpful. Lithuanian isn't exactly a dead language and is easy enough to Google.
Last edited on
#include <fstream>
#include <iomanip>
#include <iostream>
#include <cmath>
#include <string>
#include <sstream>

using namespace std;

const char CDfv[] = "Duom.txt";
const char CRfv[] = "Result.txt";
const int CMax = 15;
//------------------------------------------------------------------------------

void read(int & p, int & n, int price[CMax]);


//------------------------------------------------------------------------------
using namespace std;

int main()
{

int p;
int n;
int price[CMax];

read(p, n, price);


return 0;
}

void read(int & p, int & n, int price[CMax])
{

ifstream fd(CDfv);

string line;

while (getline(fd,line))
{
cout << line << endl;
}

}
Basicly it's doesn't matter what they mean, I just need to find out how to read files to array's and then use them. That's it, sound's easy, but its not :D
Last edited on
giblit did give a solution on your previous thread with this same topic: http://www.cplusplus.com/forum/beginner/141223/

Lets paste it here, except this time with a fixed number of rows (4), even though your previous thread did claim that you do read the number of rows from the input and therefore we cannot possibly know the number of rows ahead:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
const int Rows { 4 };
std::vector<int> data [Rows];
for ( int row = 0; row < Rows; ++row )
{
    std::vector<int> tempData;
    std::string str;
    std::getline( in, str );
    std::istringstream ss(str);
    int temp;
    while ( ss >> temp )
    {
        tempData.push_back( temp );
    }
    data[row] = tempData;
}
What does the "std::" means? And why int temp is not declared?
Last edited on
C++ standard library types and functions are in scope of namespace std. Namespace is like an area code in phone number.

Line 9 declares that identifier "temp" refers to an integer variable. I.e. that is the declaration.
Ok. And how to get printed answer? I still can't understand this code...
What do you mean by "printed answer"? You did ask "how to read and store numbers?". The code in the example does exactly that.

You have not told why you need to read the numbers.
Thank you! sorted
Topic archived. No new replies allowed.