Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

C++ How can i use default value when user just press enter?

Status
Not open for further replies.

islouis

Junior Member level 1
Joined
Sep 24, 2004
Messages
17
Helped
1
Reputation
2
Reaction score
0
Trophy points
1,281
Activity points
194
c++ cin default

Dear Sir:

May i know how can i use the default int value, when user didnt enter any value and just press enter?

I have used the following code but didnt quit work, assume the default value is int 50, Thanks for any help!

char Chr;
int number;
number=cin.get();
if (Chr==10) // When press enter without a value Chr=10?
number=50;
else
number=Chr;

cout << number;
 

c++ cin default value

your question is logicaly wrong . can you expalin again .

your are trying to set value of number with cin.get() but check the value of chr

char chr is not initilaized so you can not guess its initial value
 

default integer value + int + cpp

Sorry for any mistake. Actually, I want to write a program that will prompt for a user to enter an int value. If however, the user did not enter any value and just pressed enter, the program will use the default value.

For example

Int i=50; set the default value is 50

cout << "Please input a value\n";
cin>>i; \\ <-- in this line, if the user didnt input any value and just press enter, i
want int i use the default 50 as declare in the first place. But i dont
how to do that? And of course if it user did enter a value, then
the program will use the value that user input.

Thanks for any advice!!

Added after 4 minutes:

And the code i used should be:

char Chr;
int number;
Chr=cin.get(); // get a value
if (Chr==10) // When press enter without a value Chr=10?
number=50; //if only press enter, use 50
else
number=Chr; // if there is an input, use the input value

cout << number;

The problem here is, cin.get() return a char, not an int, so it is not working. Is there any better way to achieve this?

Thanks!
 

c++ default cin value

use atoi()

the function convert ascii to integer.....
in the code is:

Char Chr[10]; //Chr is array
.
gets(Chr);//get strings
if(Chr[0]==10)//if Chr[0] is space
number=50;
else
number=atoi(Chr); // is valid


number=Chr; // if there is an input, use the input value IS INVALID only char!!!

enjoy

Added after 15 minutes:

do you want to get ONLY A char???
 

cin default value c++

Thanks a Lot Sir, it works!

char Chr[10]; //Chr is array
int number;
gets(Chr);//get strings
if(Chr[0]==0)//if Chr[0] is space
number=50;
else
number=atoi(Chr); // is valid

cout << number;

BTW, instead of using Char, is that possible to use Int for all variable?
thanks!!
 

what is the default value for integer in cpp?

No. you can Array of Char and convert to int, float or double
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top