954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

character arrays...help please!!!

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;
}
tigger0484
Newbie Poster
6 posts since Apr 2008
Reputation Points: 10
Solved Threads: 1
 

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

ravenrider
Light Poster
25 posts since Apr 2008
Reputation Points: 10
Solved Threads: 0
 

what are you trying to do??

joshmo
Posting Whiz in Training
280 posts since Oct 2007
Reputation Points: 19
Solved Threads: 20
 

I would help you If you provided abit more info on what your trying to do.

William Hemsworth
Posting Virtuoso
1,591 posts since Mar 2008
Reputation Points: 1,429
Solved Threads: 129
 

>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.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

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.

tigger0484
Newbie Poster
6 posts since Apr 2008
Reputation Points: 10
Solved Threads: 1
 

>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.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

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.

eager
Newbie Poster
12 posts since Jan 2008
Reputation Points: 18
Solved Threads: 2
 
>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???

tigger0484
Newbie Poster
6 posts since Apr 2008
Reputation Points: 10
Solved Threads: 1
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You