This is my code in writing a file. The problem I have is when I input a data in "Enter Name: ", lets say I input Sarah Jessica it writes weird output in my file. This is th weird output:
Name: Sarah
age:-858993460
address:Jessica
My question is:
1. Why is it that my program can't accept input with space?
eg. Sarah Jessica<-it ignores string sarah
2. Whats that wierd -858993460
3. Why is it that string sarah jumps to Address?
I need some experts thoughts. Thanks a lot in advance![]()
Code:#include"include.h" main() { ofstream out_stream; char out_file_name[20]; char name[20]; char adr[20]; int age; printf("\nTHIS IS A MINI DATABASE PROGRAM"); printf("\n\nEnter Name:"); scanf("%s",&name); printf("You entered %s as name", name); printf("\nEnter age:"); scanf("%i",&age); printf("You entered %i as age", age); printf("\nEnter address:"); scanf("%s", &adr); printf("\nYou entered %s as address:", adr); out_stream.open(out_file_name); if(out_stream.fail()) { printf("Output File opening failed.\n"); exit(1); } out_stream<<"Name:"<<name<<endl<<"age:" <<age<<endl <<"address:"<<adr<<endl; out_stream.close(); return 0; }



LinkBack URL
About LinkBacks



