我需要幫助。我有一個任務說:C++檢查整數是否在數組中
要求用戶鍵入10個整數的數組和整數v。該程序必須搜索如果v是在10個整數的數組。如果整數v在數組中或者「v不在數組中」,那麼程序寫入「v在數組中」,如果不是,則寫入「v不在數組中」。
我的代碼似乎很好,但它不能正常工作。請幫忙。
這裏是我的代碼:
#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;
}
歡迎來到Stack Overflow!這聽起來像你可能需要學習如何使用調試器來遍歷代碼。使用一個好的調試器,您可以逐行執行您的程序,並查看它與您期望的偏離的位置。如果你打算做任何編程,這是一個重要的工具。進一步閱讀:[如何調試小程序](http://ericlippert.com/2014/03/05/how-to-debug-small-programs/) – NathanOliver
和https://stackoverflow.com/questions/2069367/如何調試使用gdb – Yunnosch
'{檢查=「v在陣列中」;打破; }' –