dynamic allocation help. char and int

hello i need an output screen like this:


Enter the number of students: 2
Enter the number of exams: 2
1. student's name: michael
1. student's school number: 5000
1. student's 1. exam grade: 50
1. student's 2. exam grade: 40
The avarege of this student is: 45
2. student's name: mary
2. student's school number: 6000
2. student's 1. exam grade: 70
2. student's 2. exam grade: 80
The avarege of this student is: 75


Note: bold ones are keyboard input.

The problem is i have to do that with 'new' and 'delete' operators. Here is some codes that i have tried:

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
void getdata()
{
    int i,j;
    i=0;j=0;
    int number;
    char *ArrStdName;
    int *ArrStdSchlNum;
    int examNumber;
    int *ArrExam;
    cout<<"Enter the number of students: "<<endl;
    cin>>number;
    ArrStdName = new char[number];
    ArrStdSchlNum = new (nothrow) int [number];
    cout<<"Enter the number of exams: "<<endl;
    cin>>examNumber;
    ArrExam=new (nothrow) int [examNumber];    
    for (i=0;i<sayi;i++)
    {
        cout<<i+1<<". student's name: "<<endl;
        cin>>ArrStdName[i];
        cout<<i+1<<". student's school number: "<<endl;
        cin>>ArrStdSchlNum[i];
        for (j=0;j<tsayi;j++)
        {
            cout<<i+1<<". student's "<<j+1<<". exam grade: "<<endl;
            cin>>ArrExam[j];

        }
        calculateavarege(); //this is not important right now
    }
    
}


When I watch the variables in debug mode, all of them seem silly:\

Any one to help? thanks....
anyone? :\
I think my code is very different...
I dunno, does this help?

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
// 10/11/2011
#include <iostream>
#include <string>
using namespace std;

int main()
{
	int numberOFstudents;
	cout << "Enter the number of students: ";
	cin >> numberOFstudents;
	cin.ignore(99,'\n');

	int numberOFexams;
	cout << "Enter the number of exams: ";
	cin >> numberOFexams;
	cin.ignore(99,'\n');

	string student1name;
	cout << "1. student's name: ";
	cin >> student1name;
	cin.ignore(99,'\n');

	int student1SchoolNumber;
	cout << "1. student's school number: ";
	cin >> student1SchoolNumber;
	cin.ignore(99,'\n');

	int student1Examgrade1;
	cout << "1. student's 1. exam grade: ";
	cin >> student1Examgrade1;
	cin.ignore(99,'\n');

	int student1Examgrade2;
	cout << "1. student's 2. exam grade: ";
	cin >> student1Examgrade2;
	cin.ignore(99,'\n');

	int testaverage = (student1Examgrade1 + student1Examgrade2) / numberOFexams;

//// This is the output

	cout <<endl << endl << "Enter the number of students: " << numberOFstudents << endl;
	cout << "Enter the number of exams: " << numberOFexams << endl;
	cout << "1. student's name: " << student1name << endl;
	cout << "1. student's school number: " << student1SchoolNumber << endl;
	cout << "1. student's 1. exam grade: " << student1Examgrade1 << endl;
	cout << "1. student's 2. exam grade: " << student1Examgrade2 << endl;
	cout << "The avarege of this student is: " << testaverage << endl;

	cin.ignore(99,'\n');
	return 1;
}


Tested your input, Output looks like this:
Enter the number of students: 2
Enter the number of exams: 2
1. student's name: michael
1. student's school number: 5000
1. student's 1. exam grade: 50
1. student's 2. exam grade: 40
The average of this student is: 45
Last edited on
no buddy, i have to do it with only dynamic allocation...

there is no problem here:

1
2
3
4
int *x;
x = (int *) malloc(0 * sizeof(int));
int value = 5;
x = (int *) realloc(x,(value)*sizeof(int));


but i cant use that for strings... :\
up up
closed account (zb0S216C)
Is this straight C or C++?

whocares21 wrote:
x = (int *) malloc(0 * sizeof(int)); (sic)

Why allocate 0 bytes?

whocares21 wrote:
but i cant use that for strings (sic)

STL strings or C-Strings?

Here's a tip, you're using C++ right? Use new/delete.

Wazzak
Last edited on
Topic archived. No new replies allowed.