[완료]c++ strtok 문제

y10h23s의 이미지

	char sentence[100];
	char *tokenstr;
	cout<<"이름 전화번호 관계를 차례대로 입력해주세요.(이름/전화번호/관계)"<<endl;
	cin.getline(sentence,100);
 
	tokenstr = strtok(sentence,"/");
	char *name = new char[strlen(tokenstr)+1];
	strcpy(name,tokenstr);
 
	tokenstr = strtok(NULL,"/");
	char *phone = new char[strlen(tokenstr)+1];
	strcpy(name,tokenstr);
 
	tokenstr = strtok(NULL,"/");
	char *relation =new char[strlen(tokenstr)+1];
	strcpy(relation,tokenstr);

한줄로 입력받은 정보를 strtok으로 나눠서 각각 변수에 저장할려고 하는데..
cin.getline도 문제가 있는건가요? 저부분에서 런타임오류가 나네요..
sentence에 직접 문장을 저장하고 시작해보면 저장한 순서가 뒤죽박죽이고 어떤 곳엔 쓰레기값이 들어가있습니다.

yielding의 이미지

cin.getline은 문제가 없어보입니다

1) char sentence[100] => 함수의 local variable은 초기화를 해줘야 합니다.

 
char sentence[100] = { 0 } ;

2) char* phone = new char[strlen(...) + 1]

 char *phone = new char[...];
 strcpy(..) //ok

strcpy는 NULL까지 복사를 하죠. 그래서 동적할당한 메모리를 초기화할 필요가 없습니다.

여담으로, strtok는 thread safety문제가 있어서 POSIX에서는 strtok_r 버전이 따로 있지요. 그리고 modern c++를 쓴다면 strtok 말고 다른 깔끔한 방법을 쓸겁니다.

Life rushes on, we are distracted

y10h23s의 이미지

cin.getline은 앞에 cin.ignore을 써넣으니깐 런타임 오류없이 작동되는데 뒤에 토큰문제는
1/2/3으로 입력한다면 2/쓰레기/3 이런식으로 입력됩니다..

Anti-Lock의 이미지

tokenstr = strtok(NULL,"/");
char *phone = new char[strlen(tokenstr)+1];
strcpy(name,tokenstr);

위부분에서 strcpy(name,tokenstr); 부분이 잘못 되었네요^^
strcpy(phone,tokenstr); 라고 해야...
y10h23s의 이미지

예 결국 그게 문제였습니다.
저도 어제 찾고한참을...멍때렸네요.
감사합니다

댓글 달기

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
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.