The number 3, how can I ask user to input there desired number here?
I wanted to run for command next.
But for now how can I ask user for input there number in place of 3?
You mean alphabetical order? Yeah, you can sort a vector or an array by alphabetical order. You can use std::sort.
For array: std::sort(arr, arr + num_elements);
For vector: std::sort(vec.begin(), vec.end());
It is written right next to vector
??
We can assign variable to vector but not array?
Correct, you can assign vectors to another vector just by using =. With arrays, you have to write a loop to copy over each element. So in this sense, vectors are more convenient, but you can still do it the manual way with a vector as well.
Sorry for disturbing you again. But if I want to write variable names like first second student instead of student no.1 and student no.2. How can I do that.
If the user enter the value of how many students he want to enter like he wants to enter 5.
Then my program display first student, second student, etc instead of student no.1, student no.2, etc.
I mean, I just want to print alphabets instead of integers.
Okay, that makes more sense. So you want multiple students, and each student has a name, "obtained marks", and "total marks", right?
This is where structs or classes are actually quite useful, because they let you organize data into logical units. For example, you can have a "Student" object, and a Student contains: {string for name, number for obtained marks, number for total marks}.
#include <iostream>
#include <string>
#include <vector>
usingnamespace std;
struct Student {
string name;
double obtained_marks;
double total_marks;
};
int main()
{
cout << "Enter number of students: ";
int num_students;
cin >> num_students;
vector<Student> students(num_students);
for (int i = 0; i < num_students; i++)
{
cin.ignore(); // [C++ quirk, must remove newline in the buffer from cin >>]
cout << "Enter name of student: ";
string student_name;
getline(cin, student_name);
cout << "Enter data for " << student_name << ":\n";
cout << " Marks: ";
int marks;
cin >> marks;
cout << " Total Marks: ";
int total_marks;
cin >> total_marks;
students[i].name = student_name;
students[i].obtained_marks = marks;
students[i].total_marks = total_marks;
}
cout << "Printing all information:\n\n";
for (int i = 0; i < num_students; i++)
{
cout << "Student Name: " << students[i].name << '\n';
cout << students[i].obtained_marks << "/" << students[i].total_marks << " marks\n\n";
}
return 0;
}
If you haven't learned structs or classes yet, that's fine.
Just use 3x vectors: One for the student names, one for the obtained marks, and one for the total marks.
#include <iostream>
#include <string>
#include <vector>
usingnamespace std;
struct Student {
string name;
double obtained_marks;
double total_marks;
};
int main()
{
cout << "Enter number of students: ";
int num_students;
cin >> num_students;
vector<Student> students(num_students);
for (auto& s : students)
{
cin.ignore(); // [C++ quirk, must remove newline in the buffer from cin >>]
cout << "Enter name of student: ";
getline(cin, s.name);
cout << "Enter data for " << s.name << ":\n";
cout << " Marks: ";
cin >> s.obtained_marks;
cout << " Total Marks: ";
cin >> s.total_marks;
}
cout << "Printing all information:\n\n";
for (constauto& s: students)
cout << "Student Name: " << s.name << '\n' << s.obtained_marks << "/" << s.total_marks << " marks\n\n";
}
lines 6 - 10 define the struct. Student has 3 elements - name, obtained_marks and total_marks.
lines 14-16 obtain the number of students
line 18 defines vector students of type Student that has num_students elements.
line 20 is the range-base for loop that will sequentially go through all the elements of students. As s is a ref, changes to s are reflected in the vector.
lines 22-32 obtain the data and store it in the current element s of the vector.
lines 35-37 displays the contents of vector students
What don't you understand? The code is mostly just input/output, with the new concept being a struct.
Have you learned about structs yet? Like I said, they are ways to group data together. So instead of having 3x arrays (one for names, one for marks, one for total marks), you can have one array of Student objects, each having a name, marks, and total marks.
getline gets a string from the user, allowing them to type spaces as well ("John Doe"). cin >> let's you input a single value. student[i].name accesses the name of each Student in the vector, either for writing to or accesses (say, for printing).