-1

I am trying to learn how to code array.. (i am still a beginner in programming just starting to learn last year) i don't know what's wrong in my code.. the problem is to take 5 integer inputs from user and store them in an array. Again ask user to give a number. Now, tell user whether that number is present in array or not. but my code doesn't recognize my else. Even if i put a number that isn't present in my array it still says "It is present".

int main(){

 int num[4];
 int i;
 int comp;

 cout<<"Please input 5 numbers: "<<endl;
 for (i=0;i<=4;i++)
{
 cin>>num[i];

}
 cout<<"Now please input another number to compare: ";
 cin>>comp;

 if (num[i]=comp){
   cout<<"The number you inputted is present in the array";
}
 else {
   cout<<"It is not present in the array";
}
}
Muhammad Zeeshan
  • 4,608
  • 4
  • 15
  • 41
Yami
  • 7
  • 1
  • 1
    You might want to use a [debugger](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) for that or look at [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). And also, please fix the indentation of your code – Andreas Feb 10 '20 at 04:02
  • Consider using std::array as it remembers own size, provides interface for algorithms and (maybe) catches undefined behavior (e.g. when you give it an invalid index) in debug builds. – passing_through Feb 10 '20 at 07:27

2 Answers2

3

the problem is to take 5 integer inputs from user and store them in an array.

Let's start declaring an array that can hold up to 5 integers.

int numbers[5] = {};
//         ^^^        This should be the size of the array, not the maximum index

Since C++11, you can use a proper standard container

std::array<int, 5> numbers{};

Note that in both snippet I initialized those arrays.

Now, the loop that reads all the elements of the array can be written as

for (int i = 0; i < 5; ++i) {
//                ^
}

Or use a ranged for

for (auto & number : numbers)
{ //      ^^^^^^^^  Loops through the array using `number` as a reference to each element 
    std::cin >> number;
    if (!std::cin) {
         std::cerr << "An error occurred while reading.\n";
         break;
    }
}

The posted loop also goes from 0 to 4 (included), but the array declared in that snippet only has 4 elements, so that it is accessed out of bounds and the program has undefined behaviour.

Another problem, is that the program doesn't check

whether that number is present in array or not

It does only one "check", instead of a loop:

if ( num[i] = comp) {     

num[i] = comp is an assignment, not a comparison (you should adequately rise your compiler warning level) and an assignment to an element outside the array (i has value 5, now). It's the result of this assignment, comp, that is used as a condition, which means that the branch is executed whenever comp is not 0.

Or, at least, this is the most likely outcome, given that that assignment is UB too and the compiler could genereate whatever code it decides or your program could seg-fault due to that access out of bounds.

Bob__
  • 12,361
  • 3
  • 28
  • 42
  • I'd rather *not* see C arrays showcased for C++ answers anymore... and range-for is a thing, too. – DevSolar Feb 10 '20 at 10:10
  • @DevSolar Me neither, but I can't control what is teached to the askers. – Bob__ Feb 10 '20 at 10:12
  • I'd `if (!std::cin >> number)` – Caleth Feb 10 '20 at 13:03
  • @Caleth Yes, but I tried to keep it as clear as possible and the `!` operator in a separate line *may* be more readable. – Bob__ Feb 10 '20 at 13:58
  • thank you so much for the help i'll keep this info noted and try the code.. im really sorry if my code isn't well written because i'm just new here, plus i'm just following what codes sample my teacher gave me.. But thank you so much for helping me – Yami Feb 17 '20 at 10:45
-1
#include "stdafx.h"
#include <iostream>


int _tmain(int argc, _TCHAR* argv[])
{

 int num[4];
 int i;
 int comp;
 int coo;
 int flg=0;

std::cout<<"Please input four numbers: "<<"\n";
 for (i=0;i<=3;i++)
{
 std::cin>>num[i];

}
 std::cout<<"Now please input another number to compare: ";
 std::cin>>comp;

 for (i=0;i<=3;i++)
{
     if (num[i]==comp){
       flg = 1;      
       break;
    }
 }

 if (flg) {
     printf("The number you inputted is present in the array.\n","%s");
 } else {
     printf("It is not present in the array\n", "%s");
 }
std::cin>>coo;
return 0;

}

Basically, you ommitted to loop through all the array members when comparing the array with the number to be compared and I just added the loop in so that it looped through all array members and compared it with the input number.

Ezani
  • 535
  • 3
  • 18