Помощ - Търси - Регистрирани - Календар
Пълна версия: Грешка в програма
UniBG Forums > Дискусии > Dexter's Lab > Програмиране
123guest
Здравейте, на ред 18, компилаторът ми DevCpp, ми изписва, че имам грешка:
CODE
#include <stdio.h>
#include <stdlib.h>
#include <string.>
#define  MAX 25
int ans2;
/**********************/
struct payment
      {
      char name[25];
      char date[9];
      float money;
      };
/**********************/
int main(void);
int showMessage(void);
int chooseMenu(int ans);
struct payment addPay(struct payment pays[MAX], int nums);
struct payment editPay(struct payment pays[MAX], int nums);
int viewPay(struct payment pays[MAX], int nums);
/**********************/
int main(void)
{
      int ans;
      
      
      do
       { printf("Choose from the following:\n");
         showMessage();
         scanf(" %d", &ans);
        
       }while((ans=chooseMenu(ans))!=4);
       return 0;
}          
/**********************/
int showMessage(void)
{
       printf("1. Add a new payment:");
       printf("2. Edit a payment:");
       printf("3. View all the payments:");
       printf("4. Exit.");
       return 0;
}
/**********************/
int chooseMenu(int ans)
{
       struct payment pays[MAX];
       int nums=0;
      
       switch(ans)
        {
         case(1):{ pays[nums] = addPay(pays, nums);
                   nums++;
                   break;
                 }
         case(2):{ printf("Enter the number that will be edited:");
                   scanf(" %d", &ans2);
                   ans2-=1;
                   pays[ans2] = editPay(pays);
                   break;
                 }
         case(3):{ viewPay(pays, nums);
                   break;        
                 }
         default:{ printf("Input a write character!\n");
                   break;
                 }
        }
       return 0;
}
/**********************/
struct payment addPay(struct payment pays[MAX], int nums)
{
       int ctr;
      
        for(ctr=0;ctr<=nums;ctr++)
           {
                 printf("What is the name of the pay?");
                 gets(pays[ctr].name);
                 printf("What was the date of th pay?");
                 gets(pays[ctr].date);
                 printf("How money did you paid?");
                 scanf(" %f", &money);
           }
        return (pays[nums]);
}
/**********************/
struct payment editPay(struct payment pays[MAX], int nums)
{
       int ctr;
                  
       printf("Enter the new name of the pay:");
       gets(pays[ans2].name);
       printf("Enter the new date:");
       gets(pays[ans2].date);
       printf("Enter the new/edited money that you gave:");
       scanf(" %f", &pays[ans2].money);
      
       printf("End of editing!\n\n");
       return (pays[ans2]);
}
/**********************/
int viewPay(struct pays[MAX], int nums)
{
       int ctr;
       float all_money;
      
       for(ctr=0;ctr<nums;ctr++)
          {
                printf("\tYour %d pay name is: %s\n", (ctr+1), pays[ctr].name);
                printf("\tOn date: %s\n", pays[ctr].date);
                printf("\tAnd the money are: %.2f\n", pays[ctr].money);
                all_money+= pays[ctr].money;
          }
       printf("All the money that you gave are: %.2f\n", all_money);
       return 0;
}
otherGuest
Открих, че на 82 ред трябва да е 'scanf(" %f", &pays[ctr].money);' и на функцията 'viewPay()', също съм сгрешил параметрите, но въпреки това програмата пак не са компилира...
dlh
$:> gcc -o t t.c
t.c:3:19: string.: No such file or directory
t.c: In function `chooseMenu':
t.c:58: error: too few arguments to function `editPay'
t.c: In function `addPay':
t.c:82: error: `money' undeclared (first use in this function)
t.c:82: error: (Each undeclared identifier is reported only once
t.c:82: error: for each function it appears in.)
t.c: At top level:
t.c:102: warning: "struct pays" declared inside parameter list
t.c:102: warning: its scope is only this definition or declaration, which is probably not what you want
t.c: In function `viewPay':
t.c:102: error: parameter name omitted
t.c:109: error: `pays' undeclared (first use in this function)
guesta
Грешките са предимно свързани с масива от структири, но аз го предавам правилно мисля...
Narf
Ето source-а с вече поправени грешки:

