에러를 못잡겟어요 ㅠㅜ

NamSa의 이미지

sscanf가 .. 포인터형을 못받는 것같다는 생각 만 드네요

Quote:

#include <stdio.h>
main()
{
int i,t,j;
char *kor[100];
printf("insert the number of times : ");
scanf("%d",&t);//면개의 수를 대입할지 정한다.
for(j=0;j<=t;j++){
while(1)
{
printf("input number (1 ~ 100) : ");
scanf("%s",kor[j]);
if(sscanf(kor[j], "%d", &i) == 1 && (i >= 1&& i <= 100))//100에서 0사이의 숫자만 받도록한다
break;
printf("\nError!\n");
}
}
for(j=0;j<=t;j++){
printf("%s\n",kor[j]); //출력해준다
}
}
jachin의 이미지

헉!!! -_-;;; 이렇게 위험한 프로그래밍을 하시다니...

T-T 먼저 공부부터 하시고 나신다음에 하시죠.

char *kor[100]이라고 하시면, 임의의 1차원 배열공간을 100개 만드신다는 뜻입니다.

헌데 scanf 에서 kor[j]라고 설정을 하시면, 무조건 한 단어를 입력... 또 다시 sscanf를 이용하시면... -_-;;;;

문제 분석 자체를 잘못 이해하신게 아니신가요...?

NamSa의 이미지

항상 조언 감사합니다..

그럼 어떤방식으로 해야..

여러개의 .. 문장을 체크할수 있을까요~~

운형의 이미지

Quote:

char *kor[100]이라고 하시면, 임의의 1차원 배열공간을 100개 만드신다는 뜻입니다.

배열공간 100?????????

4바이트 주소 공간 100개 아닐런지..

Do you think that's the air you are breathing now?

jasonlife의 이미지

there are two solutions, i think..

you need to allocate memory to kor[j] before using the scanf(), or
declare the kor as two dimension array, like char kor[100][100] instead of char *kor[100].

You need memory space to store the reading value.

Jason.

:D

I tested following code, and it works well..

#include <stdio.h>
main()
{
int i,t,j;
char kor[100][10];
printf("insert the number of times : ");
scanf("%d",&t);
for(j=0;j<t;j++){
while(1)
{
printf("input number (1 ~ 100) : ");
scanf("%s",kor[j]);
if(sscanf(kor[j], "%d", &i) == 1 && (i >= 1&& i <= 100))
break;
printf("\nError!\n");
}
}
for(j=0;j<=t;j++){
printf("%s\n",kor[j]);
}
}

=================================
Jason

You are never too old to do something smart
=================================

jachin의 이미지

운형 wrote:
Quote:

char *kor[100]이라고 하시면, 임의의 1차원 배열공간을 100개 만드신다는 뜻입니다.

배열공간 100?????????

4바이트 주소 공간 100개 아닐런지..

^^; 네. 4바이트 주소 공간 100개.

아무래도 우선 제가 문제 파악을 제대로 했는지 확인해 주세요.

1. 입력은 자유롭되, 공백문자를 통해 구분된 문자열이 1 이상 100이하의 숫자 이외에는 입력되지 않는다.

2. 100이상의 숫자가 입력되지 않아야 한다.

한 번 제가 만들려고 해보니... -_-;;; 으으음...

자꾸 고치게 되네요...

FSM(Finite State Machine)을 짜보려고 하고 있습니다 ( ' ')a

NamSa의 이미지

바로 위에분의 소스는 먹히지를 않네요 ㅠ,ㅜ
위에 위에 분의 소스는 잘되고요..

jachin의 이미지

(' ') 에엑? 그럼 jasonlife님 소스처럼 1부터 100 사이의 숫자만 입력되면 에러없이

계속 입력을 반복해야 하는 것인가요?

으음... ( _ _)a 어떻게 동작해야 하는 소스인지 모르겠네요...

NamSa의 이미지

