943,551 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 2498
  • C++ RSS
Jul 13th, 2004
0

in need of some hw help

Expand Post »
I've been trying for hours and still haven't been able to figure out whats the problem. I've tried many variations in the functions, but still unsure. I'm trying to make a program that changes the inputted array of names first name middle name, and last name to different format (Last Name, First Name Middle Inital). While capitalizing the first letters and making the middle name an inital. I'm trying to get the first, middle, and last names into one array first which are seperated by spaces, then copying them into different arrays, character by character using a for loop. Then deleting the name copied in the main array by just moving the arrays back to array 0 from the first space. I'm thinking there might be problems with the two functions I made to send the names to the other arrays and the array moving back portion. Please bare with my coding, still a beginner... anyone have any advice on what might be the problem?

C++ Syntax (Toggle Plain Text)
  1. #include <cctype>
  2. #include <cstring>
  3. #include <iostream>
  4. using namespace std;
  5.  
  6. void sort_name(char name[], char new_name[]);
  7. void remove_name(char name[], int space);
  8.  
  9. const int NUM_LETTERS = 21;
  10. const int MAX_LETTERS = 64;
  11.  
  12. int main()
  13. {
  14. char get_name[MAX_LETTERS], last_name[NUM_LETTERS], first_name[NUM_LETTERS],
  15. middle_inital[NUM_LETTERS];
  16.  
  17. cout << "\nPlease enter your first name, middle initial, and last name.\n"
  18. << "(max 20 letters per name)\n";
  19. cin >> get_name;
  20.  
  21. sort_name(get_name, first_name);
  22. sort_name(get_name, middle_inital);
  23. sort_name(get_name, last_name);
  24.  
  25. cout << last_name << ", " << first_name << " " << middle_inital << "." << endl;
  26.  
  27. return 0;
  28. }
  29.  
  30. void remove_name(char name[], int space)
  31. {
  32. int size = NUM_LETTERS;
  33. // space++;
  34.  
  35. for(int i = space; i < space; i--)
  36. name[i] = name[i+1];
  37. }
  38.  
  39. void sort_name(char name[], char new_name[])
  40. {
  41. for(int i=0; i<MAX_LETTERS; i++)
  42. {
  43. if(!isspace(name[i]))
  44. {
  45. remove_name(name, i);
  46. break;
  47. }
  48. else
  49. new_name[i] = name[i];
  50. }
  51. new_name[0] = static_cast<char>(toupper(new_name[0]));
  52. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
boi86 is offline Offline
2 posts
since Jul 2004
Jul 13th, 2004
0

Re: in need of some hw help

Greetings.
Hi. Could you please provide a set of input and its expected output?
Just to make sure I understood perfectly and would be analysing your code correctly.
Reputation Points: 53
Solved Threads: 1
Posting Whiz
red_evolve is offline Offline
313 posts
since Jun 2003
Jul 13th, 2004
0

Re: in need of some hw help

The output I'm getting is

C++ Syntax (Toggle Plain Text)
  1. Please enter your first name, middle initial, and last name.
  2. (max 20 letters per name)
  3. william jefferson clinton
  4. William, William William.

but the expected output is,

C++ Syntax (Toggle Plain Text)
  1. Please enter your first name, middle initial, and last name.
  2. (max 20 letters per name)
  3. william jefferson clinton
  4. Clinton, William J.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
boi86 is offline Offline
2 posts
since Jul 2004
Jul 13th, 2004
0

Re: in need of some hw help

Greetings.
Thanks.
I've modified a bit, it may not be as efficient. Hope this helps

C++ Syntax (Toggle Plain Text)
  1. #include <cctype>
  2. #include <cstring>
  3. #include <iostream>
  4. using namespace std;
  5.  
  6. void test(char [], int, char[]);
  7.  
  8. const int NUM_LETTERS = 21;
  9. const int MAX_LETTERS = 64;
  10.  
  11. char get_name[MAX_LETTERS], last_name[NUM_LETTERS], first_name[NUM_LETTERS];
  12. char middle[NUM_LETTERS];
  13. int until;
  14.  
  15. int main()
  16. {
  17. cout << "\nPlease enter your first name, middle initial, and last name.\n"
  18. << "(max 20 letters per name)\n";
  19. cin.getline( get_name, MAX_LETTERS);
  20.  
  21. test(get_name, strlen(get_name), last_name);
  22. test(get_name, until, middle);
  23. test(get_name, until, first_name);
  24.  
  25. cout<<last_name<<", "<<first_name<<" "<<middle[0]<<"."<<endl;
  26. return 0;
  27. }
  28.  
  29. void test(char name[], int i, char new_name[NUM_LETTERS])
  30. {
  31. int size=i;
  32. int j=0;
  33.  
  34. while(!isspace(name[i]) && i>=0)
  35. {
  36. i-=1;
  37. }
  38. until = i-1;
  39. for( int x=i+1; x<=size; x++)
  40. {
  41. new_name[j] = name[x];
  42. j+=1;
  43. }
  44. new_name[0] = static_cast<char>(toupper(new_name[0]));
  45. }
Reputation Points: 53
Solved Threads: 1
Posting Whiz
red_evolve is offline Offline
313 posts
since Jun 2003
Jul 14th, 2004
0

Re: in need of some hw help

How about:
C++ Syntax (Toggle Plain Text)
  1. #include <iostream.h>
  2. #include <ctype.h>
  3. #include <conio.h>
  4. void main()
  5. {
  6. cout<<"please enter first name, middle name and last name"<<endl;
  7. cout<<"(max 20 chars each)"<<endl;
  8. char fn[20],mn[20],ln[20];
  9. cin>>fn>>mn>>ln;
  10. fn[0]=toupper(fn[0]);
  11. mn[0]=toupper(mn[0]);
  12. ln[0]=toupper(ln[0]);
  13. cout<<ln<<", "<<fn<<" "<<mn[0]<<".";
  14. }

Reputation Points: 16
Solved Threads: 0
Light Poster
Fili is offline Offline
34 posts
since Jun 2004

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: acceleration and brakes in a car game
Next Thread in C++ Forum Timeline: Stack Queue Fstream





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


Follow us on Twitter


© 2011 DaniWeb® LLC