(C++ 초보) string 입력받기 오류

kldpmania의 이미지

과제 하는 중에 너무 막혀서 답답해서 질문 올립니다.
함수들 차례대로 실행하면, 쭉 문제없다가,
questionContent 함수에서 자꾸 오류가 나네요.
뭐가 문제일까요.

#include <iostream>
#include <stdio.h>
#include <string>
#include <cstring>
using namespace std;
 
class SurveyClass {  //설문 클래스
	string question; //질문 내용
	int selectqty;  // 보기 개수
	string selectcontent[10]; //보기 내용
	int answer; //설문자 답변 내용
 
public:
	void questionRegister() {  //질문 등록 메소드
		cout << "1. 질문을 입력해주세요 (40자 이내) : ";
		std::getline(cin,question);
		cout << endl;
	}
	void questionSelection() {  //보기 개수 지정 메소드
		cout << "2. 보기 개수를 지정해주세요 : ";
		cin >> selectqty;
		cout << endl;
	}
	void questionContent() {  //보기 내용 등록 메소드
		cout << "3. 보기 내용을 등록해주세요" << endl;
		for (int i=0; i < selectqty; i++) {
			cout << i+1 << "번째 보기 : ";
			std::getline(cin,selectcontent[i]);
			cout << endl;
		}
	}
pchero의 이미지

실행 결과랑 에러 메시지도 같이 올려주세요.
언뜻 봐서는 questionSelection() 함수에서 input 받을 때, 10 이상을 지정해서 오버 플로우가 나는 것 같습니다.

---------------------------------
제일 왼쪽이 저입니다 :)

익명 사용자의 이미지

아, 오류 내용을 제가 안 적었네요ㅠㅠ
빌드시 오류도 없고 실행은 되는데, 예를 들어 보기 개수를 2로 지정하면,
"1번 보기 :
2번 보기 : " ... 이렇게 1번 보기 입력을 지나가 버리고, 바로 2번부터 입력을 받네요...

morolty의 이미지

C++은 아주 예전에 해봐서 잘 기억이 안나긴 하는데

questionContent 함수 실행후 for문 들어가기 전에 최초 인풋 버퍼 플러쉬를 한번 해보시는게 어떨까요

c++에서 버퍼 플러쉬 방법은 잘 모르겠네요.. fflush(stdin) 이 되는지..

위에서 뭔가 cin으로 입력 받은후 \n 개행문자가 아직 stdin 버퍼에 남아있을지도 모릅니다

kldpmania의 이미지

fflush 넣으니, 잘 되네요!
감사합니다!!

댓글 달기

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