그런데.. 입력받은 값을..
혹시나해서 int 형으로 캐스트한후에 ..
더해ㅤㅂㅘㅅ너니 .. 역시나.. 아스키코드으ㅣ10진코드 값을
덜한 값으로 나오더군요..
더하기가 안되니 .. 평균이 ..될리 만무하구요 ㅠ,.ㅜ
#include <stdio.h>
main()
{
int i,t,j;
char kor[100][10],eng[100][10],math[100][10],name[100][10];
printf("insert the number of times : ");
scanf("%d",&t);
for(j=0;j<t;j++){
printf("input name : ");
scanf("%s",name[j]);
while(1)
{
printf("input kor number (1 ~ 100) : ");
scanf("%s",kor[j]);
if(sscanf(kor[j], "%d", &i) == 1 && (i >= 1&& i <= 100))
break;
printf("\nError!\n");
}
while(1)
{
printf("input eng number (1 ~ 100) : ");
scanf("%s",eng[j]);
if(sscanf(eng[j], "%d", &i) == 1 && (i >= 1&& i <= 100))
break;
printf("\nError!\n");
}
while(1)
{
printf("input number (1 ~ 100) : ");
scanf("%s",math[j]);
if(sscanf(math[j], "%d", &i) == 1 && (i >= 1&& i <= 100))
break;
printf("\nError!\n");
}
}
printf("|%4s|%4s|%4s|%4s|%5s\n","name","kor","eng","math","sum);
for(j=0;j<=t;j++){
printf("|%4s|%4s|%4s|%4si%5d|\n",name[j],kor[j],eng[j],(int)math[j],(int)kor[j]+(int)eng[j]+(int)math[j]);
}
}

jachin의 이미지

-_- +

으으음... FSM을 짜보려다가, 입력 버퍼 때문에 코드가 실패했군요...

jasonlife님의 코드가 월등히 좋으신 것 같습니다. -_-;;;

그리고 입력 받으신 값을 숫자로 합산 하시고 싶으시면 string.h 포함하셔서 atoi()함수로 변환 후 합산하시죠.

전 FSM을 계속 시도해 보려고 합니다. ( - -)a 왜 안되지...?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
  char c, s[4]="0";
  int i, temp;
  i = 0;
  printf("Input Number(1-100) : ");
  while(1)
  {
    c = getchar();
    if ( (int) c > 47 && (int) c < 58 )
    {
      switch (i)
      {
        case 0 :
          s[0] = c;
          temp = atoi(s);
          if (temp > 0 && temp < 10) i = 3;
          else i = 1;
          break;
        case 1 :
          s[1] = c;
          temp = atoi(s);
          if (temp > 19 && temp < 101) i = 3;
          else i = 2;
        case 2 :
          s[2] = c;
          temp = atoi(s);
          if (temp > 1 && temp < 101) i = 3;
          else
          {
            printf("\nLimit over.Try again.\nInput Number(1-100) :");
            i = 0;
            strcpy(s,"000");
            temp = 0;
          }
          break;
        case 3 :
          printf("\nInput Complete!\n");
          printf("Input Number is %d.\n", temp);
          break;
      }
    }
    else
    {
      printf("\nInput Error. Try again.\nInput Number(1-100) :");
      i = 0;
      strcpy(s, "000");
      temp = 0;
    };
    if (i == 3) break;
  };
  printf("Input Value is %d.\n", temp);
  return 0;
}
jachin의 이미지

흑... T-T 저의 우매함을 이제야 깨달았습니다....

아... 정말 저도 프로그래밍 공부 다시해야 겠군요...

이 문제를 FSM으로 짜려고 하다니... 그것도 허접한 코드로... T-T

오늘을 계기삼아 다시 노력해야 겠습니다. ( T T)

NamSa의 이미지

드디어 완성입니다.. ㅋ
다음에는 ncure 이던가하는.. 것을 이용해.. 구연해볼 까 합니다.

NamSa의 이미지

어째서 math값에 .. 이상한 값이 찍히는 건지.. ~

#include <stdio.h>
#include <string.h>
main()
{
int i,t,j,sum,a[100],b[100],c[100],d,e,k;
char kor[100][10],eng[100][10],math[100][10],name[100][10];
printf("insert the number of times : ");
scanf("%d",&t);
for(j=0;j<t;j++){
printf("input name : ");
scanf("%s",name[j]);
while(1)
{
printf("input kor number (1 ~ 100) : ");
scanf("%s",kor[j]);
if(sscanf(kor[j], "%d", &i) == 1 && (i >= 1&& i <= 100))
break;
printf("\nError!\n");
}
while(1)
{
printf("input eng number (1 ~ 100) : ");
scanf("%s",eng[j]);
if(sscanf(eng[j], "%d", &i) == 1 && (i >= 1&& i <= 100))
break;
printf("\nError!\n");
}
while(1)
{
printf("input math number (1 ~ 100) : ");
scanf("%s",math[j]);
if(sscanf(math[j], "%d", &i) == 1 && (i >= 1&& i <= 100))
break;
printf("\nError!\n");
}
}
printf("|%2s|%5s|%5s|%5s|%5s|%7s|%5s|\n","no","name","kor","eng","math","sum","avg");
printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
for(j=0;j<t;j++){
sum=atoi(kor[j])+atoi(eng[j])+atoi(math[j]);

printf("|%02d|%5s|%5s|%5s|%5s|%7d|%3.2f|\n",j,name[j],kor[j],eng[j],math[j],sum,sum/3.00);
a[j] = atoi(kor[j]);
b[j] = atoi(eng[j]);
c[j] = atoi(math[j]);
}
printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");

for(j=0;j<t;j++){
d += a[j];
e += b[j];
k += c[j];
}
sum = d+e+k;
printf("|%02s|%5s|%5d|%5d|%5d|%7d|%3.2f|\n","no","all",d,e,k,sum,sum/(float)t);
}

세벌의 이미지

소스코드를 올릴 때는 BB code 를 활용하시면 아래와 같이 들여쓰기가 반영됩니다. 그렇지 않으면 들여쓰기 열심히 한 게 소용이 없게 되지요.

#include <stdio.h>
#include <string.h>
main()
{
	int i,t,j,sum,a[100],b[100],c[100],d,e,k;
	char kor[100][10],eng[100][10],math[100][10],name[100][10];
	printf("insert the number of times : ");
	scanf("%d",&t);
	for(j=0;j<t;j++){
		printf("input name : ");
		scanf("%s",name[j]);
		while(1)
		{
			printf("input kor number (1 ~ 100) : ");
			scanf("%s",kor[j]);
			if(sscanf(kor[j], "%d", &i) == 1 && (i >= 1&& i <= 100))
				break;
			printf("\nError!\n");
		}
		while(1)
		{
			printf("input eng number (1 ~ 100) : ");
			scanf("%s",eng[j]);
			if(sscanf(eng[j], "%d", &i) == 1 && (i >= 1&& i <= 100))
				break;
			printf("\nError!\n");
		}
		while(1)
		{
			printf("input math number (1 ~ 100) : ");
			scanf("%s",math[j]);
			if(sscanf(math[j], "%d", &i) == 1 && (i >= 1&& i <= 100))
				break;
			printf("\nError!\n");
		}
	}
	printf("|%2s|%5s|%5s|%5s|%5s|%7s|%5s|\n","no","name","kor","eng","math","sum","avg");
	printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
	for(j=0;j<t;j++){
		sum=atoi(kor[j])+atoi(eng[j])+atoi(math[j]);

		printf("|%02d|%5s|%5s|%5s|%5s|%7d|%3.2f|\n",j,name[j],kor[j],eng[j],math[j],sum,sum/3.00);		
		a[j] = atoi(kor[j]);
		b[j] = atoi(eng[j]);
		c[j] = atoi(math[j]);
	}
	printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");

	for(j=0;j<t;j++){
		d += a[j];
		e += b[j];
		k += c[j];
	}
	sum = d+e+k;
	printf("|%02s|%5s|%5d|%5d|%5d|%7d|%3.2f|\n","no","all",d,e,k,sum,sum/(float)t);
}

댓글 달기

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