cin.get() ?

OK this idea came from General from his/her post I want to catch the "idea" !
so I started to work on it and got stuck but I don't know why my code does work here is an example
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
#include <iostream>

using namespace std;

int main( ) {

  char c;
  int countSix = 0;

  cout << "Enter 4 digits: ";

  for ( int i = 1; i <= 4; i++ )
    {
      cin.get(c);

      cout << c << endl;

      if (c == 6 )
      {
          countSix++;
      }
    }

  cout << endl
  cout << "6's = " << countSix << endl;

  return 0;
}



here the out put when you enter 1236

Enter 4 digits: 1236
1
2
3
6

6's = 0


so it read the number and one of them is a 6 so shouldn't 6's == 1 and not 0?

thanks in advance

Notice that 6 and '6' are two different things. You want to check if the character is ASCII representation of '6', not the exact '6' value
Ooo I see well is there a way that I can read all the 4 number individually ?
Can you elaborate more your last question?
well can I enter a 4 digit number(int) into an input stream and have like a loop of some sort read the each number individually to see if there any six's in the 4 digit number the use has enter?
Last edited on
Aren't you doing it already?
If you want to read decimal ints you could use >> but it would read them separated by whitespaces ( eg: 1236 will produce a single number, not four digits )
What Bazzy is trying to say your inputting char and then comparing the ASCII code of the char and the int in this case the number 6. Check your declaration of c variable.
Topic archived. No new replies allowed.