i need arrays boss @_@

topic posted Tue, June 28, 2005 - 2:37 PM by  Phantom
Share/Save/Bookmark
Advertisement
simply put my program is suppose to be able to input 20 names and ages..then when it compiles it suppose to list the names in alphabetical order. Now I did something like this but it wasnt woth names it was with numbers. Then the program is suppose to save on a txt file.I used the same exact program that i did in class. I hardly changed the data. I compile it..it says "linker error." Also is my code correct for putting characters in alphabetical order...the internet wasnt much help with that. Anyone help me out..here's my code...

#include<iostream.h>
#include<string.h>
#include<iomanip.h>
#include<fstream.h>
void main()
{
int sku[10];
char description[10][25];

//prototypes
int input ( int [ ], char[ ][25]);
void report( int [ ], char[ ][25]);
void savedata( int [ ],char[ ][25]);
void sort( int [ ], char[ ][25]);


//functions
input( sku, description);
sort( sku, description);
report( sku, description);
savedata( sku, description);
}// close main

int input( int sku[ ], char descr[ ][25])
{
int n=0;
char answer;

do {
cout << "\nEnter the age: ";
cin >> sku[n];
cout << "\nEnter the name: ";
cin >> descr[n];
n++;
cout << "\nDo want to enter another name? (y/n) ";
cin >> answer;
}while( answer == 'y' || answer == 'Y' );
return n;
}//close input

void report( int sku[ ], char descr[ ][25], int n)
{
int i;
void drawline(char , int );
drawline( '_',70);
cout << setw(7) << "age"
<< setw(25) << "Description"
<< '\n';
drawline( '_',70);
for( i = 0; i <= n-1; i++ )
{
cout << setw(7) << sku[i]
<< setw(25) << descr[i]
<< '\n';
}//close for
drawline( '_',70);
}//close report
//drawline() definition
void drawline(char c, int m )
{
int counter=1;
while ( counter <= m )
{
cout << c;
counter++;
}//close while
cout << '\n';
}//close drawline

void savedata( int sku[ ], char descr[ ][25], int n)
{
int i;
ofstream outfile;
outfile.open("a:names.txt", ios::app );

for( i = 0; i <= n-1; i++ )
{
outfile << sku[i]
<< ' ' << descr[i]
<< '\n';
}//close for

outfile.close();

}//close savedata
void sort( int sku[ ], char descr[ ][25], int n)
{
int i, j, temp;
char tempdes[25];

for( i = 0; i <= n-2; i++ )
{
for( j = i+1; j <= n-1; j++ )
{
if( descr[ j ] < descr[ i ] )
{
temp = sku[ i ];
sku[ i ] = sku[ j ];
sku[ j ] = temp;

strcpy( tempdes , descr[ i ]);
strcpy( descr[ i ] , descr[ j ]);
strcpy( descr[ j ] , tempdes);
}//close true branch
}//close f for loop
}//close i for loop
}//close sort
posted by:
Phantom
New York City
Advertisement
Advertisement
  • Re: i need arrays boss @_@

    Tue, June 28, 2005 - 7:54 PM
    You don't mention what the linker error is so it's hard to guess. Likely, you either typo'd a name or forgot to link with a lib.

    Just glancing at your file, there's no safety from running off the end of the array, like stopping input after 10 items or insuring strings are large enough to support what ever is typed in.