1,105,581 Community Members

Sorting a string or character array

Member Avatar
sasucker
Newbie Poster
5 posts since Feb 2007
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

For my assignment this week, I was supposed to create a program that would read in names from a file, sort them, then output the names sorted. I attempted to do it as a insertion sort, and seemed to have failed miserably... if anybody could point me in the right direction that would be great :D All of my errors appear in the functions I've 'created', starting from strcpy()

Also please point out any poor coding habits. Though I am a noob at this, I realize it doesn't excuse my inability to do things right.

#include<iostream>
#include<cstring>
#include<fstream>

using namespace std;

void sortString(char array[], int num);
void outputString(char array[], int num);

int main() {
	int columns=30, rows=0;
	char filename[30], letter;
	ifstream fin;
	
	cout << "Enter the file name: ";
	cin >> filename;
	
	fin.open(filename);
	
	if (!fin) {
		cout << "Failure to open the file.";
	} else {
		while (fin >> letter) {
			if (letter == '\n')
				rows++;
		}
		
		fin.clear();
		fin.seekg(0,ios::beg);
		
		char name[rows][columns];
		rows = 0;
		
		while (fin.get(name[rows][columns])) {
			rows++;
		}
		
		sortString(name, rows+1);
		outputString(name, rows+1);
	}
	
	return 0;
}

void sortString(char array[], int num) {
	int i, j;
	char check[num][30];
	
	for (i=1; i<num; i++) {
		strcpy(check, array[i][30]);
		for (j=1; j>=1 && (strcmp(check, array[30][j-1])==1); j--) {
			array[j][30] = array[j-1][30];
			array[j-1][30] = check;
		}
	}
}

void outputString(char array[], int num) {
	for (int i=0; i<num; i++) {
		cout << array[i][30] << endl;
	}
}
Member Avatar
thekashyap
Practically a Posting Shark
809 posts since Feb 2007
Reputation Points: 193 [?]
Q&As Helped to Solve: 77 [?]
Skill Endorsements: 0 [?]
 
0
 

First the code won't compile due to This line:
=> char name[rows][columns];

Have a look at this and this.

Member Avatar
sasucker
Newbie Poster
5 posts since Feb 2007
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Thank you thekashyap for your help. For the next hour or so I tried various ways to do this, and what my main issue seems to be is this:

-if I use strings, I suddenly lose the ability to use strcmp, perhaps there's some side to this function that I'm unaware of?
-if I use normal character arrays, it has to become "2d" yes? That was what I was going for with 'char name[rows][columns]
-I tried using pointer character arrays (something I read up on at cplusplus.com but felt like their tutorial was lacking a little... might just be my learning style) and couldn't figure out a way to get file/user input to create an array with that. Perhaps someone could demonstrate to me the right way? :D

It feels like if I could just get strcpy and strcmp to work with my above program, it would work, though perhaps the [rows][columns] issue would show up again later the SDK hasn't complained to me about it yet.

If I could be shown how those same functions work with strings (if they do at all), I could get it to work.

Or if someone could explain to me how to use the char *blah[numberhere] to work with user/file input, what I have going now would work. Then again, the pointer thing might not even be meant for cases such as this and I may have been grasping at straws with that one.

Thank you for your time :)

*Edit* I guess when I say 'strings' I mean the literal use of the variable 'string str'

Member Avatar
iamthwee
Posting Sage
7,066 posts since Aug 2005
Reputation Points: 1,307 [?]
Q&As Helped to Solve: 595 [?]
Skill Endorsements: 74 [?]
Featured
 
0
 

You sound really confused. Surely you teacher should tell what they expect.

Member Avatar
~s.o.s~
Failure as a human
10,399 posts since Jun 2006
Reputation Points: 2,496 [?]
Q&As Helped to Solve: 992 [?]
Skill Endorsements: 72 [?]
Administrator
Featured
 
0
 

-if I use strings, I suddenly lose the ability to use strcmp, perhaps there's some side to this function that I'm unaware of?

Nope you don't. Just use the normal comparision operator (==) for comparing C++ strings.

Read this.

Question Answered as of 7 Years Ago by iamthwee, ~s.o.s~ and thekashyap
Member Avatar
sasucker
Newbie Poster
5 posts since Feb 2007
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
1
 

iamthwee: I dunno, the assignment itself was explained rather vaguely..

Design, code, and test a C/C++ program that reads in the name of a file from standard input. The program reads strings (names) from that file (until the end of the file), sorts these names in lexicographic order, and prints the sorted names to standard output.

Print an appropriate error message if the input file cannot be opened for reading.

~s.o.s~: Thank you very much. I feel really dumb now... But that definitely did the trick.

For anybody curious, here was my final code

#include<iostream>
#include<cstring>
#include<fstream>

using namespace std;

void sort(string array[], int len);
void print(string array[], int len);

int main() {
	int rows=0;
	char filename[30];
	string names[100];
	ifstream fin;
	
	cout << "Enter a filename: ";
	cin >> filename;
	
	fin.open(filename);
	
	if (!fin) {
		cout << "Error opening file.";
		return 0;
	}
	
	while (getline(fin,names[rows])) {
		rows++;
	}
	
	sort(names,rows);
	print(names,rows);

	fin.close();
	return 0;
}

void sort(string array[], int len) {
	int i, j;
	string check;
	
	for(i=1; i<len; i++) {
		check = array[i];
		for(j=i; j>=1 && (check < array[j-1]); j--) {
			array[j] = array[j-1];
			array[j-1] = check;
		}
	}
}

void print(string array[], int len) {
	for(int i=0; i<len; i++) {
		cout << array[i] << endl;
	}
}

Thank you all for your help :)

Member Avatar
llk
Newbie Poster
1 post since Oct 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

perfectly working..................thank you very much for the code............

You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article