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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need help..Dont know what to do. 1

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
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 "data.txt" 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];

}


}


 
To read the numbers, you'll need to use a temporary string variable, then use atoi() to convert the string to an int and store it in the array. Other than that, I think you're on the right track.
 
Hi C-saw,

Since you didn't take much note of my previous suggestions I'm not sure how much point there is to this, however I'll give it another go. There's no need for atoi() or whatever, that's the easy part, you can read the numeric values with &quot;>>&quot;. The strings are the problem. It's easy to read a c-string into a char[n] with &quot;>>&quot; but you get a zero at the end (that's a standard c-string) and I think what you want is the array entry padded out with blanks. I'd suggest using a little helper function which just fills in from the end of the data you read in. Then you don't need to init everything first. It's also better to read into a temporary because you'll get an extra char (the zero) at the end of the data you read in with &quot;>>&quot;.

By the way, it's really not a bad idea to structure programs with a few functions (at least) rather than writing one single long routine. It makes things clearer for you and anyone else who hast to look at yout code. Having a constant or two for values like size of an array is also not such a bad idea if you ever want to change these values. Then you just need to change the constant. Think of changing all those 9s in your code.

So here's some code that I think does what you want:

#include &quot;stdafx.h&quot;
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;

const int rows = 9;
const cols = 8;

// print l chars
void prtca(char* a, int l)
{
for (int i=0; i<l; i++) cout << *(a+i);
}

// Output one detail line
void prtline(char* LastName, char * FirstName, int Marks[cols])
{
prtca(LastName,10);
prtca(FirstName,10);
for(int i=0;i<cols;i++) cout << setw(5) << Marks ;
cout.width(0);
cout << endl;
}


// Fill end of array with blanks
void fill(char* ps, char* pe)
{
while(ps!=pe) *ps++ = ' ';
}

int main(int argc, char* argv[])
{
ifstream Infile(&quot;c:\\xyz.txt&quot;);
int Score[rows][cols];
char Firstline[80];
char LastName[rows][10];
char FirstName[rows][10];

int row = -1;
if(Infile)
{
// Read header line
Infile.getline(Firstline,80);
fill(Firstline+strlen(Firstline),Firstline+80);
// Loop process detail lines
while(Infile.good())
{
if (++row==rows) break;
char t1[20]; // Temp

// read LastName
Infile >> t1;
fill(t1+strlen(t1)-1,t1+20);
strncpy(LastName[row],t1,10);

// read FirstName
Infile >> t1;
fill(t1+strlen(t1),t1+20);
strncpy(FirstName[row],t1,10);

// Read Score values
for(int i=0;i<cols;i++) Infile >> Score[row];
}

// Print it all out (&quot;row&quot; = actual lines read)
prtca(Firstline,80);
cout << endl;
for(int i=0;i<row;i++) prtline(LastName,FirstName,Score);
}
else
MessageBox(NULL,&quot;Fehler!&quot;,&quot;Fehler&quot;,0);

return 0;

}

fill() puts those extra spaces in; note that for FirstName I use strlen()-1 to remove the comma at the end. I added a couple of routines for printing the results. You need <iomanip> for setw() to get the field widths right. Note that your header line needs editing slightly to make it line up correctly.
 
HI dave,
Have you ever know that you're my hero?
Thank you so much! I didnt really expect you to go all the way and write the whole program. But it really help me understand how it works and what I did wrong. I really,
sinerely appreciate your help. I have followed many suggestions that people gave me on this board and by email.
But It really didnt help me to understand it because I dont get to see details and how their suggestions work.First of all IM thankful for the suggestions that I receive from everyone but for example,StukA suggest that I use Atoi(). But if someone doesnt know how to use it, its just going to lead to more confusion.A brief example may help.
Anyway thankx Davestip!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top