Given a int variable named yesCount and another int variable named noCount and a char variable named response , write the necessary code to read a value into into response and then carry out the following: if the character typed in is a y or a Y then increment yesCount and print out “YES WAS RECORDED” if the character typed in is an n or an N then increment noCount and print out “NO WAS RECORDED” If the input is invalid just print the message “INVALID” and do nothing else.

1
Given  a int  variable  named  yesCount and another int  variable  named  noCount and a char  variable  named  response , write the necessary code to read a value  into into response and then carry out the following: if the character  typed  in is a y or a Y then increment  yesCount and print out “YES WAS RECORDED” if the character  typed  in is an n or an N then increment  noCount and print out “NO WAS RECORDED”  If the input is invalid just print the message  “INVALID” and do nothing else.

LANGUAGE: C++

CHALLENGE:

Given a int variable named yesCount and another int variable named noCount and a char variable named response , write the necessary code to read a value into into response and then carry out the following: if the character typed in is a y or a Y then increment yesCount and print out “YES WAS RECORDED” if the character typed in is an n or an N then increment noCount and print out “NO WAS RECORDED” If the input is invalid just print the message “INVALID” and do nothing else.

SOLUTION:


cin >> response;

if (response == 'y' || response == 'Y'){
	yesCount++;
	cout << "YES WAS RECORDED";
}else{
	if(response == 'n' || response == 'N'){
		noCount++;
		cout << "NO WAS RECORDED";
	}else{
		cout << "INVALID";
	}
}

One comment

  • Ben Large says:

    Here is the code in C:

    scanf(“%c”, &response);

    if (response == ‘y’ || response == ‘Y’)
    {
    yesCount++;
    printf(“YES WAS RECORDED”);
    }
    else
    {
    if(response == ‘n’ || response == ‘N’)
    {
    noCount++;
    printf(“NO WAS RECORDED”);
    }
    else
    {
    printf(“INVALID”);
    }
    }

Add comment