943,515 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 909
  • C++ RSS
Apr 14th, 2008
0

character arrays...help please!!!

Expand Post »
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!!!!
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <fstream>
  3. using namespace std;
  4. int length(char phrase[]);
  5. void concat(char phrase1[], char phrase2[], int measure1);
  6. void copy(char phrase1[], char phrase2[]);
  7. int main()
  8. {
  9. ifstream input;
  10. char phrase1[40], phrase2[40];
  11. int measure1, measure2;
  12. cout << "Reading file..." << endl;
  13. input.open("phrases.txt");
  14. input.getline(phrase1, 40, '\n');
  15. input.getline(phrase2, 40, '\n');
  16. cout << "Calling length function..." << endl;
  17. measure1 = length(phrase1);
  18. cout << "The length of phrase one is " << measure1 << "." << endl;
  19. cout << "Calling length function..." << endl;
  20. measure2 = length(phrase2);
  21. cout << "The length of phrase two is " << measure2 <<"." << endl;
  22. concat(phrase1, phrase2, measure1);
  23. copy(phrase1, phrase2);
  24. return 0;
  25. }
  26. int length(char phrase[])
  27. {
  28. int length = -1, i = 0;
  29. while(length == -1)
  30. {
  31. if (phrase[i] == '\0')
  32. {
  33. length = i;
  34. }
  35. i++;
  36. }
  37. return length;
  38. }
  39.  
  40. void concat(char phrase1[], char phrase2[], int measure1)
  41. {
  42. int i, j = 0;
  43. i = measure1 + 1;
  44. phrase1[measure1] = ' ';
  45. while (j < 41)
  46. {
  47. if (phrase2[j] != '\0')
  48. {
  49. phrase1[i + j] = phrase2[j];
  50. }
  51. j++;
  52. }
  53. cout << "The new phrase is " << phrase1 << endl;
  54. return;
  55. }
  56. void copy(char phrase1[], char phrase2[])
  57. {
  58. int i = 0;
  59. while (i < 41)
  60. {
  61. phrase2[i] = phrase1[i];
  62. i++;
  63. }
  64. cout << "Phrase 1 is " << phrase1 << " and phrase 2 is " << phrase2 << "." << endl;
  65. return;
  66. }
Last edited by Narue; Apr 14th, 2008 at 12:11 pm. Reason: Added code tags, please do it yourself next time.
Reputation Points: 10
Solved Threads: 1
Newbie Poster
tigger0484 is offline Offline
6 posts
since Apr 2008
Apr 14th, 2008
0

Re: character arrays...help please!!!

I would help u if I knew ...sorry but I'm sure someone will help you, their's tons of experts here
Reputation Points: 10
Solved Threads: 0
Light Poster
ravenrider is offline Offline
25 posts
since Apr 2008
Apr 14th, 2008
0

Re: character arrays...help please!!!

what are you trying to do??
Reputation Points: 19
Solved Threads: 20
Posting Whiz in Training
joshmo is offline Offline
280 posts
since Oct 2007
Apr 14th, 2008
0

Re: character arrays...help please!!!

I would help you If you provided abit more info on what your trying to do.
Reputation Points: 1429
Solved Threads: 129
Posting Virtuoso
William Hemsworth is offline Offline
1,542 posts
since Mar 2008
Apr 14th, 2008
1

Re: character arrays...help please!!!

>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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Apr 14th, 2008
0

Re: character arrays...help please!!!

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.
Reputation Points: 10
Solved Threads: 1
Newbie Poster
tigger0484 is offline Offline
6 posts
since Apr 2008
Apr 14th, 2008
1

Re: character arrays...help please!!!

>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.
Last edited by Narue; Apr 14th, 2008 at 1:04 pm.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Apr 14th, 2008
0

Re: character arrays...help please!!!

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.
Reputation Points: 18
Solved Threads: 2
Newbie Poster
eager is offline Offline
12 posts
since Jan 2008
Apr 14th, 2008
0

Re: character arrays...help please!!!

Click to Expand / Collapse  Quote originally posted by Narue ...
>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???
Reputation Points: 10
Solved Threads: 1
Newbie Poster
tigger0484 is offline Offline
6 posts
since Apr 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Trying to get a program to extract data from a text file
Next Thread in C++ Forum Timeline: Please. Need Code Help.





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC