초보 프로그래머의 오류

6Gelemen의 이미지

소수 예제를 짰는데 제대로 작동하지가 않네요 -_-;;
도와주시면 감사하겠습니다.

int main(void)
{
	int num1 = 1;
	while (num1 < 10000)
	{
		for (int num2 = 1;num2 < num1;num2++)
		{
			if (num1 == 1)     //1은 소수가 아니므로
				goto PLUS;
			else if (num2 == 1)//1로나누면 안되기 때문
				continue;
		        else if (num1 == num2)//여기에도달하면num1이하의모든수로나눈것임 
			{
				printf("%d는 소수입니다./n", num1);
				goto PLUS;
			}
			else if (num1 % num2 == 0)//소수가 아니기 때문
				goto PLUS;
		}
	PLUS:
		num1++;
	}
}
라스코니의 이미지

디버그를 해 보세요. line by line 으로 실행하다 보면 대부분 해결됩니다.

raymundo의 이미지

복잡한 디버거를 따로 쓸 것도 없이,
안쪽 for 루프 블록 첫 줄에

for (...) {
   printf("num1 = %d, num2 = %d\n", num1, num2);
   ...

이렇게 현재 변수값만 출력시켜보셨어도 바로 깨달을 수 있었을 겁니다. num1 == num2 인 조건을 만족시킬 가능성이 없네요.

좋은 하루 되세요!

세벌의 이미지

goto 안 쓰는 걸 권고합니다. goto는 옛날에 많이 쓰던 건데 유지보수 어려워서 요즘엔 잘 안 써요...

greeze의 이미지

모듈화만 잘하면 유지보수가 어려운일은 없을텐데요

greeze의 이미지

세벌님 말씀에 동의하기 어렵네요.
뭐든 필요하기에 쓰이기 마련인데, 단순히 요즘에 안쓰니 권고하지 않는다는건 초보 프로그래머에게 문법에 대한
이유없는 편견이 생기게 할수 있습니다.
goto문은 다중 for문 탈출이라던지, 적절한 예외처리에 오히려 가독성을 높여줄수 있습니다.

즉, 필요하면 쓰는겁니다!!!!

Hodong Kim@Google의 이미지

세벌님 의견도 맞고, greeze님 의견도 맞습니다.

6Gelemen의 이미지

제가 저번에 goto를 안 쓰고 소수 예제를 짰는데 스파게티 코드가 되더군요.
코드의 목적에 따라 case by case 로 봐야 하는 것 같습니다.

6Gelemen의 이미지

그래서 어떻게 바궈야 하죠?

raymundo의 이미지

num2 가 num1 과 같아지지 못하는 게 원인이니, for 문의 조건을 num2 <= num1으로 고치세요.

좋은 하루 되세요!

6Gelemen의 이미지

int main(void)
{
	int num1 = 1;
	while (num1 < 10000)
	{
		for (int num2 = 1;num2 <= num1;num2++)
		{
			if (num1 == 1)
				goto PLUS;
			else if (num2 == 1)
				continue;
			else if (num1 == num2)
			{
				printf("%d는 소수입니다.\n", num1);
				goto PLUS;
			}
			else if (num1 % num2 == 0 )
				goto PLUS;
		}
	PLUS:
		num1++;
	}
}

로 해서 잘 됐네요.
감사합니다!

댓글 달기

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