Home > OS >  How to remove Warnings From our Code in C ?
How to remove Warnings From our Code in C ?

Time:09-06

What does " control reaches end of non-void function" means??

How to remove the warnings from out code ?

#include <bits/stdc  .h>
using namespace std;

int LinearSearch()
{
    int ans=-1;
    
    cout << "Enter the Size of the array:  \n";
    int n;
    cin >> n;
    cout << "Enter the array elements: \n";
    int arr[n];
    for (int i = 0; i < n; i  )
    {
        cin >> arr[i];
    }
    int key;
    cout << "Enter the key: \n";
    cin >> key;
    for (int i = 0; i < n; i  )
    {
        if (arr[i] == key)
        {
            cout << "the " << key << " is found at index " << i << endl;
        }
        return ans;
    }
}

    int main()
{
    while (1)
    {
        cout << "\t Main Menu\n";
        cout << "1. For Linear Search\n";
        cout << "2. For Binary Search\n";
        cout << "3. For First and last Occurence\n";

        int ch;
        cout << "Enter the choice: \n";
        cin >> ch;
        switch (ch)
        {
        case 1:
           
          cout<<  LinearSearch();
            break;
        case 2:

            break;
        case 3:

            break;

        default:
            cout << "Invalid Choice OOps!! ";
            break;
        }
    }

    return 0;
}

enter image description here

AS I am trying to run it it is giving me Warning Why?? Error is: warning: control reaches end of non-void function [-Wreturn-type] How to resolve it?

CodePudding user response:

When n is zero, you never reach a return. You need a return after the loop. Returning -1 might be the sensible choice in that situation. (Also, the return in the loop is misplaced...)

CodePudding user response:

It means that int LinearSearch() is expected to return an int but there are code paths where that does not happen. For instance, if n == 0. You fix this by adding a return statement with an appropriate value on that code path. It's probably an error that you have the return within the for() loop as this means you get at most one iteration. Maybe this is what you want?

int LinearSearch()
{
    int ans=-1;

    cout << "Enter the Size of the array:  \n";
    int n;
    cin >> n;
    cout << "Enter the array elements: \n";
    int arr[n];
    for (int i = 0; i < n; i  )
    {
        cin >> arr[i];
    }
    int key;
    cout << "Enter the key: \n";
    cin >> key;
    for (int i = 0; i < n; i  )
    {
        if (arr[i] == key)
        {
            cout << "the " << key << " is found at index " << i << endl;
            ans = i;
            break;
        }
    }
    return ans;
}

CodePudding user response:

If your C compiler supports C 17 or higher standard then I would suggest using std::optional to return a "no value" to use std::nullopt and return the "ans" when you actually find the key. Please look at the code below for a sample implementation.

#include <optional>

using namespace std;

std::optional<int> LinearSearch()
{
    int ans;
    
    cout << "Enter the Size of the array:  \n";
    int n;
    cin >> n;
    cout << "Enter the array elements: \n";
    int arr[n];
    for (int i = 0; i < n; i  )
    {
        cin >> arr[i];
    }
    int key;
    cout << "Enter the key: \n";
    cin >> key;
    for (int i = 0; i < n; i  )
    {
        if (arr[i] == key)
        {
            cout << "the " << key << " is found at index " << i << endl;
            return ans;
        }
    }
    return std::nullopt;
}

    int main()
{
    while (1)
    {
        cout << "\t Main Menu\n";
        cout << "1. For Linear Search\n";
        cout << "2. For Binary Search\n";
        cout << "3. For First and last Occurence\n";

        int ch;
        cout << "Enter the choice: \n";
        cin >> ch;
        switch (ch)
        {
        case 1:{
          auto res = LinearSearch();
          if (res) cout<<  *res;
          else cout << "key not found";
        }
            break;
        case 2:

            break;
        case 3:

            break;

        default:
            cout << "Invalid Choice OOps!! ";
            break;
        }
    }

    return 0;
}
  • Related