Incorrect output. C++ primer 1.4.4

前端 未结 1 1449
醉话见心
醉话见心 2021-01-28 10:40

the following program is supposed to count the number of times a user inputs a integer. example: user inputs 42 42 10 10. the program is supposed to out put : 42 occurs 2 times,

相关标签:
1条回答
  • Your program as-written appears correct for a series of numeric inputs separated by whitespace.

    You need to give an end-of-file indication to the program so that it will exit the while loop and print the count for the final data. In Windows, you can do that by entering [Ctrl]-[Z] as the first character on a new line. In Linux, UNIX and Mac OS X, [Ctrl]-[D] serves a similar purpose.

    Alternately, you can put your set of values into a text file and use redirection to feed your program. Suppose, for example, you put your data in a file named data.txt in the same directory as your executable. In a terminal window, you can run your program as follows:

    myprogram < data.txt
    

    As some others have noted, a non-numeric input will also work in place of end of file. For example, you could enter 42 42 10 10 fred, and it'll output what you expect as well. That doesn't appear to be the intent of the program, though. For example, if you input 42 42 10 10 fred 37, the program stops at fred and won't see 37.

    0 讨论(0)
提交回复
热议问题