C언어 관련 작성프로그램 질문이요

georgekyung의 이미지

#include

void main()
{
int n,num,rsl=0,numrsl=0;
char grade;
float avg;

printf("Input number of subject :");
scanf("%d",&n); //과목수

while(n>0)
{

printf("\n성적과 시수 입력하세요:");
scanf("%c %d",&grade,&num);
fflush(stdin);


switch(grade)
{
case'A':

rsl+=4*num;
numrsl+=num;
n--;
break;

case'B':

rsl+=3*num;
numrsl+=num;
n--;
break;


case'C':
rsl+=2*num;
numrsl+=num;
n--;
break;


}


}

avg=(float)rsl/numrsl;

printf("당신의 평점은 : %f 입니다\n",avg);


}

=========================
한번 짜본 프로그램인데요...
여기서 f5 눌러서 디버깅 과정 보면요..

처음에 성적과 시수 입력할땐 인식안하고 넘어가는지를 모르겠어요 ㅠ....

ex)과목수 3과목인데,,, 4과목을 입력하라고 뜨더군요...(자세히 디버깅 과정을 보니,, 처음 입력한건 switch case를 건너뛰고
두번째 입력할대부터 인식하고 넘어가던데.... 왜그런거죠 ㅠㅠ?

yae1021의 이미지

엔터도 문자죠.

georgekyung의 이미지

엔터도 문자로 인식되고...다음꺼에 영향을 미치는것이군요..?;

감사하빈다

그럼 fflush를 맨처음 과목수 입력할때 밑에도 써줘야하는군요?

Geor

라스코니의 이미지

fflush 보다는 \r 을 받아서 버리는 방법이 더 좋습니다.
C 입문 책들을 보면 안정되게(?) 숫자를 입력하는 방법이 나옵니다.

제 기억으로는 gets() 로 입력 문자열을 받은 후 그것을 sscanf 하여 처리하였던 것으로 기억납니다.

planetarium의 이미지

그치만 gets() 나 sscanf() 둘 다 buffer overflow 문제가 있는 함수라서...

라스코니의 이미지

없을 것 같네요.

어떤 posix 관련 책에 있는 예제입니다.

int a;
char message[64];

char line[128];
..

if(fgets(line, sizeof(line), stdin) == NULL) exit(0);
if(strlen(line) <= 1) continue;

if(sscanf(line, "%d %64[^\n]", &a, message) < 2)
{
//error status
}
else {
//ok
}

아니면 C++ 이라면 cin 을 써서 받으면 될 듯 싶네요.

planetarium의 이미지

관련 함수들 전부 POSIX 표준에서 아예 삭제되었습니다.
올려주신 예제에서도 gets() 대신 fgets(..., stdin) 을 사용하고 있죠.

댓글 달기

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