Welcome to Minetown!
To join our community, please login or register!
Internet Explorer
Internet Explorer is not supported. Please upgrade to a more modern browser.

[Enjin Archive] This weeks C++ homework!
Started by Unknown User

wow i am Lerning Python Nao ...C++ Here I come
I'm learning to use C++ and Java from this amazing you tube channel. Have a look at his channel!

http://www.youtube.com/user/thenewboston?feature=chclk
2.Write a program that takes the average of an arbitrary number of integers, displaying the result as a double with two decimal places of accuracy. The output of the program is shown below where the inputs entered by the user have been highlighted.

How many numbers would you like to average? 6

Enter Number 1: 123

Enter Number 2: 452

Enter Number 3: 12

Enter Number 4: 4

Enter Number 5: 123

Enter Number 6: 5

The average of your 6 numbers is 119.83

3.Modify your program so that the user can continue averaging numbers if they would like to. The output of the modified program is shown below where the inputs entered by the user have been highlighted.

How many numbers would you like to average? 3

Enter Number 1: 1234

Enter Number 2: 7635

Enter Number 3: 23

The average of your 3 numbers is 2964.00

Would you like to average some more numbers? (y or n)y

How many numbers would you like to average? 5

Enter Number 1: 5

Enter Number 2: 1234

Enter Number 3: 657

Enter Number 4: 234

Enter Number 5: 567

The average of your 5 numbers is 539.40

Would you like to average some more numbers? (y or n)n

Goodbye!

Okay, this is starting to get a little complicated. HALP CRYSTON!!!!
Got it! Took me like 1 hour to figure out but I got it.

#include <iostream>

#include <iomanip>

using namespace std;

int main()

{

//Initializing the variables for the loop.

int num;

int numCount;

double average = 0;

char again;

int val = 1;

do

{

//resetting the variables back to the defaults for if the user wants to loop this.

val = 1;

average = 0;

//Asking the user for how many numbers they want to average.

cout << "How many numbers would you like to average? ";

cin >> numCount;

while (val <= numCount)

{

cout <<"Enter Number " << val++ << ": ";

cin >> num;

average += num;

}

cout << "The average of your " << numCount << " numbers is " << fixed << setprecision(2) << (average/numCount) << endl;

cout << "\nWould you like to average some more numbers? (y or n)";

cin >> again;

cout << "\n";

} while (again == 'Y' || again == 'y');

cout << "Goodbye!";

cin.get();

cin.get();

return 0;

}
The Guessing Game

The goal of this project is to create a simple guessing game that emphasizes the concepts and skills you have learned thus far. Your program should consist of a single cpp file. The behavior of the program is shown below. The functions that you MUST implement are described in a table at the end of this document. Make sure you use function prototypes at the beginning of your program.

Case 1: User selects an invalid menu option.

#########################################################

# Guessing Game v1.0 #

#########################################################

Please select from the following menu options:

a) Play the game!

b) Configure the game

q) Quit the game

> r

r is an invalid selection. Please try again.

Case 2: User tries to play but the configuration file does not exist yet.

#########################################################

# Guessing Game v1.0 #

#########################################################

Please select from the following menu options:

a) Play the game!

b) Configure the game

q) Quit the game

> a

Error opening config file! Goodbye!



Case 3: User tries to configure the game but gives an invalid number range.

#########################################################

# Guessing Game v1.0 #

#########################################################

Please select from the following menu options:

a) Play the game!

b) Configure the game

q) Quit the game

> b

Enter the range of numbers to choose from separated by a space: 10 1

Your selection was not valid.

Enter the range of numbers to choose from separated by a space: 1 10

The contents of the configuration file (config.txt) should be:

1

10

Case 4: Playing the game. Notice the error message if the number is out of range. It also tells the user to guess lower or higher based on their guess. It also tells the user how many attempts they took once they guess correctly. The menu should be shown again after the game is over.

#########################################################

# Guessing Game v1.0 #

#########################################################

Please select from the following menu options:

a) Play the game!

b) Configure the game

q) Quit the game

> A

I am thinking of a number between 1 and 10 ...

