Konyac
Konyac

Reputation: 33

Im trying to put return at the end of the code but they keep messing me up with while before return

Like i want to put the "return 0" at the right place, everything in the code is working normal.

complie the code and they keep saying about "while before return", i tried to put outside of the code and inside

#include <stdio.h>
#include <stdlib.h>
#define MAXN 100

int menu(){
    printf("Menu:\n");
    printf("1- Add a value\n");
    printf("2- Search a value\n");
    printf("3- Print out the array\n");
    printf("4- Print out values in a range\n");
    printf("5- Print out the array in ascending order\n");
    printf("6- Quit?\n");
    printf("Enter your operation: ");
    int choice;
    scanf("%d", &choice);
    return choice;
}

int isFul (int*a, int n){   
return n==MAXN;
}
int isEmpty (int*a, int n){   
return n==0;
}
void add(int value, int*a, int*pn){
    a[*pn] = value;
    (*pn)++;
}
int search(int x, int *a, int n){
    int i;
    for (i=0; i<n; i++) if (a[i]==x) return i;
    return -1;
}
void printvalueinrange (int*a, int n){
    int i, min, max;
    printf("\nEnter min value: ");scanf("%d", &min);
    printf("\nEnter max value: ");scanf("%d", &max);
    for(i=0; i<sizeof(a); i++)
    if(a[i]>=min&&a[i]<=max) printf("%d", a[i]);
}
void printarray(int*a, int n){
    int i;
    for (i=0;i<n;i++) printf("%d", a[i]);
}
void printAsc(int*a, int n){ 
    int** adds =(int**)calloc(n, sizeof(int*));
    int i,j;
    for(i=0; i<n; i++) adds[i]= &a[i];
    int* t;
    for (i=0;i<n-1; i++)
       for(j=n-1; j>i; j--)
         if (*adds[j]< *adds[j-i]){
            t=adds[j];
            adds[j]=adds[j-1];
            adds[j-1]=t;
         }
    for (i=0;i<n; i++) printf("%d ", *adds[i]);
    free(adds);  
}
int main(){ 
    int a[MAXN];
    int n=0;
    int value;
    int choice;
    do
    {   choice= menu();
        switch(choice){
            case 1:{
                if (isFull(a,n)) printf("\n Sorry! The array is full.\n");
                else
                {  printf ("Input an added value:");
                   scanf("%d", &value);
                   add(value, a, &n);
                   printf("Added!\n");
                   }
                   break;
            }
            case 2:{
                if (isEmpty(a,n)) printf("\n Sorry! The array is empty.\n");
               else 
               {   printf ("Input the searched value:");
                   scanf("%d", &value);
                   int pos = search(value, a, n);
                   if (pos<0) printf("Not found!\n");
                   else printf("Postion is found: %d\n", pos);
               }   break;
            }
            case 3:{
                if (isEmpty(a,n)) printf("\n Sorry! The array is empty.\n");
                else
                   {
                     printarray(a,n);
                   } break;
                   }
            case 4:{
                if (isEmpty(a,n)) printf("\n Sorry! The array is empty.\n");
                else
                   {
                     printvalueinrange(a,n);  
                   } break;
               }
            case 5:{
                if (isEmpty(a,n)) printf("\n Sorry! The array is empty.\n");
                else
                   {
                    printAsc(a,n);          
                   } break;
                   } 
              default: printf ("Goodbye!");
              break;
        }
             while (choice>0 && choice<7);
             getchar(); 
    }
                return 0;
    }

I just adding some word to fit with the requirement :"D And if u find out any kind of error or mistake that in my code, just points out for me to improve them xd

Upvotes: 0

Views: 39

Answers (1)

Vlad from Moscow
Vlad from Moscow

Reputation: 310990

In this part of main

}
             while (choice>0 && choice<7);
             getchar(); 
    }
                return 0;
    }

the while construction occupies a wrong place.

There should be

    }
        } while (choice>0 && choice<7);
        getchar(); 
            return 0;
}

Also in this statement

if (isFull(a,n)) printf("\n Sorry! The array is full.\n");
    ^^^^^^

there is a typo. There should be

if (isFul(a,n)) printf("\n Sorry! The array is full.\n");

Upvotes: 1

Related Questions