Input /n

Hello,

I am writing a code which will be binary decimal converter. I know how to write rest of code to be functional.

I have problem with only one thing. When user type Enter and not any character, code continue to line with result, but I need when user type Enter without characters to continue to line "Not good". I tried many things but I do not know how to do it.

Could you give me some advice?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
  #include<iostream>

using namespace std;

int main()
{
    int res = 0;
    char a;
    int counter;
    cout<<"Type:"<<endl;
    while((a=cin.get())!='\n')
    {if ((a != '1' &&  a != '0') || cin.fail()) {
    cout << "Not good." << endl;
    return 0;}
    else
        res=(a);
        counter++;
    }
cout << "res is: " << res << endl;
    return 0;
}
Hello Alastor,

The program work correctly the way you wrote it. Try this version and maybe you will be able to see what is going on.

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
#include<iostream>
#include <chrono>
#include <thread>

using namespace std;

int main()
{
	int res = 0;
	char a;
	int counter{};

	cout << "Type: ";  // <--- Added a space and removed the "endl".

	while ((a = cin.get()) != '\n')
	{
		if ((a != '1' &&  a != '0') || cin.fail())
		{
			cout << "Not good." << endl;
			std::this_thread::sleep_for(std::chrono::seconds(3));  // Requires header files "chrono" and "thread"
			return 0;
		}
		else
			res = (a);
		counter++;
	}

	cout << "res is: " << res << endl;

	std::this_thread::sleep_for(std::chrono::seconds(3));  // Requires header files "chrono" and "thread"

	return 0;
}


Notice the use of blank lines and indenting that makes the code easier to read.

In the while condition if you enter "1\n" from the keyboard the "cin.get()" will extract the "1" leaving the "\n" in the input buffer. Next time when the while condition is checked it will extract the "\n" from the buffer and cause the condition to be false so the while loop ends. And if you just press enter the while condition fails leaving "res" with no value.

The pauses I put in the program should allow you to see what is printed to the screen before the program ends. With your version the program ends and may close the window before you have a chance to read anything.

The program works well once, but could use some improvement if you want.

Hope that helps,

Andy
Last edited on
Thanks for answer.

If I just press enter, "res" has value 0. Does it mean: no value=0?

I tried to search something about it and I only find that this empty value ends like this '\0'.

But I still do not know how to get it into my "if" conditions.
Hello Alastor,

But I still do not know how to get it into my "if" conditions.

If you just press "enter" the while loop condition fails and you never enter the loop. The if statement will never be reached. Same thing happens when you enter a character and press enter. The second time through the loop the "\n" is extracted from the input buffer, the while loop fails and you continue to the first statement after the while loop.

If "res" prints zero that means that its value was never changed by the program and since it was initialized to zero when it was defined it will always be zero until changed to something else.

Hope that helps,

Andy
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>

int main()
{
    int res = 0;
    char a;

    // extract characters till a non-white-space character or new line is extracted
    while( std::cin.get(a) && a != '\n' && std::isspace(a) ) ;

    if( a == '\n' ) // got a new line
    {
        std::cout << "not good\n" ;
        return 1 ;
    }

    std::cin.unget() ; // put the extracted character back into the input buffer

    if( std::cin >> res ) // and then try to read an int
    {
        std::cout << "you entered " << res << '\n' ;
        return 0 ;
    }
    else // integer input failure
    {
        std::cout << "input error: not an integer\n" ;
        return 1 ;
    }
}
Thank you all for your help.

Especially I have to thank Andy. It took me some time, but finally I realized where I have to place if statement.

When I am looking at it now, it is really simple.
Topic archived. No new replies allowed.