We're a community of 1.1M IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,080,439 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

taking integers from a string and saving them to a 2D array

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!

4
Contributors
5
Replies
3 Hours
Discussion Span
1 Year Ago
Last Updated
6
Views
lmytilin
Newbie Poster
18 posts since Dec 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

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).

Ancient Dragon
Achieved Level 70
Team Colleague
32,275 posts since Aug 2005
Reputation Points: 5,852
Solved Threads: 2,591
Skill Endorsements: 70
subith86
Junior Poster
127 posts since Mar 2011
Reputation Points: 30
Solved Threads: 14
Skill Endorsements: 1
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.

lmytilin
Newbie Poster
18 posts since Dec 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

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!

lmytilin
Newbie Poster
18 posts since Dec 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

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;
}
Lerner
Nearly a Posting Maven
2,408 posts since Jul 2005
Reputation Points: 739
Solved Threads: 406
Skill Endorsements: 9

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page generated in 0.0697 seconds using 2.72MB