QUOTE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 25
int ans2;
/**********************/
struct payment
{
char name[25];
char date[9];
float money;
};
/**********************/
int main(void);
int showMessage(void);
int chooseMenu(int ans);
struct payment addPay(struct payment pays[MAX], int nums);
struct payment editPay(struct payment pays[MAX], int nums);
int viewPay(struct payment pays[MAX], int nums);
/**********************/
int main(void)
{
int ans;


do
{ printf("Choose from the following:\n");
showMessage();
scanf(" %d", &ans);

}while((ans=chooseMenu(ans))!=4);
return 0;
}
/**********************/
int showMessage(void)
{
printf("1. Add a new payment:\n");
printf("2. Edit a payment:\n");
printf("3. View all the payments:\n");
printf("4. Exit.");
return 0;
}
/**********************/
int chooseMenu(int ans)
{
struct payment pays[MAX];
int nums=0;

switch(ans)
{
case(1):{ pays[nums] = addPay(pays, nums);
nums++;
break;
}
case(2):{ printf("Enter the number that will be edited:");
scanf(" %d", &ans2);
ans2-=1;
pays[ans2] = editPay(pays, nums);
break;
}
case(3):{ viewPay(pays, nums);
break;
}
default:{ printf("Input a write character!\n");
break;
}
}
return 0;
}
/**********************/
struct payment addPay(struct payment pays[MAX], int nums)
{
int ctr;

for(ctr=0;ctr<=nums;ctr++)
{
printf("What is the name of the pay?");
gets(pays[ctr].name);
printf("What was the date of th pay?");
gets(pays[ctr].date);
printf("How money did you paid?");
scanf(" %f", &pays[ctr].money);
}
return (pays[nums]);
}
/**********************/
struct payment editPay(struct payment pays[MAX], int nums)
{
int ctr;

printf("Enter the new name of the pay:");
gets(pays[ans2].name);
printf("Enter the new date:");
gets(pays[ans2].date);
printf("Enter the new/edited money that you gave:");
scanf(" %f", &pays[ans2].money);

printf("End of editing!\n\n");
return (pays[ans2]);
}
/**********************/
int viewPay(struct payment pays[MAX], int nums)
{
int ctr;
float all_money;

for(ctr=0;ctr<nums;ctr++)
{
printf("\tYour %d pay name is: %s\n", (ctr+1), pays[ctr].name);
printf("\tOn date: %s\n", pays[ctr].date);
printf("\tAnd the money are: %.2f\n", pays[ctr].money);
all_money+= pays[ctr].money;
}
printf("All the money that you gave are: %.2f\n", all_money);
return 0;
}


Но все пак при компилиране с gcc излиза warning:

QUOTE
narf@narf:~$ gcc moo.c
/tmp/ccUFIezq.o: In function `addPay':
moo.c:(.text+0x295): warning: the `gets' function is dangerous and should not be used.


А и е възможно да влезеш в endless loop ако въведеш неправилна стойност още в първоначалния input. :>
guest-a
Благодаря ти, но резултатът е същия - програмата се компилира(при мен без грешки), но когато се избере от менюто 1, двете съобщения след , които следва въвеждан се отпечатват едно след друго....
Narf
Просто слагай \n (newline) където искаш да се отиде на нов ред.
GUEST-A
Не мога да резбера, защо като трябва да се изведат всички плащания(като се натисне 3), всичко излиза на маймуница....
Това е семпла версия на форума. За да видиш пълната версия, която има повече информация, по-добра подредба и снимки, натисни тук.
Invision Power Board © 2001-2008 Invision Power Services, Inc.