hi,I am an amateur c programmer.I am using turbo c++ v3.1 compiler. here is a simple program to input and display name and address.. There are two problems in my program
1) telephone number is too long for int and it displays some weird value.. so i tried long int, but that also is not working.. why is that so?
2)spaces arent being displayed in character strings.. everything after the space is truncated ! isnt space a valid character? what do i do? use gets ? I dont know how to use that properly.. pls pls help me

#include<conio.h>
#include<stdio.h>
int main()
{
long int t;

char n[20],h[10],s[10],d[10],state[10],c[10];
clrscr();
printf("\t\t ENTER YOU NAME AND ADDRESS \n\n");
printf("Name : \t");
scanf("%s",&n);
clrscr();
printf("House name : \t");
scanf("%s",&h);
clrscr();
printf("Street : \t");
scanf("%s",&s);
clrscr();
printf("District : \t");
scanf("%s",&d);
clrscr();
printf("State : \t");
scanf("%s",&state);
clrscr();
printf("Country : \t");
scanf("%s",&c);
clrscr();
printf("Telephone number : \t");
scanf("%d",&t);
clrscr();
printf("\nName : \t%s",n);
printf("\nHouse name : \t%s",h);
printf("\nStreet : \t%s",s);
printf("\nDistrict : \t%s",d);
printf("\nState : \t%s",state);
printf("\nCountry : \t%s",c);
printf("\nTelephone Number : \t %d",t);
getch();
return 0;
}

long int has a range of −2,147,483,648 to 2,147,483,647 an American tele-numeral is ten digits long - without any prefixes or other telephone system specific numbers and symbols! So an integer is not a suitable data-type for storing a telephone number. Try a string.

On a side note. You should eliminate conio.h, as it is not a standard C header. clrscr() is unnecessary and can be annoying to the user. Replace getch() with getchar().

Oh yeah, and Turbo C++ 3.1 is a very out-of date, non-standard compiler. If you can, treat yourself to Code::Blocks(MinGW) or VC++.

use of a string did solve the telephone number problem and use of gets resolved the space issue.. but I still dont know what is wrong with the use of clrscr() .. How do I clear the screen before the other question is asked? is there any other way so that repeated use of clrscr() can be avoided?

#include <stdlib.h>
// on windows clear screen with
system("cls");
// on unix/linux clear screen
system("clear");

use of a string did solve the telephone number problem and use of gets resolved the space issue.. but I still dont know what is wrong with the use of clrscr() .. How do I clear the screen before the other question is asked? is there any other way so that repeated use of clrscr() can be avoided?

Sure.

Although Turbo C will include gotoxy() for you, it's still good to have this, at the top of your program:

#include <conio.h>

Now it's easy to put a blank area, anywhere you want:

gotoxy(RowYouWant, ColumnYouWant);

which puts the cursor where you want it to be, then:

printf("                                                                    ");

print a bunch of empty spaces, where you need them.

Anyone trying to run your code without conio.h built into their compiler, will need to change that line to use the Windows API for setting the console cursor position, or use the curses/ncurses library.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.