So, I'm trying to write this program here are the listen instructions.

1. You are going to write a program that uses 3 input files and 3 output files

2. your program will include a function template that sorts an array of values in ascending order. the function will receive an unordered array and will return in an ordered array. The function will not perform any input or output. your template will contain two parameters: a generic array and an integer. The int indicates the number of elements in the array to be stored.

3. read the 3 input files into 3 arrays of 100 positions each. call the function template to sort them. Write the sorted data in the arrays to 3 output files.

4. Since none of the files contains 100 values you must count the number of values in each file as you read it. This number will be passed to the sort function to indicate how many values should be sorted.

I feel like I'm pretty much done, here is what I have so far

#include <iostream>
#include <fstream>

using namespace std;

template< typename T > 

void sortArray ( const T * const array[], const int count)
{
	int smallest; 
	
	for int ( i = 0; i < count; i++)
	{ 
		smallest = i;

		for (int index = i +1; index < count; index++
			if (array[index] < array [smallest])
				smallest = index;

		T temp = array[i]
		array[i] = array[smallest];
		array[smallest] = temp;
	}
}

int main()
{

ifstream inputQuotes ("QuoteFile.txt");
ofstream outputQuotes ("outputQuotes.txt");
ifstream inputInts ("IntFile.txt");
ofstream outputInts ("outputInts.txt");
ifstream inputFloats ("FloatFile.txt");
ofstream outputFloats ("outputFloats.txt");

	const int size = 100;

	string quoteArray[size];
	int intArray[size];
	double floatArray[size];


		sortArray(quoteArray, size);

	    cout << "Array with quotes in order contains:" << endl;
		for (int i = 0; i < size; i++)
			cout << quoteArray[i] << endl;

		
		sortArray(intArray, size);

	    cout << "Array with ints in order contains:" << endl;
		for (int i = 0; i < size; i++)
			cout << intArray[i] << endl;


		sortArray(floatArray, size);

	    cout << "Array with floats in order contains:" << endl;
		for (int i = 0; i < size; i++)
			cout << floatArray[i] << endl;

}

And here are my errors codes:

Error 1 error C2958: the left parenthesis '(' found at 'h:\c++\programming assignment 5\programming assignment 5\programming assignment 5\main.cpp(22)' was not matched correctly h:\c++\programming assignment 5\programming assignment 5\programming assignment 5\main.cpp 29


Error 2 error C2784: 'void sortArray(const T *const [],const int)' : could not deduce template argument for 'const T *const []' from 'std::string [100]' h:\c++\programming assignment 5\programming assignment 5\programming assignment 5\main.cpp 49

Error 3 error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion) h:\c++\programming assignment 5\programming assignment 5\programming assignment 5\main.cpp 53

Error 4 error C2784: 'void sortArray(const T *const [],const int)' : could not deduce template argument for 'const T *const []' from 'int [100]' h:\c++\programming assignment 5\programming assignment 5\programming assignment 5\main.cpp 56

Error 5 error C2784: 'void sortArray(const T *const [],const int)' : could not deduce template argument for 'const T *const []' from 'double [100]' h:\c++\programming assignment 5\programming assignment 5\programming assignment 5\main.cpp 63

6 IntelliSense: no instance of function template "sortArray" matches the argument list h:\c++\programming assignment 5\programming assignment 5\programming assignment 5\main.cpp 49

7 IntelliSense: no operator "<<" matches these operands h:\c++\programming assignment 5\programming assignment 5\programming assignment 5\main.cpp 53

8 IntelliSense: no instance of function template "sortArray" matches the argument list h:\c++\programming assignment 5\programming assignment 5\programming assignment 5\main.cpp 56

9 IntelliSense: no instance of function template "sortArray" matches the argument list h:\c++\programming assignment 5\programming assignment 5\programming assignment 5\main.cpp 63

Not sure really where to go. I feel like it's me opening the multiple files that's messing me up. I've never had to do it before, so it's a bit new to me.

Recommended Answers

All 6 Replies

I have to dispute your "pretty much done". I spotted a half dozen problems before I looked at your errors. That said:

If you actually read the error messages, they give you pretty good clues about what the problem is. Main clue: What line is the parser on when it gives up and emits an error? (The problem will be on or before that line). Secondary clue: What did the parser say the problem is? Even if you can't understand it, you can often get a hint.

