migf1 έγραψε:Σε επίπεδο λογικής μια χαρά τον βλέπω τον κώδικα, αλλά έχεις ένα θέμα στο table[] που διαβάζεις με fgets(). Από τη στιγμή που το έχεις ορίσει μονάχα σε 2 χαρακτήρες, δεν λύνεις το πρόβλημα με τα απομεινάρια στην κύρια είσοδο. Πρέπει να το βάλεις με μεγάλη χωρητικότητα ( > 256). Εναλλακτικά, μπορείς να χρησιμοποιήσεις την s_getflushed() τον κώδικα της οποίας έχω ποστάρει σε προηγούμενο ποστ αυτού του νήματος, η οποία και διαβάζει το πλήθος χαρακτήρων που της λες και καθαρίζει και την κύρια είσοδο, χωρίς να χρησιμοποιεί την fflush().
Επίσης, δεν χρειάζεσαι και N και M (ένα από τα 2 αρκεί).
Προσπάθησε να στοιχίζεις και τα σχόλιά σου τέρμα δεξιά, γιατί έτσι κολλητά με τον κώδικα τον κάνουν πολύ δυσανάγνωστο, ειδικά αν δεν υπάρχει syntax highlighter (ξεχώριζε και τις συναρτήσεις με μια γραμμή από παύλες μέσα σε σχόλια, πάνω από τον ορισμό της κάθε μιας... βοηθάει πάρα πολύ όσους διαβάζουν τον κώδικά σου

)
Κατσε οταν λες ενα απο τα 2 αρκει... γιατι μου βγαζει συντακτικο λαθος....
στο μεταξυ γιατι ας πουμε με μονο 2 χαρακτηρες δεν καθαριζει την κυρια εισοδο??? εχει εξηγηθει καπου αυτο και δεν το εχω δει????
οσο για τα σχολια τωρα ειναι ενταξει????
- Κώδικας: Επιλογή όλων
/*========================================
*
* Calculating odd and even sum of
* elements in the arrays table_odd
* and table_even. The input was given from user
* and will store in the arrays. After all through
* the switch the user choose A or B if want to call
* function sum_odd and calculates the odd sum or
* call sum_even and calculates the even sum the simply solution
* about this problem creates by Kostas at 12/8/11 on this code
*
=========================================*/
#include<stdio.h>
#include<stdlib.h>
#define N 10
#define M 10
int sum_odd(int table_odd[N]); //sum_odd function declaration
int sum_even(int table_even[M]); // sum_even function declaration
int result; //declaration result variable as a global variable , note that is
// not good programming technique and actually might create problems
// we use it only for this specific example
int main()
{
int table_odd[N];
int table_even[N];
char table[2]="";
char c;
printf("\t\t Welcome in C programm: \n");
printf("Give A or B for odd or even sum or Q for quit: \n");
fgets(table,2,stdin); //We use the fgets function from stdlib and not getchar() because she has problem with the buffer
sscanf(table,"%c",&c); // And now we can use the sscanf instead the classic scanf
switch(c)
{
case 'A':
printf("You have choose the odd sum\n");
result=sum_odd(table_odd); //call sum function for the odd calculating
printf("The odd sum is : %4d",result); //display the odd result
break;
case 'B':
printf("You have choose the even sum\n");
result=sum_even(table_even);
printf("The even sum is: %4d",result); //display the even result
break;
default:
;
}
return 0;
}
/*The defining of sum_odd function is here */
int sum_odd(int table_odd[N])
{
int i;
result=0;
printf("\n\nGive the 10 numbers(one under of others):\n");
for(i=0; i<N; i++)
{
scanf("%d",&table_odd[i]); //save the user input into the table_odd
if((table_odd[i]%2)==0)
continue;
result += table_odd[i];
}
return result;
}
/*The defining of sum_even function is here */
int sum_even(int table_even[M])
{
int i;
result=0;
printf("\n\nGive 10 numbers(one under of others):\n");
for(i=0; i<M; i++)
{
scanf("%d",&table_even[i]); //save
if((table_even[i]%2)==0)
result += table_even[i];
}
return result;
}
/*-------------------------------------- */
αμα τα παω τελειως τερμα στον μεταγλωτιστη μου δειχνει οτι πρεπει να παω δεξια κατω κατω την μπαρα για να φανουν για αυτο
δεν ξερω αμα στο code reposiroty εχει αυτο το θεμα...