0

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, 10 occurs 2 times.

the problem: the code will not output the last result for the number 10 until you input another number. i have pasted the code below. this code comes from c++ primer. 1.4.4

#include <iostream>
int main()
{
    // currVal is the number we're counting; we'll read new values into val
    int currVal = 0, val = 0;

    // read first number and ensure that we have data to process
    if (std::cin >> currVal) 
    {
        int cnt = 1;  // store the count for the current value we're processing

        while (std::cin >> val) 
        { // read the remaining numbers

            if (val == currVal)   // if the values are the same
                ++cnt;            // add 1 to cnt
            else 
            { // otherwise, print the count for the previous value
                std::cout << currVal << " occurs " << cnt << " times" << std::endl;
                currVal = val;    // remember the new value
                cnt = 1;          // reset the counter
            }

        }  // while loop ends here

        // remember to print the count for the last value in the file
        std::cout << currVal <<  " occurs " << cnt << " times" << std::endl;
    } // outermost if statement ends here

    return 0;
}
Joe Z
  • 17,413
  • 3
  • 28
  • 39
Matt
  • 69
  • 1
  • 1
  • 7
  • 1
    Using the code as it is, you need to input a non-numeric string as well as those numbers and you'll get the count without having to input a new number. – splrs Dec 15 '13 at 23:24
  • @splrs, i am very new to programming. could you provide an example of what a "non-numeric string" is ? or an example of the code needed to correct the output issue ? – Matt Dec 16 '13 at 00:21
  • Try entering 10 10 42 42 _z_. That'll give you the correct counts and won't start another i.e. the program will finish. – splrs Dec 16 '13 at 00:22
  • This section of the book made me wonder why this book comes so highly recommended: it is not well explained, it doesn't behave as they describe, and is a pedagogical nightmare all around. FWIW, I just put the following at the beginning of my program: `std::cout << "Enter space-delimited integers and a letter to finish." << std::endl;` This is basically one of Joe Z's suggestions. Not sure why you didn't accept his answer? – eric Feb 23 '18 at 14:54

1 Answers1

2

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.

Joe Z
  • 17,413
  • 3
  • 28
  • 39
  • This is a great answer and should be accepted. This section of the book is sort of awful, frankly. – eric Feb 23 '18 at 14:49