What is your guess? 11

Your guess is out of range. You just wasted a guess <object class="emojione" data="https://resources.enjin.com/1489581540/themes/core/images/emojione/svg/2639.svg?0" type="image/svg+xml" standby=":-(">:-(</object>

What is your guess? 0

Your guess is out of range. You just wasted a guess <object class="emojione" data="https://resources.enjin.com/1489581540/themes/core/images/emojione/svg/2639.svg?0" type="image/svg+xml" standby=":-(">:-(</object>

What is your guess? 5

Lower!

What is your Guess? 2

Higher!

What is your guess? 3

Great job! You guessed correctly! It took you 5 attempts.

Press any key to continue.



Function Name Description

main This function should call displayMenu to display the main menu. It will also continue looping until the user selects the quit option (‘q’ or’ Q’). Display an error message if an invalid option is selected. Call the playGame function if option a/A is selected. Call the configureGame function if option b/B is selected. Display a message before exiting if q/Q is selected.

displayMenu This function does not return anything and does not take in any parameters. It simply prints out the main menu with the various game options. The menu should look like the following:

#########################################################

# Guessing Game v1.0 #

#########################################################

Please select from the following menu options:

a) Play the game!

b) Configure the game

q) Quit the game

>

playGame This function does not return anything and does not take in any parameters. It should be called when the user selects option ‘a’ (or ‘A’) and controls the flow of the game. It should first get the number range and the number to be guessed by calling the initialize function. Examples of game behavior were detailed above.

configureGame This function does not return anything and does not take in any parameters. It should be called when the user selects option b/B. It is responsible for prompting the user for a low and a high number. It should verify that low is less than high. These two numbers will then be written to a configuration file (e.g. config.txt) by calling writeConfiguration.

writeConfiguration This function does not return anything but takes in two integers. The first integer is the low value and the second is the high value. These numbers are then written to the config.txt file. This function should be called by the configureGame function described above.

initialize This function returns a random number which the user should be trying to guess. It also has two integer parameters, low and high, which are passed in by reference. When this function is called it should do the following:

- Initialize the random number generator with a seed value so that a different random number is generated every time the program is run

- Attempt to load the configuration file (config.txt). If it fails to load, print an error and exit the program immediately.

- Read the low and high numbers from the configuration file. The values should be stored in the parameters of this function, since they are passed in by reference. This will allow the caller of this function (playGame) to retrieve the number range.

- Return a random number that is between the values specified in the config file.
do you need help or not
Yeah I can use some help on this one. Functions are new to me.
#include <iostream>

#include <fstream>

#include <cstdlib>

#include <ctime>

using namespace std;

//The functions.

void displayMenu();

void playGame(int, int, int);

void configureGame();

void writeConfiguration(int, int);

void initialize();

//Main

int main()

{

//The Users selection for the menu.

char selection;

displayMenu();

do

{

cout << "\n> ";

cin >> selection;

switch (selection)

{

case 'a': initialize();

break;

case 'A': initialize();

break;

case 'b': configureGame();

break;

case 'B': configureGame();

break;

case 'q': cout << endl << "Goodbye!";

return 0;

break;

case 'Q': cout << endl << "Goodbye!";

return 0;

break;

default: cout<< selection << " is an invalid selection. Please try again." << endl;

selection = 'W';

}

} while (selection == 'W');

cin.get();

cin.get();

return 0;

}

//DisplayMenu. This just shows the menu.

void displayMenu()

{

cout << endl;

cout << "#########################################################" << endl;

cout << "# Guessing Game v1.0 #" << endl;

cout << "#########################################################" << endl;

cout << "\n Please select from the following menu options:" << endl;

cout << " a) Play the game!" << endl;

cout << " b) Configure the game" << endl;

cout << " q) Quit the game" << endl;

}

//ConfigureGame. In this function we ask for 2 numbers then pass it on to the WriteConfiguration function.

void configureGame()

{

int num1;

int num2;

do

{

cout << endl;

cout << endl;

cout << "Enter the range of numbers to choose from separated by a space: ";

cin >> num1;

cin >> num2;

if(num1>num2){

cout << endl;

cout << "Your selection was not valid.";

}

}while (num1>num2);

writeConfiguration(num1, num2);

}

