Write a program that accepts integers for as long as the inp

Write a program that accepts integers for as long as the input is positive. If a negative or 0 is input, the program would stop and display the number of inputs that are equal to the first number entered. Example: if the inputs were 1, 2, 3, 1, 4, 1, 4, 1, 3, -1 the program would display: The number of numbers similar to the first one is 3. Write a function that asks the user to input an integer and returns it; that is a function that gets an integer from the keyboard and returns it. int getInt() {...} Include the above function in you previous program.

Solution

Count.cpp

#include <iostream>

using namespace std;
int getInt();
int main()
{
int n=0, first;
int matchCount = 0;
int count = 0;
n = getInt();
while(n>0){
count++;
if(first == n){
matchCount++;
}
if(count == 1){
first = n;
}
n = getInt();
}
cout<<\"The number of numbers similar to the first ont is \"<<matchCount<<endl;
return 0;
}
int getInt(){
int n=0;
cout<<\"Enter a number: \";
cin >> n;
return n;
}

Output:

sh-4.3$ g++ -std=c++11 -o main *.cpp                                                                                                                                                                                             

sh-4.3$ main                                                                                                                                                                                                                     

Enter a number: 1                                                                                                                                                                                                                

Enter a number: 2                                                                                                                                                                                                                

Enter a number: 3                                                                                                                                                                                                                

Enter a number: 1                                                                                                                                                                                                                

Enter a number: 4                                                                                                                                                                                                                

Enter a number: 1                                                                                                                                                                                                                

Enter a number: 4                                                                                                                                                                                                                

Enter a number: 1                                                                                                                                                                                                                

Enter a number: 3                                                                                                                                                                                                                

Enter a number: -1                                                                                                                                                                                                               

The number of numbers similar to the first ont is 3

 Write a program that accepts integers for as long as the input is positive. If a negative or 0 is input, the program would stop and display the number of input
 Write a program that accepts integers for as long as the input is positive. If a negative or 0 is input, the program would stop and display the number of input

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site