how the user enter a value ?

Pages: 12
so, thats my question, I know the code for that ? Whta the user needs to press to enter a value ? With "enter" key, c++ windows closes, in case I am not doing 100% clear , what fisicall key from keyboard the user needs press



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
  using namespace std;

 
int main () 
{ 
   
int a=5;

 cout <<"enter a number   ";
 cin>> a;   HERE, HOW THE USER PUTS A VALUE FOR a ?????
 if (a==5)
  cout<< "yes!!!!";
  else 
   cout<<"no no no";
    cin.get ();
    return 0;
    
}
I'm quite new also but,

your int a shouldn't equal the same value your testing for so you might want to change it to 0,

cin >> a; //asks the console for an input, you would type 5 in here.
you may want to ignore the "enter" key press

you will need to put some { } for your if statement

and a cin.get(); // This stops the window from closing early

Then you need to compile the program
Hi there,

Please take no offence, I just want to help you by clarifying a few things you said:

x123xx wrote:
you may want to ignore the "enter" key press

The enter key press will be automatically ignored, no need to explicitly do so.


x123xx wrote:
you will need to put some { } for your if statement

Technically, this is not always required, in fact it is common practise to not use curly braces if it involves very simple if/else constructs:

1
2
3
4
if (true)
    std::cout << "Yup, true.";
else
    std::cout << "So false.";


For anything else than such short blocks I would indeed recommend curly braces as it increases readability a lot it is a requirement.


@pupu - could you please clarify your question? If you are having trouble translating your question into english, you may want to try formulating your question clearly in your own language, then running it through google translate.

All the best,
NwN
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
using namespace std;

int main()
{
int a = 5;
cout<<"Enter a Number\n";
cin>>a;

if ( a == 5 )
{
cout<<"Yes!\n";
}
else
{
cout<<"No!\n";
}
cin.get();
return 0;
}


or if the window is still closing without you pressing anything, just create a variable

1
2
3
int k;
cin>>k;
return 0;


force it to remain opened until you're done with it.
Last edited on
For anything else than such short blocks I would indeed recommend curly braces as it increases readability a lot.

Um, if you want more than one statement in your conditionally-executed block, you have to use curly braces. It's not a recommendation, it's a necessity. Not because of readability, but because if you don't, only the first statement will be considered to be dependent on the condition. Consider the following:

1
2
3
4
5
6
7
8
9
10
11
bool bFlag;

// ...

if (bFlag)
  std::cout << "bFlag is true" << std::endl;
else
  std::cout << "bFlag is false" << std::endl;
  std::cout << "yes, it's definitely false" << std::endl;

std::cout << "End" << std::endl;


If bFlag is true, you will get the following output:


bFlag is true
yes, it's definitely false


Because the "else" block doesn't use curly braces, only line 8 is part of the else block. Line 9 is not part of the else block, and is not part of the whole if... else... statement - it's always executed regardless of the value of bFlag.

In fact, even with single-line conditional blocks, I'd recommend using curly braces. I've seen so many bugs introduced into code, because somebody didn't use them, and then they (or someone else) went back and wanted to add a second line to the block, but forgot to put the braces in.

You're best off getting into the habit of putting them in, all the time. It's safer that way.


Last edited on
You are correct. It's what I meant, but I formulated it poorly. To be honest, I quite like the no-brace syntax for "one-liners", especially if you have a few of them, it can really clutter the code.

Of course you have a point about someone else going in and wanting to add to the block, although forgetting to add braces sounds like a rather rudimentary mistake.

All the best,
NwN
It's a "rudimentary mistake" that I've seen made in every company I've ever worked for, over the 20 years I've been a professional C/C++ developer. I've done it myself occasionally, although (to the best of my knowledge) I've always spotted what I've done straight away and fixed it.

Everybody has bad days. Everybody makes mistakes. And sometimes those mistakes don't get spotted until they find their way into production code. Part of the art of good programming is adopting practices that reduce the likelihood of you - or the person who has to maintain your code - making mistakes.

