Ok, I'm sitting here in my CIS class trying to figure out how to move words put in my character array. The program is, sorting the monthly rainfall in order from highest to lowest, organizing the months along with it. I have one array of ints and another character array. This is the code i have. I am having trouble moving the words to another part of the array. Here is my exact code.

// Written by Ryan Dillon
// November 30, 2004
// Chapter 8
// Assignment 3, Rainfall Statistics Modification

#include <iostream>
#include <conio.h>
#include <iomanip>
using namespace std;

void selectionSort(int[], char[12][10], int);
void showArray(int[], char[12][10], int);

int main()
	int month[12], total = 0, smallest = 9999, largest = -1;
	int count;
	char months[12][10] = {"January", "February", "March",
			       "April", "May", "June",
			       "July", "August", "September",
			       "October", "November", "December"};
	cout << "Please enter the total rainfall for each month." << endl;
	for (count = 0; count < 10; count++)
		month[count] = -1;
	for (count = 0; count < 10; count++)
			cout << months[count] << ": ";
			cin >> month[count];
			if (month[count] < 0)
			cout << "Please enter positive values only.";

	showArray(month, months, 12);
return 0;

void selectionSort(int array[], char months[12][10], int elems)
	int startScan, maxIndex, maxValue, index;
	char tempMonth[12][10];
	for (startScan = 0; startScan < (elems - 1); startScan++)
		maxIndex = startScan;
		maxValue = array[startScan];
		tempMonth = months;
		for(index = startScan + 1; index < elems; index++)
			if (array[index] > maxValue)
				maxValue = array[index];
				maxIndex = index;
		array[maxIndex] = array[startScan];
		months[maxIndex] = months[startScan];
		array[startScan] = maxValue;
		months[startScan] = tempMonth[maxIndex];
void showArray(int array[], char months[12][10], int elems)
	int index;
	for (index = 0; index < elems; index++)
		cout << months[index] << "\t" << array[index] << endl;

so uh... help :mrgreen:

this is my first post btw, im glad i found a good c++ forum!

Recommended Answers

All 3 Replies


First, you need comments. Every function should be commented out on what it does, and what the variables are. Insightful commenting will help out anyone trying to read the code. I am surprised your class doesn't seem to require it... and no, comments are not to be written AFTER the code is done. Use them while developing, so that it is all "there".

You need to look at the strcpy (Stringcopy) command to copy the strings.

It also looks like you are trying to copy the whole array... tempMonth = months. Unless the langauge has changed recently, in my day you could not copy elements of an array like that. You had to pound out each element in an assignment to make a copy.

Let us know...


character strings are treated as arrays and therefore to move or swap words two things can be done:
1: u can use the library function
strcpy(destination string,source string)
this will copy the `source string` into the `destination string`
2: u can use a for loop to transfer each element of the char string into the required string like this:

for(i = 0;i<length of the source string;i++)

Thanks for the help, I figured out an even simpler way to do what I want it to do. Instead of moving strings, I decided to use the month number instead and use a switch to display the correct months. For future reference, the strcopy command will come in handy. I'll try to be better on comments... I now understand why I need them. To me the program makes sense, but to someone reading it, it might not. Thanks again, I'm sure ill have future threads about more string related problems. Letters are the only thing that seems to give me issues in C++.

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.