简体   繁体   中英

c++ check if the integer is in the array

I need help. I have an assignment that says:

Asks the user to type 10 integers of an array and an integer v. The program must search if v is in the array of 10 integers. The program writes "v is in the array" if the integer v is in the array or "v is not in the array" if it's not.

My code seems fine, but it does not work properly. Please help.

Here's my code:

#include <iostream>
#include <conio.h>
#include <stdlib.h>
using namespace std;

int main () {
    const int size = 10;
    int vars[size],temp = 0,v = 0;
    int boolean = 0,choice;
    string check = "";
    for(int x = 0; x<10; x++){
        cout<<"Enter 10 Numbers: ";
        cin>>vars[x];
    }

    do{
        cout<<"Enter V variable :";
        cin>>v;

        for(int x = 0; x <10; x++)
        {
            temp = vars[x];
            if(temp == v){
                check = "v is in the array";
            }
            else{
                check  = "v is not in the array";
            }
        }
        cout<<check;
        cout<<"\nContinue ?"<<endl<<"[1]yes"<<endl<<"[2]no"<<endl;
        cin>>choice;
        system("cls");
        for(int x = 0; x<10;x++){
            cout<<"index" <<x<<" = "<<vars[x]<<endl;
        }
    } while(choice != 2);
    return 0;
}

The lack of any IO error checking notwithstanding, you should be establishing your messaging check value based on completed iteration, not based on each iteration.

This:

    for(int x = 0; x <10; x++)
    {
        temp = vars[x];
        if(temp == v){
            check = "v is in the array";
        }
        else{
            check  = "v is not in the array";
        }
    }

    cout << check;

will execute the loop iteration size times no matter what, resetting check with each iteration and only printing the last iteration results. What you want is something like this:

    int x = 0;
    for(; x <size && vars[x] != v; ++x);

    if (x == size)
        std::cout << v << " is NOT in the array";
    else
        std::cout << v << " is in the array";

Or better still, use the standard library and stop reinventing the wheel:

    auto it = std::find(std::begin(vars), std::end(vars), v);
    if (it == std::end(vars))
        std::cout << v << " is NOT in the array";
    else
        std::cout << v << " is in the array";

You are running the loop 10 times. Even if the first number is the answer you are looking for, the second iteration of the loop will rewrite your "check" variable.

Either break on the if in order to exit the loop or change your logic.

You look like a beginner, so refer to this to read more about break statements.

Your program is correct except the most important thing is in your loop when the condition succeeds meaning the number V is found you must break n order not to be changed the next iteration. So as quickly as it is found don't continue iterating over all the elements of the array break.

for(int x = 0; x <10; x++){
    if(vars[x] == v){
        check = "v is in the array";
        break;// You must add break to get out of the loop with `check = "v is in the array"`.
    }
    else{
            check  = "v is not in the array";
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM