0

I have written small C++ console application and this is source code :

#include<stdio.h>
#include<locale.h>
#include<ctype.h>
#include<stdlib.h>

void main()
{
    setlocale(LC_ALL, "turkish");
    int a,b,c,d;

    printf("first number: ");
    scanf("%d", &a);

    printf("second number: ");
    scanf("%d", &b);

    c = a+b;

    printf("Sum: : %d\n", c);
}

As you can see i'm requesting two numbers from user and than summing them. But i want to add a control which check number who enterede by user is integer?

I'll check number which typed by user and than if number isn't really a integer i will echo an error. I'm using this after every scanf but it's not working very well.

if(!isdigit(a))
{
            printf("Invalid Char !");
            exit(1);
}

In shortly, on a scanf action, if user type "a" it will produce an error message and program stop working. If user type a number program will continue

6
  • 1
    I retagged this as C since I don't see anything C++ about it and it'll help you get better answers. Commented Oct 11, 2011 at 21:13
  • I'm learning c++ since 10 minutes ago, haha ! Our professor wrote it like this. And i copied it. Commented Oct 11, 2011 at 21:14
  • 2
    @Eray It's not void main, it never was, and never will be. It's int main, despite the fact that return statements are optional in main (but that's the only non-void function where they're optional). Commented Oct 11, 2011 at 21:15
  • 6
    Unfortunately your professor is teaching you C++ from the last century. Commented Oct 11, 2011 at 21:16
  • "turkish" seems unlikely to be the right name for the locale to me, and you don't normally need to use setlocale anyway. Commented Oct 11, 2011 at 22:13

3 Answers 3

7

scanf does that validation for you. Just check the return value from scanf.

printf("first number: ");
if(scanf("%d", &a) != 1) {
  printf("Bad input\n");
  return 1;
}

printf("second number: ");
if(scanf("%d", &b) != 1) {
  printf("Bad input\n");
  return 1;
}
Sign up to request clarification or add additional context in comments.

2 Comments

I don't know what you mean "I can't customize error." You can certainly print whatever error message you want when scanf fails, can't you?
yes i mean error messages sorry for typo . Now your answer is very well, and helped me really. Thank you !
2

The C++ way to do this would be

#include <iostream>
#include <locale>

int main()
{
    std::locale::global(std::locale("nl_NL.utf8")); // tr_TR doesn't exist on my system

    std::cout << "first number: ";

    int a;
    if (!(std::cin >> a))
    {
        std::cerr << "whoops" << std::endl;
        return 255;
    }

    std::cout << "second number: ";

    int b;
    if (!(std::cin >> b))
    {
        std::cerr << "whoops" << std::endl;
        return 255;
    }

    int c = a+b;

    std::cout << "Sum: " <<  c << std::endl;

    return 0;
}

4 Comments

You must be kidding :S I'm hearing these first time (`std::locale::global() , cin , cout etc...)
scanf, printf, setlocale are all solidly in the realm of ANSI C :)
i don't know what is it :) I'm working on web development (PHP) for 4 years but i have never worked with C, C++
How much are you paying for this course? It was falsely advertised, as far as we can tell... C is a fine language for some purposes and C++ as well, and there is a limited ability to use them together, but it is a bad idea to abuse that. They are really different languages, and if you are writing new code, you should be clear which one you want to use. Your professor doesn't seem to understand this very well.
1

isdigit takes a char as an argument.

If the call to scanf succeeds, you're guaranteed that you have an integer.

scanf also has a return value which indicates how many values it has read.

You want to check if the return value of scanf is 1 in this case.

See: http://www.cplusplus.com/reference/clibrary/cstdio/scanf/

2 Comments

But for example i'm typing "A" for first number and it's skipping requisting 2nd number and than writing directly, SUM : -1717986920
That's because the first scanf tries to read the "A" and fails, returning 0. Since there's still input in the buffer, the next scanf immediately tries doing the same thing, fails, and returns 0. Try out Rob's answer - I think you'll understand what's going on when you do.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.