character arrays...help please!!!

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Apr 2008
Posts: 6
Reputation: tigger0484 is an unknown quantity at this point 
Solved Threads: 1
tigger0484 tigger0484 is offline Offline
Newbie Poster

character arrays...help please!!!

 
0
  #1
Apr 14th, 2008
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!!!!
  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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 12
Reputation: ravenrider is an unknown quantity at this point 
Solved Threads: 0
ravenrider ravenrider is offline Offline
Newbie Poster

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

 
0
  #2
Apr 14th, 2008
I would help u if I knew ...sorry but I'm sure someone will help you, their's tons of experts here
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 280
Reputation: joshmo is an unknown quantity at this point 
Solved Threads: 19
joshmo joshmo is offline Offline
Posting Whiz in Training

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

 
0
  #3
Apr 14th, 2008
what are you trying to do??
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 1,405
Reputation: William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of 
Solved Threads: 114
Sponsor
William Hemsworth William Hemsworth is online now Online
Nearly a Posting Virtuoso

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

 
0
  #4
Apr 14th, 2008
I would help you If you provided abit more info on what your trying to do.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,622
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 713
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

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

 
0
  #5
Apr 14th, 2008
>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 here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 6
Reputation: tigger0484 is an unknown quantity at this point 
Solved Threads: 1
tigger0484 tigger0484 is offline Offline
Newbie Poster

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

 
0
  #6
Apr 14th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,622
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 713
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

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

 
0
  #7
Apr 14th, 2008
>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.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 7
Reputation: eager is an unknown quantity at this point 
Solved Threads: 2
eager eager is offline Offline
Newbie Poster

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

 
0
  #8
Apr 14th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 6
Reputation: tigger0484 is an unknown quantity at this point 
Solved Threads: 1
tigger0484 tigger0484 is offline Offline
Newbie Poster

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

 
0
  #9
Apr 14th, 2008
Originally Posted by Narue View Post
>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???
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC