0

If we take the input from user by asking them, like below:

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

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

char courseCode[10];

And how can I assign the name part in the array, let's 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];
mkrieger1
  • 19,194
  • 5
  • 54
  • 65
Khubaib Khawar
  • 161
  • 1
  • 1
  • 9

3 Answers3

3

It's actually kind of simple once you remember that the input operator >> stops on white-space, and also know about the std::getline function.

Then you can do something like

std::string courseCode;
std::string courseName;

std::cin >> courseCode;
std::getline(std::cin, courseName);

Note that I use std::string for the strings instead of arrays. This is what you really should use. If you're not allowed (by your teacher or something) and must use arrays, then you can't use std::getline but instead have to use std::istream::getline instead.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • "_If you're not allowed (by your teacher or something) then send him to the cpp lounge and we'll deal with it_" FIFY. – Hatted Rooster Jan 12 '17 at 14:49
  • @Some programmer dude I can use strings and allowed to, but to make myself more strong in arrays, I want to use arrays here. Secondly, the code you wrote: `std::getline(std::cin, courseName);` How would it assign the course name (taken from user with teh course code as input) to courseName string? – Khubaib Khawar Jan 12 '17 at 14:58
  • @KhubaibKhawar The `std::getline` function will read the rest of the line and store it in the string provided as the second argument. – Some programmer dude Jan 12 '17 at 15:00
  • @Someprogrammerdude thats fine now. Well, I am not understanding that how would it split the code and assign it to courseCode string? because there is not anything finding any whitespace in the code you explained. – Khubaib Khawar Jan 12 '17 at 15:03
  • @KhubaibKhawar Like I said in my answer, the input operator `>>` will handle it for you. – Some programmer dude Jan 12 '17 at 15:06
0

Store the input in a single string say x

Now on x perform linear search for the first whitespace and split the string about the first whitespace. Store the two resultant strings in your struct.

Krash
  • 2,085
  • 3
  • 13
  • 36
  • Lets say, I used the linear search and stored the first input in courseCode array, after finding the white space, now how do I assign the left string which is the course name in the courseName array? – Khubaib Khawar Jan 12 '17 at 15:01
0

I solved my problem using the cin.getline() functions to get the string in token pointer and then used strchr(char [], cahr) of <string> header file to separate the current string from the place where the first white space comes. Then I copied both separated strings into my desired elements of my structure using strcpy() function.

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
Khubaib Khawar
  • 161
  • 1
  • 1
  • 9