1

My program has no problems detecting space which i store in a character variable. The program returns zero but does not return one.

edit: changed '/n' to '\n' (typed wrong escape sequence for a new line) Also my question does not relate to the suggested question: std::cin:: and why a newline remains because that question is about a stray new line sequence in buffer as per my understanding while i simply demonstrated my failed attempt at trying to store an enter key in a char variable and using an if statement as a flag to detect an enter key.

This set up now works with the flag outputting one. However, I am still keen on learning new ways to store an enter key into a variable so that i can detect it via a flag or any other setup.

#include <iostream>

using namespace std;

int main()
{
char a;
cin.get(a);
if(a=='\n')
cout<<"one";

if(a==' ')
cout<<"zero";

return 0;
}
  • Does this answer your question? [std::cin:: and why a newline remains](https://stackoverflow.com/questions/15904963/stdcin-and-why-a-newline-remains) – tevemadar Sep 03 '22 at 09:30
  • `'/n'` is not a carriage return. The escape sequence for that is `'\n'` – UnholySheep Sep 03 '22 at 09:38
  • @UnholySheep Isn't `'\n'` a newline while a carriage return is a `'\r'`? That is not always the same. There are weird environments out there which make a relevant difference.... – Yunnosch Sep 03 '22 at 09:43
  • 1
    @Yunnosch right, my mistake (I always forget about those weird differences) - though OPs `'/n'` is definitely wrong in either case – UnholySheep Sep 03 '22 at 09:44
  • That for sure, true. – Yunnosch Sep 03 '22 at 09:45
  • The language allows specifying an integer using a *multi-character constant* like `'/n'`, which is an integer with the value of **12142** (on ASCII machines). It's a bit of a legacy wart in the language. "The value of an integer character constant containing more than one character (e.g., 'ab'), [...] is implementation-defined." – Eljay Sep 03 '22 at 11:45
  • My bad. I corrected it and it works but @Yunnosch apparently '\n' works like an enter (carriage return) too, atleast in a case of detecting stored information. – Ahmed Abdullah Sep 03 '22 at 12:02
  • @UnholySheep I corrected it and the program works fine now, but my question is open ended and still valid. Thanks for letting me know. – Ahmed Abdullah Sep 03 '22 at 12:03
  • @Eljay Thanks for pointing that out. My question is about different ways a program can be prompted by an enter key to behave a certain way or more simply how a program can read an enter by the user and perform a task using a flag etc. – Ahmed Abdullah Sep 03 '22 at 12:09
  • You are most welcome. I wanted to make sure you understood why the typo was not caught by the compiler. – Eljay Sep 03 '22 at 12:22
  • @James Risner What are the different ways i can detect an enter by the user of a c++ program to do a specific task or run a specific part of the program. My method is just one way to do this. I would like to know other ways to achieve the same result. – Ahmed Abdullah Sep 08 '22 at 02:36
  • I'm sure there are other ways, I'm unsure those ways will be martially different than your way. – James Risner Sep 11 '22 at 14:20

0 Answers0