Last edited on
Where is all this discussion coming from? All that is happening is that his cin.get() is absorbing the new line character rather than holding his program open. I linked to the proper way to hold his program open, and you guys all break out into random off-topic discussion?
Where is all this discussion coming from?

People are looking at the OP's code, and coming up with other comments that they believe will be helpful to the OP, over and above the OP's original question.

This can't be the first time you've observed this phenomenon, surely? It's a pretty commonplace part of the board culture here. And - I'd argue - it's an entirely positive, beneficial one, because it gives the OP and other readers a chance to learn things they may not have known.
Last edited on
If you say so, but in this case it seems arbitrary to me.
MikeyBoy wrote:
It's a "rudimentary mistake" that I've seen made in every company I've ever worked for, over the 20 years I've been a professional C/C++ developer. I've done it myself occasionally, although (to the best of my knowledge) I've always spotted what I've done straight away and fixed it.

Everybody has bad days. Everybody makes mistakes. And sometimes those mistakes don't get spotted until they find their way into production code. Part of the art of good programming is adopting practices that reduce the likelihood of you - or the person who has to maintain your code - making mistakes.


Just for the record, I wasn't attacking your point or anything. I don't have any professional programming experience so I fully acknowledge yours and will consider adopting your suggestion. I actually started out writing braces for every statement, but after it came up in a thread around here some time ago it seemed to like it was quite common practise to leave them out for short statements. Common practise and best practise are not necessarily the same thing though. :)

All the best,
NwN
I find that if the window closes sometimes putting
cin.ignore(); before the cin.get();
helps keep it open.
No, that is NOT the correct way to do it. Read the first answer here:
http://www.cplusplus.com/forum/beginner/1988/
> Where is all this discussion coming from?

From here: http://www.cplusplus.com/forum/beginner/105019/

Typically, in all these diatribes regarding the absolute, inviolable superiority of a particular element of coding style, the dramatis personae tend to be the same.

> Common practise and best practise are not necessarily the same thing though. :)

Regarding coding style, what constitutes best practise is a matter of opinion.

From the thread linked to:
Mono coding guidelines:
Avoid using unnecessary open/close braces, vertical space is usually limited:
good:
1
2
if (a)
	code ();


bad:
1
2
3
if (a) {
	code ();
}


http://www.mono-project.com/Coding_Guidelines

There are innumerable others; for instance
GNU: http://www.gnu.org/prep/standards/standards.html#Formatting
VCMI: http://wiki.vcmi.eu/index.php?title=Coding_guidelines#Where_to_put_braces


The GNU or mono developers are not idiots; their collective 'professional C/C++ development experience' is several orders of magnitude greater than 20 years; their point of view is at least as valid as that of anyone else.
Last edited on
sorry LB,

But I still don't understand why I can't use cin.ignore(); when it works so well. I have even read that thread before.

I just had trouble with a string the other day and needed to clear it to hold the console open with cin.get();

What worked for me... was this...
getline(cin.ignore(),myString);
I think what LB was trying to say is that
cin.ignore()
will extract and discard at most one character, and the input buffer may contain more than one character.

While std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );
will extract and discard every character up to (including) the next new line.
thats what my getline did...
> thats what my getline did...

Try this (entering 1234 abcd as requested):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <string>

int main()
{
    int i ;
    std::cout << "please type in '1234 abcd' and then press enter: " ;
    std::cin >> i ; // type in "1234 abcd" ;

    std::cout << "press enter to quit\n" ;
    std::string myString ;
    std::getline( std::cin.ignore(), myString ) ;
    //std::getline( std::cin.ignore( 100, '\n' ), myString ) ;
}


Then, comment out line 12, uncomment line 13, and try it again.
well I see your point but why does doing it the bottom way forgive the attempt to store a string as an int?
Pages: 12