Need Help with New C++ Program? Thanks.

closed account (2zApDjzh)

Hello everyone, I'm new to C++ and this forum. I really need some help getting this code to work, but its not working the way I want it to. I cannot get the cout statements to display and for some reason my program does not ask the user if they want to play again. I will post the instructions and my code below, thanks. If you can, please answer my questions with the whole code in a corrected form so I could get an overall look of the accurate code for my program.

Write a C++ console application that accepts up to 5 numbers from the user. Display
all numbers, the highest, the lowest, and the average of the numbers. Ask the user if
they want to continue entering another set of numbers.


Tell the user what the program is all about. Do NOT start the program with
“Enter a number”!!
4) Create an array to store the numbers.
5) The user does not have to enter all 5 numbers. They can enter fewer. However,
of course, you need at least two numbers to be able to look for the highest and
the lowest. Make sure you explain this to the user.
6) Create three functions to perform the following tasks:
a. Calculate the highest number.
b. Calculate the lowest number.
c. Calculate the average.
7) All three functions receive an array and its size, and return a single value.
8) Keep track of the number of the values the user enters. Remember, they do
not have to enter all 5 numbers.


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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117

#include <iostream>
#include <cmath>
#include <cstdlib>
#include <string>
#include <ctime>
using namespace std;

int populate(double array1[], int number);			// Prototype of the array function 'int populate'.
void spit(double array1[], int number);				// Prototype of the array function 'void split'.
int add(double array1[], int number);				// Prototype of the array function 'int '.
int highest_value(double array1[], int number);		// Prototype of the array function 'int highest_value'.
int lowest_value(double array1[], int number);		// Prototype of the array function 'int lowest_value'.
int average_number(double array1[], int number);	// Prototype of the array function 'int average_number'.
bool play_again;
int num_games = 0;
string play;
num i=5;

int main(){

	double input[5];				// Declaring an array to use for the function arrays.

	populate(input, 5);				// Calls the array function 'int populate'.
	spit(input, 5);					// Calls the array function 'void split'.
	add(input, 5);					// Calls the array function 'int plus'.
	highest_value(input, 5);		// Calls the array function 'int highest_value'.
	lowest_value(input, 5);			// Calls the array function 'lowest_value'.
	average_number(input, 5);		// Calls the array function 'average_number'.

	cout << endl;
	cout << "------------------------------------------" << endl;

	system("pause");
	return 0;
}

int populate(double array1[], int number){  // User declares input.
	double input;
	cout << endl << endl;
	cout << " Hello welcome to my game. You could enter up to 5 numbers."; //Introduction to program.
	cout << "Once you enter your desired numbers, the program will display up the highest, lowest, and average of numbers.";
	cout << "Enjoy!";
	// Ask how many 
	cout << "How many numbers do you want to enter?" << endl;
	cin >> num;
	int array[nums];
	for (int i = 0; i < number; i++){
		cout << "enter value for item " << (i + 1) << ": "; //Enter value.
		cin >> input;
		cout << endl;
		array1[i] = input;
	}
	// Ask if the player would like to play again.
	cout << endl << endl << "Would you like to play again?: ";
	cin >> play;

	// Exit the loop if the player chooses no.
	if (play == "n" || play == "N"){
		play_again = false;
	}
	else if (play == "y" || play == "Y"){
		num_games++;
	}
	cout << "------------------------------------------" << endl;
	return input;
}

void spit(double array1[], int number){  // The console prints what the user input.

	cout << endl << endl << "CONSOLE LOG" << endl;
	cout << "You entered: " << endl;
	for (int i = 0; i < number; i++){
		cout << endl;
		cout << endl << "the value of item " << (i + 1) << " = " << array1[i];
	}
	cout << endl << endl << "-------------------------------------------" << endl;
}

int add(double array1[], int number){       // + together the users input, so it = total amount.
	int sum = 0;
	for (int i = 0; i < number; i++){
		sum += array1[i];
	}
	cout << endl << endl << "The total of all the 5 numbers" << " = " << sum << endl << endl;
	return sum;
}

int highest_value(double array1[], int number){    // Finds the highest value the user input.
	int temp = 0;
	for (int i = 0; i < number; i++){
		if (array1[i] > temp)
			temp = array1[i];
	}
	cout << endl << "The highest value of all the 5 numbers" << " = " << temp << endl << endl;
	return temp;
}

int lowest_value(double array1[], int number){     // Finds the lowest value the user input.
	int small = array1[0];
	for (int i = 0; i < number; i++){
		if (array1[i] < small)
			small = array1[i];
	}
	cout << endl << "The lowest value of all the 5 numbers" << " = " << small << endl << endl;
	return small;
}

int average_number(double array1[], int number){   // Finds the average number the user input.
	double sum = 0;
	for (int i = 0; i < number; i++){
		sum += array1[i];
	}
	sum = sum / number;
	cout << endl << "The average value of all the 5 numbers" << " = " << sum << endl << endl;
	return sum;
}
First thing- it won't compile because line 18 is invalid.

Now, don't take this the wrong way, but for comments, I prefer this basic criteria: if my reaction to the comment is "no shit," then axe the comment. Of course, if it helps you remember, feel free to- but try to avoid stating the obvious.

Furthermore, your code wouldn't work even if it did compile. Arrays are fixed-sized on compile- in other words, you can't create an array of size given on user input unless you create the array dynamically. Ergo, line 47 is... well, bad.

Your populate function only lets you put 5 elements into the array despite having the array be (incorrectly) created to a size of user's input.

Otherwise, the rest of the code works. Also, none of your functions need to return any value, because they all print out their results. So make everything but main void.

Also, your populate function isn't a loop, and will only run once regardless of the user's response to "play again."

Also, don't use global variables.

And avoid creating variables that do absolutely nothing. num_games, for example, does nothing whatsoever.

Last edited on
Topic archived. No new replies allowed.