• Register
1 vote
21 views

Problem:

Hello good people,

I am pretty much new in the programming sector. I recently started learning C, and C++. Reading a textbook is hard for me from my childhood. For the obvious reason, I decided to learn from the internet. I am solving basic problems from different websites. Today, I have got a problem that asks for a program where the input will be checked and let you know if it is a number or not! I have totally no idea about this. Could any of you please help me out to write such a program that checks if the input is a number?

I am looking forward to having your solutions. Many thanks.

12 7 7
15,250 points

Please log in or register to answer this question.

1 Answer

1 vote
 
Best answer

Solution:

Well, the thing you are willing to do is called Input-Validation in programming. Input validation is an important trick to know and understand. An invalid and irrelevant input can crash the whole system. So, checking an input before the further process is a good practice. There are a couple of ways to validate input in  c++

I hope the below code snippet will help you to understand the whole process well.

#include <iostream>
using namespace std;

int main(){
    int num;
    cout << "Give me an Input: ";
    // Error Checkin algorithim
    while (!(cin >> num)){
        cout << "ERROR: a number must be entered: ";
        cin.clear();
        cin.ignore(132, '\n');      // Discarding previous input
    }
    cout << "Your favorite number is: " << num << endl;
    cout << "Thanks. Bye." << endl;
    return 0;
}

Good Day!

13 9 6
94,240 points

Related questions

0 votes
2 answers 24 views
1 vote
2 answers 44 views
44 views
Problem: Hello Kodlogs, I am taking an online-based course to learn computer programming. As I am totally new in the business one of my friends suggest me take up the C/C++ programming language (maybe it&rsquo;s easy to grab) and I did so. Well, I was ... program should ask again to provide an integer number and then it should complete the rest of the operation. Thanks for your co-operation, guys.
asked Apr 29, 2020 Gavin 15.3k points
0 votes
1 answer 28 views
28 views
Problem: I want to know whether the character at a certain index in a string is a number or not in C++. Moreover, I wanna see whether the user entered a number as input to a character variable or some other character. How can I do that?
asked Oct 21, 2020 Code Learner 5.8k points
0 votes
1 answer 26 views
26 views
Problem: What is the fastest way to check if a file uses standard C++/C++ 11/C? I would like to find the fastest way if the file is in standard C++11, C++ or C. I have thousands of files and I need to make sure all of them are there before doing anything on ... What can I write instead of /* SOMETHING */ in the following function? inline bool exist(const std::string& name) {      /* SOMETHING */ }
asked Aug 29, 2020 sasha 12.9k points
0 votes
1 answer 7 views
7 views asked Jan 27 Afi khan 1.4k points
0 votes
1 answer 21 views
21 views
I am new to this. I was doing that but I got into some problems.
asked Nov 30, 2020 TeamScript 13.5k points
0 votes
1 answer 11 views
0 votes
1 answer 11 views
11 views
How can I do this
asked Jan 22 TeamScript 13.5k points
1 vote
1 answer 40 views
40 views
Problem: Hello good people, I am a novice in java programming. I am learning java online and solving problems daily. Today I got a problem that says to check if input is a number. How can I check it? Is it require a scanner class? Could anybody here help me to solve this problem, please?
asked Apr 7, 2020 Gavin 15.3k points
0 votes
0 answers 4 views