using visual studios C++ express 2005 edition
this is my code but when I run it I get a bunch of garbage...any suggestions would be greatly appreciated!!!!

#include <iostream>
#include <fstream>
using namespace std;
int length(char phrase[]);
void concat(char phrase1[], char phrase2[], int measure1);
void copy(char phrase1[], char phrase2[]);
int main()
{
	ifstream input;
	char phrase1[40], phrase2[40];
	int measure1, measure2;
	cout << "Reading file..." << endl;
	input.open("phrases.txt");
	input.getline(phrase1, 40, '\n');
	input.getline(phrase2, 40, '\n');
	cout << "Calling length function..." << endl;
	measure1 = length(phrase1);
	cout << "The length of phrase one is " << measure1 << "." << endl;
	cout << "Calling length function..." << endl;
	measure2 = length(phrase2);
	cout << "The length of phrase two is " << measure2 <<"." << endl;
	concat(phrase1, phrase2, measure1);
	copy(phrase1, phrase2);
	return 0;
}
int length(char phrase[])
{
	int length = -1, i = 0;
	while(length == -1)
	{
		if (phrase[i] == '\0')
		{
			length = i;
		}
		i++;
	}
	return length;
}

void concat(char phrase1[], char phrase2[], int measure1)
{
	int i, j = 0;
	i = measure1 + 1;
	phrase1[measure1] = ' '; 
	while (j < 41)
	{
		if (phrase2[j] != '\0')
		{
			phrase1[i + j] = phrase2[j];
		}
		j++;
	}
	cout << "The new phrase is " << phrase1 << endl;
	return;
}
void copy(char phrase1[], char phrase2[])
{
	int i = 0;
	while (i < 41)
	{
		phrase2[i] = phrase1[i];
		i++;
	}
	cout << "Phrase 1 is " << phrase1 << " and phrase 2 is " << phrase2 << "." << endl;
	return;
}

I would help u if I knew ...sorry but I'm sure someone will help you, their's tons of experts here

>when I run it I get a bunch of garbage...
I'm not surprised. You assume specific sizes and neglect to take the null character into account when copying. Run your code in a debugger, watch how concat and copy move characters around. You'll quickly see the problem, I'm sure.

well here is the assignment....

Write a complete C++ program that includes the functions (written by you - not existing string library or cstring functions): int length (char someText[])
o calculates and returns the length of the c-string parameter void concat (char first[], char second[])
o appends the second c-string to the end of first void copy (char source[], char dest[])
o copies c-string source to c-string dest
Create an input file that contains two text strings (phrases – each with multiple words) each not exceeding 40 characters in length. Read the strings into two character arrays. Design in a scenario to your main program that demonstrates the correct use and operation of the 3 functions.

>well here is the assignment....
That changes nothing. I've pointed out your problems and how to go about troubleshooting them. The rest is up to you. If you have any further specific problems, feel free to ask.

Every string is supposed to end with a null byte ('\0'). in concat, are you terminating phrase1 with a null byte when you copy phrase2 to the end of phrase1? In copy, are you stopping at the end of phrase1 or phrase2?

As was mentioned, the easiest way to see what is happening in your program is to step through it with a debugger.

>when I run it I get a bunch of garbage...
I'm not surprised. You assume specific sizes and neglect to take the null character into account when copying. Run your code in a debugger, watch how concat and copy move characters around. You'll quickly see the problem, I'm sure.

I'm extremely new to the whole programming thing and I'm sorry to say I don't know what you mean by a debugger....where is it located in visual studios and how does it work???

This article has been dead for over six months. Start a new discussion instead.