There's an unwanted changed line between two variables

Between two string type of variables,
course_number[], title[]

I want to print them in a same line but the line is changed somehow.

What I want:
course code name hour per week session(const)
course_number[0] title[0] 5 fall 2016
course_number[1] title[1] 5 fall 2016
course_number[2] title[2] 5 fall 2016
course_number[3] title[3] 5 fall 2016
course_number[4] title[4] 5 fall 2016
course_number[5] title[5] 5 fall 2016

What I get:
course code name hour per week session(const)
course_number[0]
title[0] 5 fall 2016
course_number[1]
title[1] 5 fall 2016
course_number[2]
title[2] 5 fall 2016
course_number[3]
title[3] 5 fall 2016
course_number[4]
title[4] 5 fall 2016
course_number[5]
title[5] 5 fall 2016

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
 
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std; 

struct courses
{
	string course_number;
	string title;
	int hours_per_week;
};

int main()
{
const string session = "Fall 2016";
      courses sub[6];

	    cout << "Please enter the code of the subject.          :" ;
		getline(cin, sub[0].course_number,'*');
		cout << "Please enter the name of the subject.			:" ;
		getline(cin, sub[0].title,'*');
		cout << "Please enter how many hours it has per a week. :" ;
		cin >> sub[0].hours_per_week;
	
	for (int i=1; i<6; i++)
	{
		cout << "\n* * * * Next Subject * * * *\n" <<endl;

		cout << "Please enter the code of the subject.			:" ;
		getline(cin, sub[i].course_number,'*');
		cout << "Please enter the name of the subject.			:" ;
		getline(cin, sub[i].title,'*');
		cout << "Please enter how many hours it has per a week. :" ;
		cin >> sub[i].hours_per_week;
	}
	
	cout << "Please enter any keys to see the result. " <<endl;
	system ("pause");
	system ("cls");
	
	cout << "Course Number\t"
		 << "Title\t\t"
		 << "Hours per week "
		 << "Session"
		 <<	endl;
	
	for (int i=0; i<6; i++)
	{
		cout <<sub[i].course_number<<sub[i].title <<sub[i].hours_per_week<<session <<endl;
	}

					
	
	system ("pause");
	return 0;
}
Last edited on
What does the input look like?
The course code will be like 123-123-123, and the course name is 'mathmatics', 'college English' etc,.

I think I found the answer.
I noticed that the second input still has the '\n' from the last input.
So I removed the buffer using
1
2
cin.clear();
cin.ignore(1,'\n');


between two getlines.

And it is now working :D
Topic archived. No new replies allowed.