简体   繁体   English

标签c ++ c-strings,strncat,strncpy

[英]c++ c-strings, strncat, strncpy

This program is supposed to input someones name and output it like " Last, first middle". 该程序应该输入某人的姓名,并像“ 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. 名称应存储在3个不同的数组中,它们是末尾全名的第四个数组。 I am also supposed to use strncpy and strncat to build the fourth array. 我还应该使用strncpy和strncat来构建第四个数组。 My issue im having is i dont know what the use of strncpy would be in this situation and how to use it. 我的问题是我不知道在这种情况下如何使用strncpy以及如何使用它。 I can make the program say "first middle last" but not the correct output. 我可以使程序说“ first first last last”,但是输出不正确。 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 我遇到的另一个问题是while循环应该允许用户说“ q”或“ Q”并退出程序,但它不会这样做

#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;
}

Your while loop doesn't work because of several reasons: 您的while循环由于以下几个原因而无效:

  • You're actually looking one past the end of the firstName array ( firstName[SIZE] ) rather than the first character ( firstName[0] ). 实际上,您正在查找的是firstName数组( firstName[SIZE] )的末尾,而不是第一个字符( firstName[0] )。
  • You're not checking to make sure firstName is only a single character q or Q . 您无需检查以确保firstName 是单个字符qQ
  • 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. 您对strncpy呼叫看起来不正确。 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. 如所写,您将使用姓氏并将其复制到firstName ,销毁刚刚在此处串联在一起的名字和中间名。 Like @steve-jessop said, assemble the full name in fullName . 就像@ steve-jessop所说的那样,将全名组装成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. 您可能应该使用strncpystrncat因为这是一个人为设计的示例/练习,其中使用全名的缓冲区的大小受限制,因此某些名称组合将无法使用,需要被截断。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM