Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

help with reading char into 2-d array.

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
hi all,


I am having difficulty getting C++ to read
data into a char type 2 dimensional array from a "Data.text" file.I have work with one dimensional array before but not a two dimensional.So its new to me. The data file looks like this,
------------------------------------------------------

T1 T2 T3 T4 T5 T6 T7 T8

Gigalo, Donald 8 9 10 7 9 82 76 72
Lennon, John 6 8 9 7 8 68 74 83
Garriss, Tom 10 10 10 10 10 100 100 100
Marriss, Nick 9 8 9 8 9 90 80 90
Ivano, Erikas 6 5 4 0 0 65 58 34

-------------------------------------------------------
1.The first line from T1 all the way right to T8, I am able to get C++ to read it into char T[80]; successfully.

2. Now comes the problem. The problem I am having is getting C++ to read the last name Gigalo and all the names on that row down to Ivan and store it into a char type two dimension array.
I dont know if Im doing it right or what, but when I c-out it keeps printing out junk characters.

This is the code Im using to get C++ to read just the lastnames in and printing it out on screen.
3.What did I do wrong? please help.I have takin like a bottle of Advil now trying to alleviate my head-ache. Thnkx in advance.
-------------------------------------------------------
void main ()
{
ifstream Infile("data.txt");
char Lastname[9][10];

while(Infile)
{
for(int row=0;row<9;row++)
{
Infile.getline(Lastname[row],10,'\n');
}
}
for(int count1=0;count1<9;count1++)
{
for(int count2=0;count2<10;count2++)
{
cout<<Lastname[count1][count2];
}
}

}
------------------------------------------------
 
To get the proper name, use
Code:
Infile.getline(Lastname[row],10,',');
It's the separator that is givind you headackes, so you might want to remove it and put a tiny harmless comma instead...
Afterall, the lastnames are sparated by commas to the rest, right?
[red]Nosferatu[/red]
We are what we eat...
There's no such thing as free meal...
once stated: methane@personal.ro
 
hi all,

brb..Thanks for all your help..

I have work onn my codes and still cant get it to do what it suppose to. Im so mad at myself! Can anyone help me please..This is what It spose to do.

Write a program that
1.Read all information from the file &quot;data.txt&quot; such that all students’ scores are stored in 2-d array Scores[9][8] of type int, all last names are stored in a char array LastName[9][10], all first names are stored in a char array FirstName[9][10], and the title line is stored in a char array FirstLine[80].
-------------------------------------------------------
this is the content of data.txt file,

------------------------------------------------------

T1 T2 T3 T4 T5 T6 T7 T8

Gigalo, Donald 8 9 10 7 9 82 76 72
Lennon, John 6 8 9 7 8 68 74 83
Garriss, Tom 10 10 10 10 10 100 100 100
Marriss, Nick 9 8 9 8 9 90 80 90
Ivano, Erikas 6 5 4 0 0 65 58 34
----------------------------------------------------------
This is the code I have so far.Its not quite finish. I cant get C++ to read in the score into Score[9][8] array.Also
I am not sure if I read the LastName[9][10] and
FirstName[9][10] right either.
--------------------------------------------------
#include<iostream>
#include<fstream>
using namespace std;

void main ()
{
int Score[9][8],row,col;
char Firstline[80],LastName[9][10],FirstName[9][10];
ifstream Infile(&quot;record.txt&quot;);
//////////////////////////////////////////////Initializer block
for(int b=0;b<80;b++)
{
Firstline=' ';
}

for(row=0;row<9;row++)
{
for(col=0;col<10;col++)
{
LastName[row][col]=' ';
FirstName[row][col]=' ';

}
}

for(row=0;row<9;row++)
{
for(col=0;col<8;col++)
{
Score[row][col]=0;
}
}

///////////////////////////////////////////////Reader block

while(Infile.good())

{
Infile.get(Firstline,80,'\n');

for( row=0;row<9;row++)
{ Infile.get(LastName[row],10,',');
Infile.get(FirstName[row],10,' ');


}


}


/////////////////PRINTERs//////////////////////////////
for(int a=0;a<80;a++)
{
cout<<Firstline[a];

}

for(int d=0;d<9;d++)
{
cout<<LastName[d];
cout<<FirstName[d];

}


}

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top