Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

the loop is in the main program not the loop - I dont print the final results un

ID: 3632750 • Letter: T

Question

the loop is in the main program not the loop - I dont print the final results until I pass in a value of 0 - which triggers the function to display the current values of count2 and count5.
void func_count(int);

int main( )
{
int i, num;

for(i = 1; i < 21; i ++ )
{
scanf("%d", &num);
if(num < 1 || num > 6)
printf("Error: Invalid number %d ");
else
func_count(num);
}
func_count(0);

return 0;
}

Explanation / Answer

Below is a comment version of your code with some changes to make it work. From the code you posted and from your comments, it appeared you wanted a program that did the following: ---Allowed the user to enter integers. ---The integers from 1 to 6 are accept without any issue. ---The integers less than 1 or greater than 6 are not accepted and a warning message is displayed. ---There is a running count of how many times 2 and 5 are entered. ---The program loops 20 times or until a 0 is entered, which ever comes first ---After 20 loops, if a 0 was not entered, no count value is displayed. ---If a 0 is entered by the 20th loop iteration, then the number of times that 2 and that 5 were entered will be displayed. ***********I've also included a suggested version below. Some helpful notes: 1. In your program, you have a prototype: void func_count(int); This is appropriate, however you also need the function declaration after the main() function. I've included the func_count function declaration. 2. I would highly recommend you change the scanf to cin and the printf to cout. The printf command is acceptable, but the scanf command is deprecated, meaning it may not be supported by some systems. *****************HERE IS THE CODE****************************************** #include using namespace std; void func_count(int); int count2=0, count5=0; //declare global variables so the func_count function can use it. //NOTE that this is bad practice to use global variables. However, it was //used because your problem did not specify whether the func_count input parameters //could be changed. //The variables count2 and count5 should be passed to the func_count function //by reference. int main( ) { int i, num; for(i = 1; i < 21; i ++ ) { scanf("%d", &num); if(num==0)//check if 0 was passed in { func_count(num); //if zero was passed in, send it to the function break;//end the loop THIS IS OPTIONAL, //you can keep running this loop if desired. }//end if else if(num < 1 || num > 6) printf("Error: Invalid number %d ",num); else func_count(num); } return 0; } void func_count(int count) { if(count==0) //if 0 was passed into the program, it will pass 0 to the function { cout