c언어 funttion 하고 struct를 배워서 코드를 짜는데, 화면이 뜨고 나서 멈추네요, 오류가 뭔지 뜨지도 않아서 어디를 고쳐야 할지 모르겠습니다.

powersizz의 이미지

손님 숫자를 넣어서 그 숫자에 따라 다른 값을 곱해서 총 금액을 얻어서 영수증을 출력하는건데

오류라도 뜨면 고쳐서 해보는데, 컴파일하고 런이 된 후에 화면이 뜨고나서 멈추네요

--------------------------------------------

#include
#include // for strtok
#include

struct ticket
{
int number[10];

int price[10];
};

void inputNumber(struct ticket *data, int NUMBER1);
void displayPrice(struct ticket *data, int PRICE1);
void findPrice(struct ticket *data, int NUMBER1);

int main()
{


struct ticket *data;
int PRICE1;
int NUMBER1;
printf("Ticket receipt system\n");
printf("Input the number of customers: \n ");
scanf("%d", &NUMBER1);
data=(struct ticket *)calloc(NUMBER1,sizeof(struct ticket));
inputNumber(data, NUMBER1);
displayPrice(data, PRICE1);
findPrice(data, NUMBER1);
system("pause");

}

void inputNumber(struct ticket *data, int NUMBER1)
{
int i;
FILE *fout; // write file
FILE *fin;
fout =fopen("receipt.txt", "w");
for(i=0; i {
fscanf(fin,"%d %d", data[i].number, data[i].price);
}
fclose(fout);
}

void displayPrice(struct ticket *data, int PRICE1)
{
int i;
//display to screen
printf("price of ticket:\n");

for(i=0; i {
printf(" %d ", data[i].price);
}
}

void findPrice(struct ticket *data, int NUMBER1)
{
int i;
int total;
if(NUMBER1<=10)
total=NUMBER1*7;
else if ((10<=NUMBER1) && (NUMBER1<20))
total=NUMBER1*6;
else if ((20<=NUMBER1)&&(NUMBER1<50))
total=NUMBER1*5;
else if (50<=NUMBER1)
total=NUMBER1*4;
printf("£%s", &total);
}

File attachments: 
첨부파일 크기
Plain text icon 11.txt1.55 KB
pchero의 이미지

오류는 바로 나오네요.
printf 구문 두곳 모두 인자값 참조가 잘못되었고, fscanf 역시 잘못 사용되었습니다.

모듈을 잘라서 하나씩 시도해보시길 바랍니다.

gcc -Wall -o "main" "main.c" 
main.c: In function ‘displayPrice’:
main.c:60:15: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘int *’ [-Wformat=]
               printf(" %d ", data[i].price);
               ^
main.c: In function ‘findPrice’:
main.c:76:2: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int *’ [-Wformat=]
  printf("¡Ì%s", &total);
  ^
main.c:66:6: warning: unused variable ‘i’ [-Wunused-variable]
  int i;
      ^
main.c: In function ‘main’:
main.c:33:1: warning: control reaches end of non-void function [-Wreturn-type]
 }
 ^
main.c: In function ‘inputNumber’:
main.c:43:11: warning: ‘fin’ may be used uninitialized in this function [-Wmaybe-uninitialized]
     fscanf(fin,"%d %d", data[i].number, data[i].price);
           ^
main.c: In function ‘main’:
main.c:29:19: warning: ‘PRICE1’ is used uninitialized in this function [-Wuninitialized]
       displayPrice(data, PRICE1);
                   ^
Compilation finished successfully.

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

powersizz의 이미지

dev 를 사용하는데요, 부분적으로 해도 오류가 안뜨고 부분적으로 작동만 되네요.

위에 설명하신 인자값을 어떻게 수정하는게 되는건가요?

아래와 같이 바꿨는데 아직 오류가 뜹니다

#include
#include // for strtok
#include

struct ticket
{
int number[10];
int price[10];
};

void inputNumber(struct ticket *data, int NUMBER1);
void displayPrice(struct ticket *data, int PRICE1);
void findPrice(struct ticket *data, int NUMBER1);
void writeReceipt(struct ticket *data,int PRICE1);

int main()
{


struct ticket *data;
int PRICE1;
int NUMBER1;
printf("Ticket receipt system\n");
printf("Input the number of customers: \n ");
scanf("%d", &NUMBER1);
data=(struct ticket *)calloc(NUMBER1,sizeof(struct ticket));
inputNumber(data, NUMBER1);
findPrice(data, NUMBER1);
displayPrice(data, PRICE1);
writeReceipt(data,PRICE1);
system("pause");

}

void inputNumber(struct ticket *data, int NUMBER1)
{
int i;

for(i=0; i {
scanf("%d", data[i].number);

}

}

void writeReceipt(struct ticket *data,int PRICE1)
{
FILE*fin=fopen("data.txt","r");
int i;
FILE *fout; // write file
FILE *fin;

fout=fopen("receipt.txt", "w");
for(i=0; i {
fscanf(fout,"%d ",data[i].price);
}
fclose(fout);

}

void displayPrice(struct ticket *data, int PRICE1)
{
int i;
//display to screen
printf("price of ticket:\n");

for(i=0; i {
printf(" %d ", data[i].price);
}
}

void findPrice(struct ticket *data, int NUMBER1)
{ int number[10],price[10];
int i;
int total;
if(NUMBER1<=10)
total=NUMBER1*7;
else if ((10<=NUMBER1) && (NUMBER1<20))
total=NUMBER1*6;
else if ((20<=NUMBER1)&&(NUMBER1<50))
total=NUMBER1*5;
else if (50<=NUMBER1)
total=NUMBER1*4;
printf("£ %d", &total);
}

댓글 첨부 파일: 
첨부파일 크기
Plain text icon 113.txt1.83 KB
pchero의 이미지

하나씩 집어드리기보다는 더 간단한 main 프로그램을 만들어 보시라고 하고 싶습니다.

아무래도 본인이 작성한 코드가 아닌것 같네요.
코드에 대한 이해없이 그냥 원하시는 결과만을 생각하시고 임의로 고쳐서 발생하는 문제 같습니다.

일단은 scanf 사용법이 잘못되었습니다.
간단한 scanf 프로그램부터 직접 작성해보세요.

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

powersizz의 이미지

숫자값에 따라 금액 이 달라지긴 하는데, 금액이 이상하게 뜨네요

그리고 결과 값을 파일에 쓰려는 구문도 고쳐보고 있는데요, 계속 아무 내용도 없는 텍스트 파일이 생겨납니다.

댓글 첨부 파일: 
첨부파일 크기
Plain text icon 3333.txt1.52 KB
pchero의 이미지

아무래도 자꾸 과제같다는 생각이 듭니다.

어찌됐든...전체 흐름을 생각하지않고 문제만을 바라보면 이 프로그램을 원하시는대로 수정하지 못할 겁니다....

현재 문제는 1. 금액이 이상하게 나오는 문제, 결과가 값이 파일에 안써지는 문제만 보이지만,
실제로는.. 내부적인 포인터 접근 오류, 잘못된 함수 사용입니다.

가장 큰 문제는 1.잘못된 포인터 사용 2.함수 사용 오류 입니다.

각 모듈을 테스트하기 위한 작은 프로그램을 먼저 만드시고 사용되는 함수에 대한 이해를 하신다음에 다시 접근하시는 걸 추천합니다.

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

댓글 달기

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