How come this does not work

Hi I tired posting yesterday but to no avail. This is a really simple code, but can someone help me understand why the cout in the first while loop is not being processed? It becomes processed if I removed the second while loop/if loops, but when they are in the code it refuses to process the
1
2
std::cout << input << " was entered " << cnt << " times." << std::endl;
return 0;
at the bottom of the while loop. Shouldn't the code bypass the second while loop if I only input 1 integer?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>

int main ()
{

    int input = 0, input2 = 0, cnt = 0, x = 0;

    while (std::cin >> input)
    {
        cnt = 1;
        while(std::cin >> input2)
              {


                    if (input == input2)
                    {
                    ++cnt;
                    }
                   if(input2 != input)
                    {
                    std::cout << input << " was entered " << cnt << " times." << std::endl;
                    input = input2;
                    cnt = 1;
                    }

              }
std::cout << input << " was entered " << cnt << " times." << std::endl;
return 0;


     }

}
If i understand you question correctly, than no, you always have to input two integers for the program to work. The return 0; is not properly placed. It has to be placed right before the last curly bracket.
Oh ok thanks about the return 0.

But can I ask why the program needs two integers? Shouldn't the first while look take the first integer, then define cnt = 1. Then check if there is a second integer(if there is run the second while loop). If there is not a second integer, shouldn't it ignore the second while loop and the if's then execute the remaining code in the first while loop which is to cout "input"?

I don't understand why the second while loop is overriding the remaining code int the first while loop :S
Last edited on
There are always to integers because the 2nd while loop is inside of the 1st and as there aren't any ifs statements condioning if there is or if there isn't a 2nd integer you'll always have to input 2 integers
Oh, so you're saying that the first while loop runs when the first integer is inputed, then the second while loop will stay active until a second integer is inputted no matter how many times I hit enter, unless I write an if statement to say something like "If input == //no input // // execute command" ?(sorry I have no idea how to write this yet). Well ok, I'll work into that.

But how come it ignores the final cout? It should execute that line once the second while loop is completed no?
How certain are you that the final cout is ignored? Since it is identical to line 21. It might execute and you not realize it.
But how come it ignores the final cout? It should execute that line once the second while loop is completed no?


The second while loop is only completed when input fails. Are you causing the input to fail? (Entering something other than a number or whitespace, for instance?)
This is what happens.

-When I input -

1 2 3

-It returns -

1 was entered 1 times.

2 was entered 1 times.

- When I input a second time -

4 5 6

-It returns -

3 was entered 1 times

4 was entered 1 times

5 was entered 1 times


------
It just stores the last integer until I input more data a second time. But even then it still excludes the final didget. I've tried just typing in std::cout << "test";
even then it doesn't want to acknolwedge that there is code after/outside the second while loop.
Again, are you doing anything to make the input fail, such as enter a character which cannot be interpreted as part of a number (e.g. 'a')?

Does the console window close or remain open?
Again I typed out exactly what I wrote.

it remains open, so I'm assuming it's still active on one of the while loops, more than likely the second.

(specifically , i just enter in numbers)

---- Edit ---

Ahhh I see what you mean. Sorry, it probably is like talking to someone from another planet. Ok so the second while loop isn't failing/completing because I haven't set anything to finish it, so the final contents of the first while loop aren't getting a chance to initate.

Great thanks :) I'll try and figure out how to get it to do this now. I appricate the info
Last edited on
Ax7 after I stopped replying yesterday, I messed with your code trying to execute it the way you want to. I'm not going to lie, I'm not able to figure out how to get those last numbers from the input to register.

It be so much easier if you allowed the user to enter a number followed by pressing enter.
Oh that's cool, thanks for giving it a shot sherre02. I'm just learning so I'm not sure what you mean by it being easier by making the user enter the numbers followed by pressing enter.
In the one I've posted that's what you do, or are you meaning doing it individually 1 number/enter at a time? Or somehow defineing enter to be a end value of sorts?

In anycase, so far a way that "works" is to enter in a non-int value after the final integer(cire pointed this out). I don't really know the anatomy of C++ yet so, I'm a bit unsure how to set it up so break out of the second while loop when the final integer has been inputted.
Topic archived. No new replies allowed.