//WriteConfiguration. In this function we take the 2 numbers from the last function and put them in the config file.

void writeConfiguration(int num1, int num2)

{

ofstream outputFile;

outputFile.open("config.txt");

outputFile << num1 << endl;

outputFile << num2 << endl;

outputFile.close();

main();

}

//Initialize. In this function we generate a random number through the numbers passed by through the Config file. Then starts PlayGame.

void initialize()

{

int min;

int max;

int r;

ifstream inFile;

inFile.open("config.txt");

if (!inFile){

cout << endl << "Error opening config file! Goodbye!";

cin.get();

cin.get();

main();

}

inFile >> min;

inFile >> max;

inFile.close();

srand(time(NULL));

r = rand() % max + min;

playGame(min, max, r);

}

//PlayGame. In this function we play the game using the new values we just got from the initialize function.

void playGame(int min, int max, int r)

{

//I put -9999 as a number just to initialize the variable. I wanted to put a number nobody would use.

int guess = -9999;

int val = 0;

cout << endl;

cout << "I am thinking of a number between " << min << " and " << max << " ..." << endl;

cout << endl;

while (guess != r)

{

cout << "What is your guess? ";

cin >> guess;

val++;

if(guess>max || guess<min){

cout << "Your guess is out of range. You just wasted a guess <object class="emojione" data="https://resources.enjin.com/1489581540/themes/core/images/emojione/svg/2639.svg?0" type="image/svg+xml" standby=":-(">:-(</object>" << endl;

}

if(guess<r){

cout << "Higher!" << endl;

}

if(guess>r){

cout << "Lower!" << endl;

}

}

cout << endl;

cout << "Great job! You guessed correctly! It took you " << val << " attempts." << endl;

cout << endl;

cout << "Press any key to continue.";

cout << endl;

cin.get();

cin.get();

main();

}

It's buggy but I did get it to work. Hopefully there's nothing my professor will catch in this.
ahhhhhhhhhh my head hurts
Comprehensive, Offline, Universal and Resilient Student Evaluation DataBase

(aka COURSE DB)

In this project you will create a simple database system (COURSE DB) for storing student’s course grades. Your program will create and access a text file with the student’s grades. The database will contain two pieces of information for each student: the student’s name (a string) and their current grade (a double). Assume that each student can be uniquely identified by their first name. The database file should look like the following after it is created by your program:

Bjarne 99.00

Mary 99.50

Dexter 92.30

Bob 75.67

Susan 98.32

Your database system will provide the following functionality:

1. Create a new database

This operation will create a new text file in the format shown above. It will allow a maximum of 20 students to be entered into the database. Your program should have a constant global variable that defines the maximum number of students allowed (e.g. MAX_NUM_STUDENTS). By simply changing the value of this constant in your code, your program should be able to support different maximum class sizes. The program should prompt the user for each student’s name and grade. It should verify a valid grade (between 0 and 100) was entered. If a value of DONE is typed for the student’s name, then the program should stop asking for names and grades. The data entered by the user should then be written to a file. Allow the student’s name to be up to 32 characters in length and assume no spaces will be entered (i.e. first name only). Again, this should be defined as a global constant variable (e.g. MAX_NAME_LENGTH). If the user enters the same name twice your program should display an error and ask them to enter a unique student name.

2. View the current database contents

This operation will load the contents of the database from a file formatted as described above. It will print out the name of each student and their class grade. It will also print the current class average.

3. Modify a student’s grade.

This operation will first load the current database then display all the student’s names and their current grades. It will then ask the user to type in the name of the student whose grade they want to change. If an invalid name is entered, the user should be prompted to enter another name. The program should then ask for the new grade and verify that it is between 0 and 100. Finally, the file should be written again with the updated grade.

Some General Guidance

Your program must use arrays to store data. Use your judgment to figure out how many functions to create (hint: more than two!). Other than constants, do not use any other global variables in your program. We talked about using the strcmp function to compare two strings.