Question about arrays

Need to write a function that takes in grades as arrays. However, to start, you ask the user for data, such as "cout << "Please enter all of the grades you would to input: ";".
My question is, how would you determine the length dynamically, using strlen()?

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
#include <iostream>
#include <iomanip>

using namespace std;

// Prototype Functions
double getAverage(int, int);
double getMedian(int, int);
int getMode(int, int);
double getSd(int, int);

int main()
{
	int grades[100];
	int size = 0;
	cout << "Please enter all of the grades you would like to input: ";

	// *Insert code here to determine the length dynamically


	cout << "Your numbers are ";

	for (int i = 0; i < size; i++)
	{
		cout << grades[i] << endl;
	}

	return 0;

}

/*double getAverage(int *grades, int size)
{

}
double getMedian(int *grades, int size)
{

}
int getMode(int *grades, int size)
{

}
double getSd(int *grades, int size)
{

}*/
First you need to decide the input format.

What if I entered '988712634485' Assuming your program would take care of spacing the numbers.? Would that be the same as, '98 87 12 63 44 85' or what if the true grades were '98, 87, 12, 63, 44, 8, 5'. You need to first tell the user how to enter the grades.

After you have done this, if you have things like spaces, that would account in your strlen().

A simpler way to do it is to increment your size variable inside of a loop that will receive the grades. If you need to delete a grade, just decrement the size variable.

Assuming these are grades, they cannot be negative, and are between 0-100 +/- extra credit.

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
int grades[100];
int size = 0;

while (/*loop condition*/)
{
	int tempGrade;
	cout << "Enter the grades, if you would like to stop entering grades enter -1 at any time";

	//input grade

	if (tempGrade == -1)
	{
		// leave loop
	}
	else{
		// Place tempGrade at grades[size]
		// Increment size;
	}
}
	cout << "Your numbers are ";

	for (int i = 0; i < size; i++)
	{
		cout << grades[i] << endl;
	}
}


This is assuming you are being mandated to use an array.
Last edited on
How would i account for characters that are inputted as tempGrade?
You mean, how do you account for non-numeric data?

http://www.cplusplus.com/forum/beginner/157545/
I don't understand std::. Now I don't quite understand why my function is not running properly. I'm trying to input data between 1-100 but it essentially ignores everything I input

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
#include <iostream>
#include <iomanip>

using namespace std;

// Prototype Functions
double getAverage(int, int);
double getMedian(int, int);
int getMode(int, int);
double getSd(int, int);

int size = 0;

int main()
{
	int grade[10];
	int tempGrade = 0;
	bool loop = true;
	cout << "Please enter after each of the grades (0-100) you would like to input." << endl;
	cout << "Type -1 if you finished entering your grades." << endl;
	while (loop)
	{
		cin >> tempGrade;

		while (!isdigit(tempGrade))
		{
			cout << "You did not enter a digit. Please try again." << endl;
			cin >> tempGrade;
		}

		if (tempGrade == -1)
		{
			loop = false;
		}
		else if (tempGrade <= 0)
		{
			cout << "You cannot input any negative numbers. Please input between 0-100." << endl;
		}
		else if ((tempGrade > 0) && (tempGrade <= 100))
		{
			grade[size] = tempGrade;
			size++;
		}
		else
		{
			cout << "That number is too high! Please input between 0-100." << endl;
		}
	}
	cout << "Your numbers are ";
	for (int i = 0; i < size; i++)
	{
		cout << grade[i] << endl;
	}
	return 0;
}
Last edited on
First of all, you have to declare size.

 
int size = 0;

Somewhere at the top.

Second.

1
2
3
4
5
while (!isdigit(tempGrade))
		{
			cout << "You did not enter a digit. Please try again." << endl;
			cin >> tempGrade;
		}


Do you really need this? For now, Just trust the user to enter a digit and not letters.

The program should run fine at that point. But you should have some kind of text each time the user inputs a grade, So it doesnt just look empty.

 
cout << "Please enter another grade ";


Here is the entire code -

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
int size = 0;
	int grade[10];
	int tempGrade = 0;
	bool loop = true;

	cout << "Please enter after each of the grades (0-100) you would like to input." << endl;
	cout << "Type -1 if you finished entering your grades." << endl;

	while (loop)
	{
	        cin >> tempGrade;

		if (tempGrade == -1)
		{
			loop = false;
		}
		else if (tempGrade <= 0)
		{
			cout << " You cannot input any negative numbers. Please input between 0-100." << endl;
		}
		else if ((tempGrade > 0) && (tempGrade <= 100))
		{
			grade[size] = tempGrade;
			size++;
			cout << "Please enter another grade ";
		}
		else
		{
			cout << " That number is too high! Please input between 0-100." << endl;
		}
	}
	cout << "Your numbers are: " << endl;
	for (int i = 0; i < size; i++)
	{
		cout << grade[i] << endl;
	}
Last edited on
Topic archived. No new replies allowed.