a specific score. it is not working because i type inn 72 and it shows 20. the array only has two 72 scores. then, if i want to type inn another score, i have to type it twice in order for the screen to show an output. this is my code:
#include %26lt;iostream%26gt;
using std::cout;
using std::cin;
using std::endl;
int main()
{
//declare array
int count = 0;
int searchFor = 0;
int scores[20] = {90, 54, 23, 75, 67, 89, 99, 100, 34, 99,
97, 76, 73, 72, 56, 73, 72, 20, 86, 99};
//get input from user
while (searchFor != -1)
{
cout %26lt;%26lt; "Enter a score from 0 to 100: " ;
cin %26gt;%26gt; searchFor;
//search for score equal to the searchFor value
for (int x = 0; x %26lt; 20; x = x + 1)
if (scores[x] = searchFor)
count = count + 1;
//end if
//end for
//display cout
cout %26lt;%26lt; "Count: " %26lt;%26lt; count %26lt;%26lt;endl;
//get input from user
cout %26lt;%26lt; "Enter a score from 0 to 100: " ;
cin %26gt;%26gt; searchFor;
}//endwhile
return 0;
} //end of the searchArray function
Can someone please tell me what's wrong with my C++ program?this program should show how many students earned
There has to be == on the if statement
while (searchFor != -1)
{
// User enters a score
cout %26lt;%26lt; "Enter a score from 0 to 100: " ;
cin %26gt;%26gt; searchFor;
// If score is in the array, then add to count
for (int x = 0; x %26lt; 20; x++)
{
if (scores[x] == searchFor)
count++;
}
// Display count
cout %26lt;%26lt; "Count: " %26lt;%26lt; count %26lt;%26lt;endl;
}
return 0;
}//end of the searchArray function
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment