0

This program is supposed to input someones name and output it like " Last, first middle". The names are supposed to be stored in 3 different arrays and their is a fourth array for the full name at the end. I am also supposed to use strncpy and strncat to build the fourth array. My issue im having is i dont know what the use of strncpy would be in this situation and how to use it. I can make the program say "first middle last" but not the correct output. Another issue im having is the while loop is supposed to allow the user to say 'q' or 'Q' and quit the program but it doesnt do this

#include <iomanip>
#include <iostream>
#include <cctype>
using namespace std; 

int main()
{
char replay; //To hold Q for quit

const int SIZE = 51;
char firstName[SIZE]; // To hole first name
char middleName[SIZE]; // To hold middle name
char lastName[SIZE]; // To hold last name
char fullName[SIZE]; //To hold the full name
int count = 0;
int maxChars1;
int maxChars2;



cout << "Enter Q to quit or enter your first name of no more than " << (SIZE - 1)
     << " letters: ";
cin.getline(firstName, SIZE);

while(firstName[SIZE] != 'Q' || firstName[SIZE] != 'q')
{
cout << "\nEnter your middle name of no more than " << (SIZE - 1)
     << " letters: ";
cin.getline(middleName, SIZE);

cout << "\nEnter your last name of no more than " << (SIZE - 1)
     << " letters: ";
cin.getline(lastName, SIZE);

maxChars1 = sizeof(firstName) - (strlen(firstName) + 1);
strncat(firstName, middleName, maxChars1);

cout << firstName << endl;

maxChars2 = sizeof(lastName) - 1;
strncpy(firstName, lastName, maxChars2);
lastName[maxChars2] = '\0';

cout << lastName << endl;
    }

system("pause");
return 0;
}
user1807815
  • 83
  • 2
  • 12
  • 2
    If C++, don't use `char *` but `std::string`. If C, don't use `std::cin` but `fgets()`. –  Apr 23 '13 at 16:21
  • 1
    Hint: your code should assemble the result in `fullName`. Which perhaps should just be defined large enough to contain all three components, but then again if it was large enough then there would be no point using the `strn` functions, so you would not learn whatever it is this exercise is supposed to teach you. – Steve Jessop Apr 23 '13 at 16:28
  • @Steve Jessop But why would i use strncpy or strcpy at all here? – user1807815 Apr 23 '13 at 16:50
  • @user1807815: personally I wouldn't. I assume that you've been told to, in order to learn how they work. The basic problem at hand is that you have three strings, each up to 50 chars long, and you are trying to concatenate them into an array that is only 50 chars long and hence might be shorter than their total length. So *something* has to check the sizes to avoid overrunning the buffer. `strncpy` and `strncat` provide some size checking, so (under duress) they can be used to do this. But they have so many limitations that they're basically not worth it. – Steve Jessop Apr 23 '13 at 16:53
  • @Steve Jessop Yeah so thats the problem. If strncpy just replaces whats in one of the arrays with something from another one then doesnt that not work? Like if the middle name is replaced with the first name then the first name is gone and that helps none – user1807815 Apr 23 '13 at 16:58
  • 1
    @user1807815: right, so don't do `strncpy(middleName, firstName, ...)` . First decide what should go where (taking into account my hint above), and then for each thing that you're putting into place, decide whether to use `strncpy` or `strncat` to get it there. – Steve Jessop Apr 23 '13 at 17:02
  • @Steve Jessop So i fixed it and its running good. But it outputs "LastFirstMiddle" but it's supposed to say "Last, First Middle". Do you know how to do this? Thank you – user1807815 Apr 23 '13 at 17:54

1 Answers1

1

Your while loop doesn't work because of several reasons:

  • You're actually looking one past the end of the firstName array (firstName[SIZE]) rather than the first character (firstName[0]).
  • You're not checking to make sure firstName is only a single character q or Q.
  • You're only asking for the first name one time, before the loop, instead of every loop.

Your call to strncpy doesn't look right. As written, you're taking the last name and copying it to firstName, destroying the first and middle names that you just concatenated together there. Like @steve-jessop said, assemble the full name in fullName.

You're probably supposed to use strncpy and strncat because this is a contrived example/exercise where the buffer taking the full name is of a restricted size, so some name combinations will not fit, and need to be truncated.

Richard Walters
  • 1,464
  • 8
  • 16
  • Thanks, so i fixed the program and it does what it's supposed to do but the name outputs as "LastFirstMiddle", instead of "Last, First Middle". Do you know how to put spaces or commas between these? – user1807815 Apr 23 '13 at 17:49
  • In between concatenating the parts, concatenate the small strings that form the spaces and commas. Use `strncat` again, this time what you're concatenating is `", "` and `" "`. – Richard Walters Apr 23 '13 at 19:19