error in the output

Mar 22, 2015 at 11:33am
can anyone help with this? the error is in the output . the output:

http://s300.photobucket.com/user/Dimass_Kurniawan/media/Untitled_zpsuwlzkz0w.png.html

here's the code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include<iostream>
#include<fstream>
#include<string.h>
#include<conio.h>


using namespace std;


struct biodata{		
	char nama[50];
	char alamat[50];
	char gender[15]; 
	char telp[15];
	char tanggal[15];	
};

main(){
	struct biodata bio[10]; 
	char x;
	char nama[50];
	char alamat[50];
	char gender[15]; 
	char telp[15];
	char tanggal[15];
	int i=0,z;
	
	ofstream output;
	output.open("E:/suek.txt");
		if(!output){
		cout<<"File tidak dapat dibuka"<<endl;
		return 1;
	}
	
	gak:
	
	cout<<"Nama: ";
	cin.getline(nama,sizeof nama);
	strcpy(bio[i].nama,nama);
	cout<<"Alamat: ";
	cin.getline(alamat,sizeof alamat);
	strcpy(bio[i].alamat,alamat);
	cout<<"gender ";
	cin.getline(gender,sizeof gender);
	strcpy(bio[i].gender,gender);
	cout<<"Nomor HP: ";
	cin.getline(telp,sizeof telp);
	strcpy(bio[i].telp,telp);
	cout<<"TTL: ";
	cin.getline(tanggal,sizeof tanggal);
	strcpy(bio[i].tanggal,tanggal);
	i++;
	
	ulang:
	cout<<"apakah ingin mengisi lagi? ya/tidak: ";
	cin>>x;
	if (x=='y'||x=='Y'){
		
		goto gak;
	
	}
	else if (x=='n'||x=='N'){
			for(z=0;z<=i;z++){
			
			output<<bio[z].nama<<endl;
			output<<bio[z].alamat<<endl;
			output<<bio[z].gender<<endl;
			output<<bio[z].telp<<endl;
			output<<bio[z].tanggal<<endl;
			output<<endl;
			}
		
			output.close();
			cout<<"File anda tersimpan pada: ";
			return 0;
		
		}
			else{
			cout<<"isi dengan y/n : ";
			goto ulang;
		}
		getche();
		return 0;
		}
		


thanks before and sorry :(
Mar 22, 2015 at 11:36am
I dont see whats wrong. Its hard to understand when its in another language. But from what I understand from looking at the picture, you're program is working exactly as its supposed to.
Mar 22, 2015 at 3:35pm
closed account (D80DSL3A)
It is a common problem encountered when mixing cin with getline methods. cin leaves a '\n' in the input stream which the following getline interprets as a line.
One solution is to flush out the '\n' after calling cin. Try this to replace lines 57-61 in your code:
1
2
3
4
5
if (x=='y'||x=='Y'){	
	cin.ignore(256,'\n');// flush that '\n' !
	goto gak;
	
}

EDIT: Another solution is to not mix usage of these input methods.
Last edited on Mar 22, 2015 at 3:39pm
Mar 22, 2015 at 4:01pm
Why do you declare the variables once in the structure and again once in the main function ? Also, It looks like you're using a very old form of C++ (Borland c++). Why don't you switch to a newer IDE ?
Last edited on Mar 22, 2015 at 4:03pm
Mar 22, 2015 at 11:09pm
thanks for fun2code for your help now the problem is solve ^^
Topic archived. No new replies allowed.