A program to see Input in C + + as the logical __c++ of judgment statements

Source: Internet
Author: User
Tags numeric value

Count the number of times each value in the input appears:

int currval = 0, val = 0;
	if (std::cin >> currval) {
		int cnt = 1;
              while (Std::cin >> val) {
			if (val = = currval)
				++cnt;
			else {
				std::cout << currval << "occurs" << cnt << "Times" << Std::endl;
				Currval = val;
				CNT = 1;
			}
		}

Std::cout << currval << "occurs" << cnt << "Times" << Std::endl} return 0;

When you run the program by compiling it:


A row enters a set of data and is separated by a space, which is the correct input method for the program.



When you enter a number and press ENTER one at a time, you will find that the results are different from the previous result.



Analysis of the program, you can know:

if (std::cin >> currval)

is to determine whether there is an integer input, the entire program is the core part of the entrance, when the first number of input, the program will enter the body of If.

At this point, define the integer variable:

int cnt=1;

CNT is the function of a counter that is commonly used in while and for loops for recording times. This program is worth the same number of times as the record.

Then the program goes into the most important while body:

while (Std::cin >> val) {
			if (val = = currval)
				++cnt;
			else {
				std::cout << currval << "occurs" << cnt << "Times" << Std::endl;
				Currval = val;
				CNT = 1;
			}
		}

The first number entered is stored in the cuurval in the judgment of the IF (), while the while () is still in the constant input of new values into Val;

The IF () in the while body is judged to be "val = = Currval" And, if it is equal, the counter CNT plus 1;

For example, we enter three digits: 12 12 12, the first digit 12 is stored in the Currval (Ps. Currval meaning is the current value, meaning that the current value is 12, the second value 12 is stored in the Val, then the comparison, if it is equal, is cnt+1; if not, output the counter and store the new value in the Currval.

So the value to the last output is 12.

However, there is a problem at this time, if


Enter the number and press enter directly, the output is only two lines, why is this.

The reason is that the first if () judgment statement enters a variable that is an integral type, so that the program is actually executing only if the input is not an integer value or EOF.

So 46 has to have another integer input to have today while loop.



When we enter the last numeric value of type ' a ', we can actually execute and end the program, which is the correct way to use this program.



Why is the second input method so awkward?

The reason for this is: Enter a number, press ENTER, go to the next loop, and use the next entered value to trigger the while and while if.




So, by this program we can know that in C + +, when we debug and use the program, we need to figure out:

1. input type of variable;

2.if and the condition of the end of the while loop;

3. The correct use of the procedure, need to understand the program on the basis of the correct use of the program.

//************************************************************************************************************* ************

Reference: "C + + Primer Chinese Version" (fifth edition)



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.