Please help me on my program

Feb 28, 2017 at 10:54am
I'm having so much trouble with my own program.
I tried to ask the user for the size of the array.
But ends up keep telling me about the variable "input" is not a constant int. Please help me out. It's my homework, and I got stuck for past couple hours.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
 int main() {

	int *p1;
	string *p2;
	cout << "How many score you will enter : ";
	int input;
	cin >> input;

	int *p1 = new int[input];
	string *p2 = new string[input];
	
	string name_array[*p2];
	int score_array[*p1];
	int size = *p1;
	}
Feb 28, 2017 at 11:09am
string name_array[*p2];

What are you trying to do here? It looks as though you're trying to declare an array of strings. However, you're trying to use *p2 as the size, and *p2 isn't a number, it's a string.

Similar question for the following line. Although *p1 is an integer, you can't use it as the size in an array declaration, because the size needs to be a compile-time constant.
Last edited on Feb 28, 2017 at 11:10am
Feb 28, 2017 at 4:58pm
I don't know what you are trying to do here. However, from what I see here, you are using pointer. Pointer is a reference to a memory address, you can't use *p1 or *p2 as size.

Also you initialize p1 and p2 two time, and when you assign p1 as an array, you can't use p1 as the size integer too.

To test your code, you can try to comment out the code bellow 8 and but cout<<input; see if it will output input properly.
Feb 28, 2017 at 7:18pm
On lines 12 and 13, you can only create an array if the size is constant. That means the compiler has to be able to determine the size when you compile the program. If you want to create a variable sized array, you have to do it with new line you have on lines 9 and 10.

Why don't you explain what the program is supposed to do. Then we'll be able to help you better.
Feb 28, 2017 at 10:18pm
This program is supposed to ask the user about how many score and name hes goin to enter. If the user entered 5, the program will ask for score and name 5 times. This is only part of my program.
Feb 28, 2017 at 11:44pm
Can you use a vector instead of an array? Have you learned about vectors yet?
Mar 1, 2017 at 2:12am
If the user entered 5, the program will ask for score and name 5 times.
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
#include <iostream>
#include <string>
#include <limits>

int main()
{
    size_t entries{};
    std::cout << "How many? \n";
    std::cin >> entries;
    //input validation:http://www.cplusplus.com/forum/beginner/206234/
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    std::string* names = new std::string[entries];
    double* scores = new double[entries];
    size_t i{};
    while (i < entries)
    {
        std::cout << "Entry " << i + 1 << " of " << entries << "\n";
        std::cout << "Enter name \n";
        getline(std::cin, *(names + i));
        std::cout << "Enter score \n";
        std::cin >> *(scores + i);
        std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
        ++i;
    }
    for (size_t i = 0; i < entries; ++i)
    {
        std::cout << "Name: " << *(names + i) << " Score: " << *(scores + i) << "\n";
    }
    delete []names; delete []scores;
}
Mar 1, 2017 at 2:13am
no, the instructor told me to use "Dynamic Memory Allocation".
If I can use vector, I would just use vector instead.
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
#include <iostream>
#include <string>
using namespace std;

void initializeArrays(string names[], int scores[], int size) {

	for (int i = 0; i < size; i++) {
		cout << "Enter the name for score #" << i + 1 << ": ";
		getline(cin, names[i]);
		cout << "Enter the score for score #" << i + 1 << ": ";
		cin >> scores[i];

		cin.ignore();
	}
}

int findBiggest(int scores[], int startingIndex, int size) {
	int targetIndex = startingIndex;

	for (int i = startingIndex + 1; i < size; i++) {
		if (scores[i] > scores[targetIndex]) {
			targetIndex = i;
		}
	}

	return targetIndex;
}

void sortData(string names[], int scores[], int size) {

	for (int i = 0; i < size; i++) {
		swap(names[findBiggest(scores, i, size)], names[i]);
		swap(scores[findBiggest(scores, i, size)], scores[i]);
	}
}

void displayData(string names[], int scores[], int size) {
	cout << endl << "Top Scorers:" << endl;

	for (int i = 0; i < size; i++) {
		cout << names[i] << ": " << scores[i] << endl;
	}
}

int main() {

	int *p1;
	string *p2;
	cout << "How many score you will enter : ";
	int input;
	cin >> input;

	int *p1 = new int[input];
	string *p2 = new string[input];
	
	string name_array[*p2];
	int score_array[*p1];
	int size = *p1;
	

	initializeArrays(name_array, score_array, size);

	sortData(name_array, score_array, size);

	displayData(name_array, score_array, size);

}


This is the whole program.
I only stuck on asking user to declare the array size for the string array and int array.
Because it has to be constant
Last edited on Mar 1, 2017 at 2:15am
Mar 1, 2017 at 4:34am
P1 is declared twice first of. Then you need to make a loop of input times. Use unsigned value as well if at least 1 for the size for allocation. Initialize your element during the loop then access element by index.
Mar 1, 2017 at 11:10am
Because it has to be constant

Not if you're dynamically allocating the array, as you're doing in lines 53 - 54.

You seem to have completely ignored my previous post. Why are you declaring and allocating memory for two arrays on lines 53 - 54, and then immediately trying to create two more arrays on 56 - 57? And why are you trying to use a string as if it were an integer on line 53?

What's the point in us posting and explaining what you're doing wrong, if you're going to ignore our posts?
Last edited on Mar 1, 2017 at 11:10am
Mar 1, 2017 at 1:48pm
closed account (48T7M4Gy)
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
#include <iostream>
#include <string>

using namespace std;

void initializeArrays(string names[], int scores[], int size) {
    
    for (int i = 0; i < size; i++) {
        cout << "Enter the name for score #" << i + 1 << ": ";
        cin.ignore(1000, '\n');
        getline(cin, names[i]);
        
        cout << "Enter the score for score #" << i + 1 << ": ";
        cin >> scores[i];
    }
}

int findBiggest(int scores[], int startingIndex, int size) {
    int targetIndex = startingIndex;
    
    for (int i = startingIndex + 1; i < size; i++) {
        if (scores[i] > scores[targetIndex]) {
            targetIndex = i;
        }
    }
    
    return targetIndex;
}

void sortData(string names[], int scores[], int size) {
    
    for (int i = 0; i < size; i++) {
        swap(names[findBiggest(scores, i, size)], names[i]);
        swap(scores[findBiggest(scores, i, size)], scores[i]);
    }
}

void displayData(string names[], int scores[], int size) {
    cout << endl << "Top Scorers:" << endl;
    
    for (int i = 0; i < size; i++) {
        cout << names[i] << ": " << scores[i] << endl;
    }
}

int main() {
    
    cout << "How many score you will enter : ";
    
    int limit = 0;
    cin >> limit;
    
    int *score_array = new int[limit];
    string *name_array = new string[limit];
    
    initializeArrays(name_array, score_array, limit);
    
    sortData(name_array, score_array, limit);
    
    displayData(name_array, score_array, limit);
    
}


How many score you will enter : 4
Enter the name for score #1: Will Dill
Enter the score for score #1: 9
Enter the name for score #2: Gill Dill
Enter the score for score #2: 7
Enter the name for score #3: Bill Dill
Enter the score for score #3: 8
Enter the name for score #4: Jill Dill
Enter the score for score #4: 3

Top Scorers:
Will Dill: 9
Bill Dill: 8
Gill Dill: 7
Jill Dill: 3
Program ended with exit code: 0
Last edited on Mar 1, 2017 at 1:55pm
Topic archived. No new replies allowed.