I can't get the program to run correctly

Write your question here.

Hi guys, Please am new to c++ and am trying to write a program that prompts the user a value(char), and it will be inside a while loop. If the user enters an integers it will continue the loop, but if the user enters a char it will break out of the loop.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>

using namespace std;

int main()
{
    char intvar;
    while( cin >> intvar)
    {
      if(!cin >> intvar)
      {
        cout << intvar << endl;
        break ;
      }
      else
      {
        cout << intvar << endl;
      }
  
    }
    return 0;
}
Print the last non-digit character:
1
2
3
4
5
6
7
8
9
#include <iostream>
#include <cctype>

int main()
{
    char c ;
    // http://en.cppreference.com/w/cpp/string/byte/isdigit
    while( std::cin >> c && std::cout << c << '\n' && std::isdigit(c) ) ;
}


Do not print the last non-digit character:
1
2
3
4
5
6
7
8
9
#include <iostream>
#include <cctype>

int main()
{
    char c ;
    // http://en.cppreference.com/w/cpp/string/byte/isdigit
    while( std::cin >> c && std::isdigit(c) ) std::cout << c << '\n' ;
}
Thanks so much JL, you just thought me something new.
Topic archived. No new replies allowed.