| | |
in need of some hw help
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jul 2004
Posts: 2
Reputation:
Solved Threads: 0
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)
#include <cctype> #include <cstring> #include <iostream> using namespace std; void sort_name(char name[], char new_name[]); void remove_name(char name[], int space); const int NUM_LETTERS = 21; const int MAX_LETTERS = 64; int main() { char get_name[MAX_LETTERS], last_name[NUM_LETTERS], first_name[NUM_LETTERS], middle_inital[NUM_LETTERS]; cout << "\nPlease enter your first name, middle initial, and last name.\n" << "(max 20 letters per name)\n"; cin >> get_name; sort_name(get_name, first_name); sort_name(get_name, middle_inital); sort_name(get_name, last_name); cout << last_name << ", " << first_name << " " << middle_inital << "." << endl; return 0; } void remove_name(char name[], int space) { int size = NUM_LETTERS; // space++; for(int i = space; i < space; i--) name[i] = name[i+1]; } void sort_name(char name[], char new_name[]) { for(int i=0; i<MAX_LETTERS; i++) { if(!isspace(name[i])) { remove_name(name, i); break; } else new_name[i] = name[i]; } new_name[0] = static_cast<char>(toupper(new_name[0])); }
•
•
Join Date: Jul 2004
Posts: 2
Reputation:
Solved Threads: 0
The output I'm getting is
but the expected output is,
C++ Syntax (Toggle Plain Text)
Please enter your first name, middle initial, and last name. (max 20 letters per name) william jefferson clinton William, William William.
but the expected output is,
C++ Syntax (Toggle Plain Text)
Please enter your first name, middle initial, and last name. (max 20 letters per name) william jefferson clinton Clinton, William J.
Greetings.
Thanks.
I've modified a bit, it may not be as efficient. Hope this helps
Thanks.
I've modified a bit, it may not be as efficient. Hope this helps
C++ Syntax (Toggle Plain Text)
#include <cctype> #include <cstring> #include <iostream> using namespace std; void test(char [], int, char[]); const int NUM_LETTERS = 21; const int MAX_LETTERS = 64; char get_name[MAX_LETTERS], last_name[NUM_LETTERS], first_name[NUM_LETTERS]; char middle[NUM_LETTERS]; int until; int main() { cout << "\nPlease enter your first name, middle initial, and last name.\n" << "(max 20 letters per name)\n"; cin.getline( get_name, MAX_LETTERS); test(get_name, strlen(get_name), last_name); test(get_name, until, middle); test(get_name, until, first_name); cout<<last_name<<", "<<first_name<<" "<<middle[0]<<"."<<endl; return 0; } void test(char name[], int i, char new_name[NUM_LETTERS]) { int size=i; int j=0; while(!isspace(name[i]) && i>=0) { i-=1; } until = i-1; for( int x=i+1; x<=size; x++) { new_name[j] = name[x]; j+=1; } new_name[0] = static_cast<char>(toupper(new_name[0])); }
"Study the past if you would define the future" - Confucius
How about:
C++ Syntax (Toggle Plain Text)
#include <iostream.h> #include <ctype.h> #include <conio.h> void main() { cout<<"please enter first name, middle name and last name"<<endl; cout<<"(max 20 chars each)"<<endl; char fn[20],mn[20],ln[20]; cin>>fn>>mn>>ln; fn[0]=toupper(fn[0]); mn[0]=toupper(mn[0]); ln[0]=toupper(ln[0]); cout<<ln<<", "<<fn<<" "<<mn[0]<<"."; }
![]() |
Other Threads in the C++ Forum
- Previous Thread: acceleration and brakes in a car game
- Next Thread: Stack Queue Fstream
| Thread Tools | Search this Thread |
api application array arrays based beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion convert count data database delete desktop developer directshow dll dynamiccharacterarray email encryption error file forms fstream function functions game generator getline graph homeworkhelper iamthwee ifstream input int integer java lib linux list loop looping loops map math matrix memory multiple newbie news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates text tree url vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets





