한문제만 도와주십쇼 ㅠㅠㅠ

익명 사용자의 이미지

#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include

void flush_input_buffer(void)
{
while (getchar() != '\n');
}

int main(void)
{
char first_name[20];
char last_name[20];
char full_name[40];

printf("Enter your first name: ");
fgets(first_name, sizeof(first_name), stdin);

void flush_input_buffer(void);

printf("Enter your last name: ");
fgets(last_name, sizeof(last_name), stdin);

void flush_input_buffer(void);

strcat(first_name, last_name);
strcpy(full_name, first_name);
printf("Your full name is %s\n", full_name);

return 0;
}
제가 짠 코드인데요.. 여기서 아무리 긴 이름을 입력해도 오류 없이
Enter your first name: Meg ( Meg 입력)
Enter your last name: Ryan ( Ryan 입력)
Your full name is Meg Ryan. (입력없이 출력)
이렇게 어떻게 만드나요ㅠ..
제가한거는
Enter your first name: Meg ( Meg 입력)
Enter your last name: Ryan ( Ryan 입력)
Your full name is Meg
Ryan. 이렇게 나오고
20글자 넘게치니까 무조건 오류가나오는데..ㅠ

xtiinhs의 이미지

아무리 자세한 질문글이라도

Quote:
코드 입력 또는 들여쓰기가 필요한 내용의 경우,
<code> tag block 을 이용하여 작성 하십시오.

이걸 무시하면 글을 읽기 힘든 것은 사실입니다. include 날아간 거 안 보이시나요?

Quote:
Your full name is Meg
Ryan. 이렇게 나오고

fgets()로 받은 문자열 끝에 개행 문자가 들어 있다면 저렇게 됩니다. 그걸 찾아서 없애는 방법은 연습 문제로 남겨 두겠습니다. 아울러 strcat, strcpy 함수 대신 strncat, strncpy를 사용하는 것이 더 좋을 것 같습니다.

Quote:
20글자 넘게치니까 무조건 오류가나오는데..ㅠ

char first_name[20];
char last_name[20];
char full_name[40];

저기에 20이라는 숫자가 괜히 있는 게 아닙니다. 글자 길이 제한을 더 높이고 싶다면 20을 그냥 더 큰 숫자로 바꾸거나 malloc을 사용한 동적 할당에 대해서 알아 보세요.

 void flush_input_buffer(void);

main 함수에 이건 무엇을 의도하고 쓴 문장인가요?

질문자의 이미지

#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include

int main()
{
char first_name[20];
char last_name[20];
char full_name[40];

printf("Enter your first name: ");
fgets(first_name, sizeof(first_name), stdin);
first_name[strlen(first_name) - 1] = 0;

printf("Enter your last name: ");
fgets(last_name, sizeof(last_name), stdin);
last_name[strlen(last_name) - 1] = 0;

strcpy(full_name, first_name);
strcat(full_name, " ");
strcat(full_name, last_name);
printf("Your full name is %s\n", full_name);
return 0;
}
이렇게 해봤는데 여기서 strncat, strncpy 은 처음보는데 어떤식으로 가야하나요 글자 수를 조정하는거 같긴한데..

익명 사용자의 이미지

레퍼런스를 애용합시다.

https://en.cppreference.com/w/c/string/byte/strncat
https://en.cppreference.com/w/c/string/byte/strncpy

QnA에 질문올려놓고 무한정 기다리는 것보다 매우 빠르게 답을 얻을 수 있습니다.

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.