#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
    char a_char;
    int cu = 1;
    while (cu != -1)
    {
          cin>>a_char;
          cout<<"Is a digit!" << a_char << endl;
          
    }
    system("PAUSE");
    return EXIT_SUCCESS;
}

When I input a132, the output is:
Is a digit!a
Is a digit!1
Is a digit!2
Is a digit!3

my expect result is Is a digit!a123, why my code doesn't work? plx help

> char a_char;
Well you asked for only one char, and that's what you got.

Consider a char array (or better, a std::string ) if you want to store many chars in the same object.

> char a_char;
Well you asked for only one char, and that's what you got.

Consider a char array (or better, a std::string ) if you want to store many chars in the same object.

thank you for your help, but do you know why will it happen in this way. why the print out result is not Is a digit!a123 ?

As salem stated you are asking for an input of a char which is a sinlge digit or letter.

so this can only give one letter at a time however, you are also using a while loop so you are
getting first

char digit;
int count(0);
while(cu != -1)
{
//this line might make it clearer
cout << "call number: "<< count << std::endl;
cin>> digit;
cout << "is digit: " << digit << endl;
}

this while loop runs four times
if you use std::string you will need to change your stop condition
but it will take in a whole word at a time.

so to read in one word

#include <string>
#include <iostream>
using namespace std;

int main()
{
string multiple_digits;
cout << "enter a number" <<endl;
cin >> multiple_digits;
cout << "Is digit:" << multiple_digits << endl;
return 0;
}

To check that this is a number will require a function
say:

bool check_only_numbers(std::string &str)
{
bool ret(true);
   int sz = (int) str.size();
  for(int i = 0; i < sz; ++i)
 {
    if(str[i] < '0' || str[i]  > '9')
    {
       ret = false;
       break;
    }
 }
return ret;
}

there are cleaner ways to write this last function

thank you for your help, but do you know why will it happen in this way. why the print out result is not Is a digit!a123 ?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.