I have to admit, the first error is a doozy to figure out. If you scan backward in your code from line 29 until you find an unmatched '(', though, you will see a problem on line 16.

P.S. I'd rename that function to "bubbleSortArray"

P.P.S. <rant>Why are you using a raw array in a C++ class? (Yeah, I see what the prof says to do, but it is, in my opinion, a very poor use of your time and the compiler.) Introductory C++ should make much use of the STL and all the built in C++ types until you get your feet under you about what it means to program. Arrays are efficient C structures, but there's no reason to care about efficiency in a beginning programming class; and very little reason to ever need them in professional C++ programming (unless you end up writing library code, which is very unlikely). </rant>

It's not really a top of the line school. Just a community college for now, so I can't really expect the best use of our class time unfortunately. I found out most of the errors. I'm still pulling my hair out with these though.


Error 1 error C2784: 'void bubbleSortArray(const T *[],int)' : could not deduce template argument for 'const T *[]' from 'std::string [100]' h:\c++\programming assignment 5\programming assignment 5\programming assignment 5\main.cpp 50

Error 2 error C2784: 'void bubbleSortArray(const T *[],int)' : could not deduce template argument for 'const T *[]' from 'int [100]' h:\c++\programming assignment 5\programming assignment 5\programming assignment 5\main.cpp 58

Error 3 error C2784: 'void bubbleSortArray(const T *[],int)' : could not deduce template argument for 'const T *[]' from 'double [100]' h:\c++\programming assignment 5\programming assignment 5\programming assignment 5\main.cpp 65


Here is what I have now:

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

template< typename T > 

void bubbleSortArray (const T * const array[], const int count)
{
	int smallest; 
	
	for int (i = 0; i < count; i++)
	{ 
		smallest = i;

		for (int index = i +1; index < count; index++)
			if (array[index] < array [smallest])
				smallest = index;

		T temp = array[i]
		array[i] = array[smallest];
		array[smallest] = temp;
	}
}

int main()
{

ifstream inputQuotes ("QuoteFile.txt");
ofstream outputQuotes ("outputQuotes.txt");
ifstream inputInts ("IntFile.txt");
ofstream outputInts ("outputInts.txt");
ifstream inputFloats ("FloatFile.txt");
ofstream outputFloats ("outputFloats.txt");

	const int size = 100;

	string quoteArray[size];
	int intArray[size];
	double floatArray[size];


		bubbleSortArray(quoteArray, size);

	    cout << "Array with quotes in order contains:" << endl;

		for (int i = 0; i < size; i++)
			outputQuotes << quoteArray[i] << endl;

		
		bubbleSortArray(intArray, size);

	    cout << "Array with ints in order contains:" << endl;
		for (int i = 0; i < size; i++)
			outputInts << intArray[i] << endl;


		bubbleSortArray(floatArray, size);

	    cout << "Array with floats in order contains:" << endl;
		for (int i = 0; i < size; i++)
			outputFloats << floatArray[i] << endl;

}

P.P.S. <rant>Why are you using a raw array in a C++ class? (Yeah, I see what the prof says to do, but it is, in my opinion, a very poor use of your time and the compiler.) Introductory C++ should make much use of the STL and all the built in C++ types until you get your feet under you about what it means to program....</rant>

Ahh, yet another "I know better than the instructor" rant. Of course you do. You've been at it longer and understand more concepts than the student that hasn't been taught 100% of the language yet. Until they get to STL, you can't expect them to use STL.

And it's better not to use the built in types until you know how to make them yourself. When you learn how they work, you can then use the easy version. Otherwise you can't program worth beans if you get into a language that doesn't magically define all your special data types.

...
And it's better not to use the built in types until you know how to make them yourself. When you learn how they work, you can then use the easy version. Otherwise you can't program worth beans if you get into a language that doesn't magically define all your special data types.

Of course your opinion is the best one: Its yours! But in the opinion of most programming educators, it is more important to learn how to program at the first stage, and only then learn nuts and bolts details about some language or other. But this isn't the right forum for that discussion.

Well, I'm still stuck on it. If anyone has any useful hints on it so I can learn what i'm doing wrong :\ I'd be thankful

The issue you're still seeing, I think, is that T* doesn't match std::string. Stop and think about what T really is here.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.