Write a program which takes input as integer only that is restrict other charactersโ€‹ formโ€‹ inputs ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Write a program which takes input as integer only that is restrict other charactersโ€‹ formโ€‹ inputs ?

c++ program which is important for a good programmer.

26th Mar 2017, 7:57 AM
Harish kumawat
Harish kumawat - avatar
11 Answers
+ 14
int main() { int x; while (!(cin >> x)) { cin >> x; cin.clear(); cin.ignore(512, '\n'); } cout << "You entered : " << x; return 0; }
26th Mar 2017, 8:40 AM
Hatsy Rei
Hatsy Rei - avatar
+ 13
What do you mean by removing one digit?
26th Mar 2017, 8:41 AM
Hatsy Rei
Hatsy Rei - avatar
+ 5
Please refer https://code.sololearn.com/cvbzeX3s8MN7/#cpp Try it on your PC as it may not work on Playground
26th Mar 2017, 8:31 AM
เคฆเฅ‡เคตเฅ‡เค‚เคฆเฅเคฐ เคฎเคนเคพเคœเคจ (Devender)
เคฆเฅ‡เคตเฅ‡เค‚เคฆเฅเคฐ เคฎเคนเคพเคœเคจ (Devender) - avatar
+ 4
#include<iostream.h> #include<conio.h> int getintegerOnly(); int main() { int x=0; clrscr(); x=getintegerOnly(); cout<<"/n you have "<<x; getch(); return 0; } int getintegerOnly() { int num=0;char ch; do { ch=getch(); if(ch>=48&&ch<=57) { cout<<ch; num=num*10+(ch-48); } if(ch=='\b') { clrscr(); num=num/10; cout<<num; } if(ch==13) { return(num); }}while(1);}
27th Mar 2017, 4:07 PM
Harish kumawat
Harish kumawat - avatar
+ 3
suppose first input 1 and second input 2 and third input 3 then we can see 123 on console screen but we want to remove 3 (last digit of number) on Console screen then output 12 . if want to remove again last digit then output 1.
26th Mar 2017, 9:11 AM
Harish kumawat
Harish kumawat - avatar
+ 2
if we want to removing one digit on Console screen when what do you do?
26th Mar 2017, 8:35 AM
Harish kumawat
Harish kumawat - avatar
+ 2
Great! But I have a doubt whether clrscr () function is supported by current C++ version
27th Mar 2017, 4:13 PM
เคฆเฅ‡เคตเฅ‡เค‚เคฆเฅเคฐ เคฎเคนเคพเคœเคจ (Devender)
เคฆเฅ‡เคตเฅ‡เค‚เคฆเฅเคฐ เคฎเคนเคพเคœเคจ (Devender) - avatar
+ 2
Then hats off to you I tried the same way back in 1995, hard to recall.
27th Mar 2017, 4:16 PM
เคฆเฅ‡เคตเฅ‡เค‚เคฆเฅเคฐ เคฎเคนเคพเคœเคจ (Devender)
เคฆเฅ‡เคตเฅ‡เค‚เคฆเฅเคฐ เคฎเคนเคพเคœเคจ (Devender) - avatar
+ 2
Make a little change - use getche () instead of getch (). You will see the difference.
27th Mar 2017, 4:25 PM
เคฆเฅ‡เคตเฅ‡เค‚เคฆเฅเคฐ เคฎเคนเคพเคœเคจ (Devender)
เคฆเฅ‡เคตเฅ‡เค‚เคฆเฅเคฐ เคฎเคนเคพเคœเคจ (Devender) - avatar
+ 1
no sir but my answers according to turbo c++
27th Mar 2017, 4:14 PM
Harish kumawat
Harish kumawat - avatar
0
yes sir but we want to taking only integers value according to question .when we use getche() we can takes character and integers so we can use getch().
27th Mar 2017, 4:51 PM
Harish kumawat
Harish kumawat - avatar