Filestreaming error?

I have this little problem that whenever I add a student, the first letter of the char is always missing at when viewd at the text file. For example : "Jhonny Brown" it would be copied to the .txt file but the first letters always come out missing. In the text file, it would appear as "honny Brown". Same goes with the other data. Like for the next question which is the ID number. I will enter "2012-390", but "012-390" are the numbers that are saved in the text file.

I belive there is something wrong with this:
1
2
std::getline(cin, temp1);cin.ignore();
		cout<<"Enter the ID number: ";



But I don't know what to do next. Please enlighten me. By the way, below is the main().
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
86
87
88
89
90
91
92
int main()
{
	Students b1;
	Books b2;
	char ch;
	string temp1, temp2, temp3, temp4;
	while(1)
	{system("cls");
	cout<<endl;
	cout<<"Welcome"<<endl;
	cout<<"----------------"<<endl;
	cout<<"a.) Add a Book "<<endl;
	cout<<"b.) Delete a Book "<<endl;
	cout<<"c.) Display the Books "<<endl;
	cout<<"d.) Add a student "<<endl;
	cout<<"e.) Delete a student "<<endl;
	cout<<"f.) Display the students list "<<endl;
	cout<<"g.) Exit "<<endl;
	cout<<"  ENTER YOUR CHOICE  ";
	cin>>ch;
	if (ch=='a'){
		cout<<"Enter the Title of the book ";cout<<" ";
		std::getline(cin, temp1);std::cin.ignore();
		cout<<"Enter the author of the book ";cout<<" ";
		std::getline(cin, temp2);std::cin.ignore();
		cout<<"Enter the ISBN of the book ";cout<<" ";
		std::getline(cin, temp3);std::cin.ignore();
		cout<<"Enter the quantity of the book ";cout<<" ";
		std::getline(cin, temp4);std::cin.ignore();
		b2.addbooks(temp1,temp2,temp3,temp4);
		cout<<"The book was succesfully added!\n\n";

		ofstream book("book.txt", ios::app);
			if (!book) {
        cerr << "File could not be opened." << endl;
        exit(1);
			}book << temp3<<' '<<temp1<<' '<<' '<<temp2<<' '<<temp4<<  '\n';
		system("pause");
		main();
	
	}
		
	else if (ch=='b'){cout<<"Enter the title of the book to be deleted: ";
		std::getline(cin, temp1);cin.ignore();
		cout<<"Enter the author of the book: ";
		std::getline(cin, temp2);cin.ignore();
		cout<<"Enter the ISBN of the book: ";
		std::getline(cin, temp3);cin.ignore();
		cout<<"Enter the quantity of the book: ";
		std::getline(cin, temp4);cin.ignore();
		b2.removebooks(temp1,temp2,temp3,temp4);
	}
	else if (ch=='c'){cout<<"The list contains : ";
	cout<<b2;}
		
	else if (ch=='d'){ cout<<"Enter the name of the student: ";
		std::getline(cin, temp1);cin.ignore();
		cout<<"Enter the ID number: ";
		std::getline(cin, temp2);cin.ignore();
		cout<<"Enter the year of the student: ";
		std::getline(cin, temp3);cin.ignore();
		cout<<"Enter the no. of books borrowed: ";
		std::getline(cin, temp4);cin.ignore();
		b1.addstudent(temp1,temp2,temp3,temp4);
		cout<<"Student was succesfully added!\n\n";
		system("pause");}
	
	else if (ch== 'e'){cout<<"Enter the name of the student to be deleted: ";
		std::getline(cin, temp1);cin.ignore();
		cout<<"Enter the ID number: ";
		std::getline(cin, temp2);cin.ignore();
		cout<<"Enter the year of the student: ";
		std::getline(cin, temp3);cin.ignore();
		cout<<"Enter the borrowed books: ";
		std::getline(cin, temp4);cin.ignore();
		b1.removestudent(temp1,temp2,temp3,temp4);
	}


	else if (ch=='f'){ cout<<"The list of the student contains: ";
		cout<<b1;
	}

	else if (ch =='g'){
		return 0;
	}

	else system("cls");
		
	
	}
}

the point of the
 
cin.ignore();

is to avoid skipping of questions. Every time the cin.ignore function is removed, the question would always skip. It would ask the name but wouldn't let the user enter a name because it skipped to the ID number, which just made things worse. I guess it is something that controls another thing. What should I do to avoid skipping of questions but still retain all the complete entered information in the text file?

Thank you for reading. Hoping for answers :D
Kimmydoo wrote:
I belive there is something wrong with this:
1
2
std::getline(cin, temp1);cin.ignore();
		cout<<"Enter the ID number: ";


[...]

the point of the
 
cin.ignore();

is to avoid skipping of questions. Every time the cin.ignore function is removed, the question would always skip. It would ask the name but wouldn't let the user enter a name because it skipped to the ID number, which just made things worse. I guess it is something that controls another thing. What should I do to avoid skipping of questions but still retain all the complete entered information in the text file?
On line 20, you read a single character, leaving a newline in the stream. This completely throws off everything afterward. If you fix this issue, you will not need the cin.ignore(); calls after getline(), as getline() will automatically take out the newline from the stream.
Last edited on
actually I'm making a linked list that can copy the entered characters in the node, and also in the stream.
This is what comes before that 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
/*----------------------------------------*/
/*--------------ADD STUDENT---------------*/
/*----------------------------------------*/
void Students::addstudent(const string& q, string& w, string& e, string& r){
	if (empty())
	{
	nodeS* temp = new nodeS;
	head = temp;
	last = temp;
	temp->prev=NULL;
	temp->next=NULL;
	temp->Sname =q;
	temp->idno =w;
	temp->year =e;
	temp->bb =r;
	}
	else 
	{
	nodeS* curr;
	curr=head;
	while((q>curr->Sname&&curr->next!=last->next)&&(w>curr->idno&&curr->next!=last->next)&&(e>curr->year&&curr->next!=last->next)&&(r>curr->bb&&curr->next!=last->next))curr=curr->next;

	if(curr==head){
	nodeS*temp = new nodeS;
	temp->Sname =q;
	temp->idno =w;
	temp->year =e;
	temp->bb =r;
	temp->prev = curr;
	temp->next= NULL;
	head->next= temp;
	last = temp;
	}
	else 
	{
		if ((curr==last&&q>last->Sname)&&(curr==last&&w>last->idno)&&(curr==last&&e>last->year)&&(curr==last&&r>last->bb))
		{
		last->next= new nodeS;
		(last->next)->prev=last;
		last=last->next;
		last->next=NULL;
		last->Sname=q;
		last->idno=w;
		last->year=e;
		last->bb=r;
		}
		else 
		{
		nodeS* temp=new nodeS;
		temp->Sname =q;
		temp->idno =w;
		temp->year =e;
		temp->bb =r;
		temp->next = curr;
		(curr->prev)->next=temp;
		temp->prev=curr->prev;
		curr->prev=temp;

		}
	}
	}
}


and on line 20, I think it just gets a character so it will know what to do next, if it is to add a student, add a book, remove a student and so on.
Topic archived. No new replies allowed.