How to divide the input taken from user in two parts and assign them to two different arrays in C++?

Hi Guys, basically if we take the input from user by asking him, like below:

cout << "Enter your course code and course name: ";

Now if user enters this "CS201 Introduction to Programming", now how can I only assign the code part i.e. 'CS201' to an array, lets say;

char courseCode[10];

And how can I assign the name part in the array, lets say:

char courseName[50];

I want to do this to 5 students, using the structure, defined below:

struct student
{
char courseName[50];
char courseCode[10];
};

student stu[5];

Kindly help me, I can't figure out how can I divide the input in two parts and assign them in two different arrays which are present in the structure I defined above. Thanks in advanced.
I suppose asking the user to enter the 2 pieces of information separately into 2 variables is out of the question?
I suppose asking the user to enter the 2 pieces of information separately into 2 variables is out of the question?

In that case use the following to split user input into course code and course name:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <string>
#include <sstream>

int main()
{
    std::cout << "Enter your course code and course name: \n";
    std::string input;
    getline(std::cin, input);
    std::istringstream stream(input);
    std::string course_code;
    stream >> course_code;
    std::string course_name{};
    std::string dummy;
    while (stream >> dummy)
    {
        course_name += dummy + " ";
    }
    std::cout << "Course code: " << course_code <<'\n';
    std::cout << "Course name: " << course_name << '\n';
}
gunnerfunner yes it is out of the question, actually it is my Assignment, so thats why user must have to enter information in one piece only
I assume this is one of this horrible C++ courses where you have to use c char arrays instead of strings. Here is an example - it assumes that course code doesn't contain a space:

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
#include <iostream>
#include <string>
#include <cstring>
#include <cstdlib>

using namespace std;

struct student
{
  char courseName[50];
  char courseCode[10];
};

int main()
{
  student s = {0};
  char input[62] = {0}; // length courseMame + length courseCode + separator + terminating space
  cout << "Enter course code and name: ";
  cin.getline(input, 62, '\n');
  char *cp = strchr(input, ' '); // find space after course code
  if (cp) // found
  {
    *cp = '\0'; // input ends at space
    strcpy(s.courseCode,input);
    cp++; // move to start of course name
    strcpy(s.courseName, cp);
    cout << "Course code " << s.courseCode;
    cout << "\nCourse name " << s.courseName;
  }
  else // not found
  {
    cerr << "invalid input";
  }
  system("pause");
  return EXIT_SUCCESS;
}


OUTPUT:
1
2
3
Enter course code and name: cs2010 Programming in C++
Course code cs2010
Course name Programming in C++
I assume this is one of this horrible C++ courses where you have to use c char arrays instead of strings. Here is an example - it assumes that course code doesn't contain a space:


Thank you Thomas, this is the idea I needed, below is how I have written my code for my Assignment, Although I just finished this part in which I had to take input from user of at least 5 students at a time, single string containing course name and code and then I had to separately assign the course name and code tostructure's elements respectively. Thanks again. Below is the code which I have done so far yet, the remaining is easy so going to do that soon:

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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// Declaring the C++ header file to use Input and Output Functions in the program (e.g. cin, cout)
#include <iostream>
// Declaring the C++ header file to use String Functions in the program (e.g. string keyword, strcpy)
#include <string>
// Declaring the C header file to use the C Language's String Functions in the program (e.g. strchr)
#include <cstring>

// This statement is used to use the standard names for some functions which are updated in C++ and old names are not used by most compilers
using namespace std;


/*
STEP 4.	Use structure name "course" in your program.
*/

// Declaring the structure for students' information
struct course;
	
// Defining the structure
struct course
{
	// Declaring the Structure's Elements that contains course name, course code, semester and degree's current year
	char courseName[50];
	char courseCode[10];
	int srNo[5];
	char sem[50];
	char currentYear[10];
};


/*
 STEP 5: Use separate functions for taking user inputs, displaying and formatting outputs. 
		 e.g. InputCourses(),DisplayHeader(),DisplayCourses(),FormatCourse(---) etc.
*/

// Declaring the function to get input of courses
char getInputCourses(char arr[62], char arr2[62], course s[5]);

// Declaring the function to display header at output screen
char dispTopHeader();

// Declaring the function to display formatted courses
char dispFormattedCourses();

// Defining the function to get input of courses
char getInputCourses(char arr[62], char arr2[62], course s[5])
{
	// Declaring tokens for to determine the first white space in the string of course information and semester information
	char *coursePtr; // Token for separating Course name and Course Code from the first string taken from user
	char *semPtr; // Token for separating Semester and Degree's Current Year
	
	// Declaring and Defining a variable 'maxStuds' to use it in the loop to make the data limited to 5 students only
	int maxStuds = 5;
	
	// This for loops runs and make the limit of students entering their data to 5 only
	for (int i = 0; i <= maxStuds; i++)
	{
		// Asks user to enter the course code along with name
		cout << "Please enter the code and name of your course\n" << "(e.g. CS201 Introduction to Programming): ";
		// Takes the user input of course code and assign it to the array1
		cin.getline(arr, 62, '\n');
		// This pointer token starts reading the input from array1 given by user and stops at the place where the first 'white space' comes
		coursePtr = strchr(arr, ' ');
		// This if statement runs when the above token has finally reached at 'white space'
		if (coursePtr)
		{
			// After token reached at white space in array1, this statement puts the null character there to separate the string and save the read string and leave the left
			*coursePtr = '\0';
			// This function copes the string currently present in the above pointer token to the 'courseCode' element of the structure we defined at start
			strcpy(s[i].courseCode, arr);
			// This statement makes increment in token that makes it leave the string which was read already and now reads the next part of string which was left before
			coursePtr++;
			// This function copies the new string from the token to 'courseName' element of the structure we defined at start
			strcpy(s[i].courseName, coursePtr);
		// If statement ends
		}
		
		// Asks user to enter the semester and the current degree year
		cout << "Please enter your semester\n" << "(e.g. Fall 2016): ";
		// Takes the user input of course code and assign it to the array2
		cin.getline(arr2, 62, '\n');
		// This function copies the whole string from array2 to 'sem' element of the structure we defined at start
		strcpy(s[i].sem, arr2);
		// This pointer token starts reading the input from array2 given by user and stops at the place where the first 'white space' comes
		semPtr = strchr(arr2, ' ');
		// This if statement runs when the above token has finally reached at 'white space'
		if (semPtr)
		{
			// After token reached at white space in array2, this statement puts the null character there to separate the string and save the read string and leave the left
			*semPtr = '\0';
			// This statement makes increment in token that makes it leave the string which was read already and now reads the next part of string which was left before
			semPtr++;
			// This function copies the new string from the token to 'currentYear' element of the structure we defined at start
			strcpy(s[i].currentYear, semPtr);
		// If statement ends
		}
	// For loop ends
	}
// Definition of function 'getInputCourses' ends
}

// The main function starts here
int main()
{
	char courseInfo[62]; // Array size calculated by adding the size of courseName(i.e. 50), courseCode(i.e. 10), space(i.e. 1) and the null character(i.e. 1)
	char semStartingDetail[62]; // Array size calculated by adding the size of courseName(i.e. 50), courseCode(i.e. 10), space(i.e. 1) and the null character(i.e. 1)
		
	course stud[5];
	
	getInputCourses(courseInfo, semStartingDetail, stud);

}
Topic archived. No new replies allowed.