0
#include<iostream>
using namespace std;
void main()
{
    int i,N,Loc,New;
    char a[10];
    int array[20];

    cout<<"Enter the size of Array Max20 :";
    cin>>N;
    if(N<=20)
    {
        array[N];

        cout<<"Enter the Element of Array";
        for(i=0;i<N;i++)
        {
            cout<<"\nEnter the element "<<i+1<<" : ";
            cin>>array[i];
        }

        cout<<"You want to enter new element  \n";
        cin>>a;

        if(a=="yes"|| "Yes")
        {
            cout<<"\nEnter the element which you want to insert";
            cin>>New;
            cout<<"Enter the location to insert the element";
            cin>>Loc;
            int y=Loc-1;
    
            for(i=0;i<N;i++)
            {
                if(y==i){
                    array[i]=New;
                    break;
                }
            }

            cout<<"\nNew Array";
            for(i=0;i<N;i++)
            {
                cout<<"["<<array[i]<<"]";
            }
        }
        else{
            cout<<"thank you";
        }
    }
    else{
        cout<<"Size is to large";
        }
    }
}

Details About problem :

The if/else block (else{cout<<"thank you";})is not working. Every time I run the code

  1. I enter the size of array.
  2. I enter the elements of it.
  3. I get two choices yes or no.
  4. problem arising here if I enter yes if will work but if I enter the no again only if block work.

I tried but I not find any answer but I think used logical or in wrong way in if block...

Please help me.

Thank you

Jerry Jeremiah
  • 9,045
  • 2
  • 23
  • 32
Gigs332
  • 1
  • 1
  • `array[N];` does not do what you think it does. Specifically: it does absolutely nothing. But the fatal flaw is wrong logic in the `if` statement. See the linked question for more information about one of the problems with the `if` statement. Additionally, you cannot compare an array to a literal string like that. – Sam Varshavchik Jul 05 '21 at 21:13
  • If you use character arrays, you need to use `if (strcmp(a, "yes") == 0 || strcmp(a, "Yes") == 0)`. It's much better to use `std::string` and read values with `getline(std::cin, my_string)`, then you can use `if (a == "yes" || a == "Yes")`. Look at cppreference.com for documentation on these functions. – Tony Delroy Jul 05 '21 at 21:14
  • 1
    You can't do if statements like that: The way you are comparing strings is wrong (as described in the other comment) but even with integers this doesn't work: `if(a==1 || 2)` The thing on each side of the `||` needs to be a valid comparison, so it would need to be: `if(a==1 || a==2)` – Jerry Jeremiah Jul 05 '21 at 21:21
  • I got it thank you guys .... – Gigs332 Jul 06 '21 at 15:30

0 Answers0