in need of some hw help

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

Join Date: Jul 2004
Posts: 2
Reputation: boi86 is an unknown quantity at this point 
Solved Threads: 0
boi86 boi86 is offline Offline
Newbie Poster

in need of some hw help

 
0
  #1
Jul 13th, 2004
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?

  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. }
Reply With Quote Quick reply to this message  
Join Date: Jun 2003
Posts: 313
Reputation: red_evolve is on a distinguished road 
Solved Threads: 0
red_evolve's Avatar
red_evolve red_evolve is offline Offline
Posting Whiz

Re: in need of some hw help

 
0
  #2
Jul 13th, 2004
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.
"Study the past if you would define the future" - Confucius
Reply With Quote Quick reply to this message  
Join Date: Jul 2004
Posts: 2
Reputation: boi86 is an unknown quantity at this point 
Solved Threads: 0
boi86 boi86 is offline Offline
Newbie Poster

Re: in need of some hw help

 
0
  #3
Jul 13th, 2004
The output I'm getting is

  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,

  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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2003
Posts: 313
Reputation: red_evolve is on a distinguished road 
Solved Threads: 0
red_evolve's Avatar
red_evolve red_evolve is offline Offline
Posting Whiz

Re: in need of some hw help

 
0
  #4
Jul 13th, 2004
Greetings.
Thanks.
I've modified a bit, it may not be as efficient. Hope this helps

  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. }
"Study the past if you would define the future" - Confucius
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 34
Reputation: Fili is an unknown quantity at this point 
Solved Threads: 0
Fili's Avatar
Fili Fili is offline Offline
Light Poster

Re: in need of some hw help

 
0
  #5
Jul 14th, 2004
How about:
  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. }

Reply With Quote Quick reply to this message  
Reply

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



Other Threads in the C++ Forum
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC