0

I used the below code to store an phone number(10 digit number) in c++:

#include<iostream.h>
void main(){
   long long num;
   cin>>num;
   cout<<num;
} 

Input:998578985
output:1395855233

Why is this happening? Is there any other way to store a 10 digit number. I am using turboc++ in win7. enter image description here

Pramod
  • 1,411
  • 11
  • 35
  • 66
  • 12
    Don't try to store a phone number in an integer data type. A phone number is not an integer, it's a string. (Although this is not the cause of the above issue.) – Oliver Charlesworth Mar 18 '12 at 13:12
  • 1
    A few hints on code quality: `#include `, the `.h` suffix is not allowed anymore, qualify `cin` and `cout` with `std::`, use a newer compiler. – pmr Mar 18 '12 at 13:14
  • 1
    Change your compiler. This program is not standards compliant (for example, main should return int). Under GCC, this value is handled correctly. – Rafał Rawicki Mar 18 '12 at 13:15
  • @Oli Charlesworth When i tried using string. I am getting an error "Undefined symbol string" even i included #include. Don't know y. Can u help me – Pramod Mar 18 '12 at 13:18
  • 1
    @Pramod because you should include , not – Luchian Grigore Mar 18 '12 at 13:20
  • `#include `, not ``, and fully qualify type `std::string`. – hmjd Mar 18 '12 at 13:21
  • If i am not include i am getting an error unable to open include file 'string' – Pramod Mar 18 '12 at 13:32
  • 3
    No. First step, get a modern compiler and editor. How can anyone work like this? – Konrad Rudolph Mar 18 '12 at 13:35
  • 1
    @Pramod: I guess it's a bit off topic, but why on Earth are you using a compiler from the 1990s? (This is partly relevant though, since your compiler can't find the string headers) – gspr Mar 18 '12 at 13:36
  • I am unaware of any other c++ compilers. I can't find any plugin for netbeans although it is the other one i know. I just started learning c++ 2days back – Pramod Mar 18 '12 at 13:41
  • 7
    @Pramod: You won't find many people here who can debug problems caused by using a 20-year old compiler. The code works as expected with a modern compiler: http://ideone.com/zmhP3. If you want a free modern compiler for Windows, then Visual Studio Express is probably as good a choice as any. – Mike Seymour Mar 18 '12 at 13:41
  • @Pramod: [The GNU Compiler Collection (GCC)](http://gcc.gnu.org/) comes with a very popular, modern, widely used C++ compiler. It's also free (and free of charge). Since you use Windows, you might want to have a look at [MinGW](http://mingw.org/) which will give you, among other things, GCC. – gspr Mar 18 '12 at 13:44

2 Answers2

5

I would recommend downloading Visual C++ 2010 Express which is free and a huge improvement over TurboC++. Most of the issues you're having is due to it being old and no where near standard compliant. For example, it doesn't have using (which means you have to qualify things like std::cin) and doesn't support C++ header files without the .h.

As for your original question: I would just store phone numbers in a string and only try to parse or validate them if I really had to. See this question for an example of parsing using regexes.

Community
  • 1
  • 1
uesp
  • 6,194
  • 20
  • 15
-1
  1. simple you should use long data type it will allow you to access or accept 10 digit numbers hope this will help you.
  2. otherwise make it string and allocate size how much digits you want to accept