int Array[]

I tried a lot of things but failed.

int student[3];


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?

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
#include <iostream>
using namespace std;
int main()
{
        // o1 is obtained marks, t1 is total marks, per1 is percentage
        // 1 and 2 with "o, t, per" shows student number 1 and 2
	float o1, t1, per1;

	int student[3];

	cout << "Enter details of student No 1:" << endl;
	cout << "Obtained marks = ";
	cin >> o1;
	cout << "Total marks = ";
	cin >> t1;
	per1 = (o1 / t1) * 100;
	cout << "Percentage = " << per1 << "%";
	
	cout << endl;

	if (per1 >= 80)
		{cout << "Grade is: A+ " << "(Exceptional)";}
	if (per1 >= 70 && per1 < 80)
	    {cout << "Grade is: A " << "(Excellent)";}
	if (per1 >= 60 && per1 < 70)
	    {cout << "Grade is: B " << "(Very Good)";}
	if (per1 >= 50 && per1 < 60)
	    {cout << "Grade is: C " << "(Good)";}
	if (per1 >= 40 && per1 < 50)
	    {cout << "Grade is: D " << "(Fair)";}
	if (per1 > 33 && per1 < 40)
	    {cout << "Grade is: E " << "(Satisfactory)";}
	if (per1 <= 33)
	    {cout << "Grade is: F " << "(Fail)";}



/*	cout << endl << endl;

	float o2, t2, per2;
	cout << "Enter details of second student:" << endl;
	cout << "Obtained marks = ";
	cin >> o2;
	cout << "Total marks = ";
	cin >> t2;
	per2 = (o2 / t2) * 100;
	cout << "Percentage is = " << per2 << "%";

	cout << endl;

	if (per2 >= 80)
		{cout << "Grade is: A+ " << "(Exceptional)";}
	if (per2 >= 70 && per2 < 80)
	    {cout << "Grade is: A " << "(Excellent)";}
	if (per2 >= 60 && per2 < 70)
	    {cout << "Grade is: B " << "(Very Good)";}
	if (per2 >= 50 && per2 < 60)
	    {cout << "Grade is: C " << "(Good)";}
	if (per2 >= 40 && per2 < 50)
	    {cout << "Grade is: D " << "(Fair)";}
	if (per2 > 33 && per2 < 40)
	    {cout << "Grade is: E " << "(Satisfactory)";}
	if (per2 <= 33)
	    {cout << "Grade is: F " << "(Fail)";}
	*/

	// System commands
	cout << endl << endl << endl;
	system("pause");
}
Last edited on
The size of an array must be a compile-time constant. Use a vector, instead.

1
2
3
4
5
6
7
#include <vector>

// ...

int num_students;
cin >> num_students;
vector<int> students(num_students);


students is now a vector, which is very similar to an array.
You can access and use it in the same exact way as an array, e.g.

cout << students[0] << '\n';

1
2
for (int i = 0; i < num_students; i++)
    students[i] = 42;

etc.
Last edited on
Thanks!

Last question, array cannot use alphabets? Like vectors?
I don't know what you mean by "alphabets". Please show an example if something is not working.
I mean like student is written in alphabets form. Right?
It is written right next to vector.
We can assign variable to vector but not array?
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.
Last edited on
With arrays, you have to write a loop to copy over each element.

There is an iterator constructor for std::vector that allows a vector constructed from an array (C++11 or later):
1
2
3
4
5
   int myints[] { 16,2,77,29 };
   
   std::vector<int> my_vec(myints, myints + sizeof(myints) / sizeof(int));
   
   std::vector<int> myvec(std::begin(myints), std::end(myints));
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}.

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
#include <iostream>
#include <string>
#include <vector>
using namespace 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.
Last edited on
Thanks, that works. But can you explain me how it works line by line? Like how this code works that you wrote?
I will be grateful to you.
Last edited on
This can be simplified:

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
#include <iostream>
#include <string>
#include <vector>
using namespace 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 (const auto& 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
Last edited on
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).
@Ganado ... No, I haven't learnt about structs yet. That is why, I can not understand it.
Last edited on
Thanks, @seeplus
I haven't learnt about structs yet.

If you want to learn about structs on your own:
https://www.learncpp.com/cpp-tutorial/47-structs/

Lots of C++ material at that online tutorial, Learn C++.
Thanks a lot, everyone.
Last edited on
Topic archived. No new replies allowed.