| | |
character arrays...help please!!!
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Apr 2008
Posts: 6
Reputation:
Solved Threads: 1
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!!!!
this is my code but when I run it I get a bunch of garbage...any suggestions would be greatly appreciated!!!!
C++ Syntax (Toggle Plain Text)
#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; }
Last edited by Narue; Apr 14th, 2008 at 12:11 pm. Reason: Added code tags, please do it yourself next time.
>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 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 here to prove you wrong.
•
•
Join Date: Apr 2008
Posts: 6
Reputation:
Solved Threads: 1
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.
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.
•
•
Join Date: Jan 2008
Posts: 7
Reputation:
Solved Threads: 2
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.
As was mentioned, the easiest way to see what is happening in your program is to step through it with a debugger.
•
•
Join Date: Apr 2008
Posts: 6
Reputation:
Solved Threads: 1
•
•
•
•
>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???
![]() |
Other Threads in the C++ Forum
- Previous Thread: Trying to get a program to extract data from a text file
- Next Thread: Please. Need Code Help.
| Thread Tools | Search this Thread |
api array based binary c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock wordfrequency wxwidgets






