I'm a little lost with C++ so I could really use your help with this one! I googled it but nothing does what I want to do!
So here is my problem...
Let's say I have a file like this:
A 10 20 30 40
B 25 13 100 99
C 2 5 4 20

and I take one line at a time...

I want to create an array with A, B, C in it. I did it! But now I want to create an int array that will look like this:
10 20 30 40
25 13 100 99
2 5 4 20

How do I do it?
please help me!

Recommended Answers

All 5 Replies

Post the code how you got the first part to work. In the second part, create a 2d array of integers and read each integer one at a time using cin's >> operator (that's why I want to see your first code so that I can see how you are reading the file).

while (getline(cin,entoli))
        {
                if (entoli.find("newrect")!=(-1))
                {
                        entoli.erase(0,entoli.find("newrect")+7);
                        if ((entoli.find('.')!=(-1))||(entoli.find('-')!=(-1)))
                                cout<<"Pliktrologeiste 3ana tin entoli dinontas 8etikous akeraious apo 1 ews kai 100 ws suntetagmenes!";
                        else
                        {
                                while ((entoli[a]==' ')||(entoli[a]=='\t'))
                                {
                                        a++;
                                }
                                if (b<10)
                                {                                     !!!!!!!
                                        rectangles[b]=entoli[a];
                                        b++;
                                }

the !!!!!!! part is where I actually do it... the rest is some things I do before. I just have to do them to find the letter. don't mind the greek sentences. let's also say that rectangles are ten or less. so the 2D array has to be [10][4]. It's 4 because I will be given 4 numbers for it's rectangle.

could someone at least so me how to do it with structs because I'm a beginner? it's not homework or anything... I'm just trying to learn!

What are you trying to do with find()? There is no decimal in the sample input. If numerical values are restricted to positive ints, as posted, there is no dash either.

Read in an entire line at once using cin and use a stringstream object to break out the pieces and then store them however you want to:

string entoli;
const int MAXLETTERS]
char letters[MAXLETTERS];
const int MAXROW = 2;
const int MAXCOL = 2;
int numbers[MAXROW][MAXCOL];
int index = 0;

getline(cin, entoli);  //input is A 10 20 30 40 and is stored as STL string object
stringstream ss(entoli); //create a stringstream object using the input

ss >> letters[index++]; //obtain the letter

//get the numbers
for(int i = 0; i < MAXROW; ++i)
 for(int j = 0; j < MAXCOL; ++j)
   ss >> numbers[i][j];

Alternatively, read in each piece separately, in this example from a file called myFile using an ifstream. Assumes file is predictably delineated, in the case posted, using whitespace, and each line has 1 char and 4 ints per line;

int i = 0;
int j;
const int MAXLETTERS = 4;
const int MAXROW = 4;
const int MAXCOL = 4;
int i= 0;
char letters[MAXLETTERS];
int numbers[MAXROW][MAXCOL];
ifstream fin("myFile");

while(i < MAXROW)
{
  while (fin >> letter[i])
  {
    for(j = 0; j < MAXCOL; ++j)
      fin >> numbers[i][j];
  }
  ++i;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.