The following code has been simplified as much as possible and is supposed to do the following :
1) user populates list of 5 integers.
2) user inputs a 'number'.
3a) IF 'number' is the same as one of the 5 integers - double that number will be shown, then repeat 2.
3b) IF 'number' is NOT the same as one of the 5 integers - end program.
However during runtime I enter a number which is in the list, and get the output response saying its not on the list. also with this else statement being activated one would expect the while loop to stop as a = 0, however it continues to loop.
here is where I think the error lies regarding number not showing on list :
if (number == list[x])
I may have used the wrong statement within the parenthesis.
However I have no idea why it also continues to loop, perhaps both errors are because of the same thing. Any help with making the program run as stated above would be greatly appreciated.
Here is the full code :
#include <iostream>
using namespace std;
int main()
{
int list[5];
int a, x, number;
a = 1;
cout << "Fill up the list" << endl;
for (x = 0; x <= 4; x++){
cin >> list[x];
}
while (a = 1) {
cout << "enter one of the numbers you put on the list" << endl;
cout << "we will double it " << endl;
cin >> number ;
if (number == list[x])
{
cout << "double that number is " << number * 2 << endl;
}
else
{
cout << "Thats not on the list" << endl;
a = 0;
}
}
return 0;
}
Aucun commentaire:
Enregistrer un commentaire