I'm trying to output the score for each addition, subtraction, multiplication, division in the end test function. and the total of the question asked.
**Any help would be appreciated
BTW this is only using local functions and is passing the scores by reference. IT's supposed to output the number of answers that you answer correctly.
Also is there a way where you can prevent the program from letting you select the choices more than once?
here's what I got so far:
#include < iostream >
#include < iomanip >
#include < stdlib.h >
#include < time.h >
#include < stdlib.h >
using namespace std;
int addition(int addscore);
int subtraction(int subscore);
int multiplication(int multiscore);
int division(int divscore);
int endtest(int & addscore, int & subscore, int & multiscore, int & divscore);
main() {
int end_final = 0;
do {
int addscore, subscore, multiscore, divscore;
char choice;
cout << "A- " << "Addition\n";
cout << "B- " << "Subtraction\n";
cout << "C- " << "Multiplication\n";
cout << "D- " << "Division\n";
cout << "E- " << "End of Test\n";
cin >> choice;
cout << endl << endl;
switch (choice) {
case 'A':
case 'a':
addition(addscore);
break;
case 'B':
case 'b':
subtraction(subscore);
break;
case 'C':
case 'c':
multiplication(multiscore);
break;
case 'D':
case 'd':
division(divscore);
break;
case 'E':
case 'e':
endtest(addscore, subscore, multiscore, divscore);
break;
}
}
while (end_final != 1);
return 0;
}
int addition(int addscore) {
int iRandom;
// initialize random seed:
srand(time(NULL));
int answer;
cout << "You have chosen addition\n";
int randnum1, randnum2;
int total = 0;
for (int i = 1; i <= 5; i++) {
randnum1 = rand() % 15 + 1;
randnum2 = rand() % 15 + 1;
cout << randnum1 << " " << "+" << " " << randnum2 << " " << "= ";
cin >> answer;
cin.ignore(80, '\n');
total++;
if (answer == randnum1 + randnum2) {
cout << "Correct! \n";
addscore++;
} else {
cout << "Incorrect \n";
}
}
}
int subtraction(int subscore) {
int iRandom;
// initialize random seed:
srand(time(NULL));
int answer;
cout << "You have chosen subtraction\n";
int randnum1, randnum2;
int total = 0;
while (total != 5) {
randnum1 = rand() % 20 + 1;
randnum2 = rand() % 20 + 1;
if (randnum1 >= randnum2) {
cout << randnum1 << " " << "-" << " " << randnum2 << " " << "= ";
cin >> answer;
cin.ignore(80, '\n');
total++;
if (answer == randnum1 - randnum2) {
cout << endl;
cout << "Correct!\n ";
subscore++;
} else {
cout << "Incorrect\n ";
}
}
}
}
int multiplication(int multiscore) {
int iRandom;
int total = 0;
// initialize random seed:
srand(time(NULL));
int answer;
cout << "You have chosen Multiplication\n";
int randnum1, randnum2;
for (int i = 1; i <= 5; i++) {
randnum1 = rand() % 20 + 1;
randnum2 = rand() % 20 + 1;
cout << randnum1 << " " << "x" << " " << randnum2 << " " << "= ";
cin >> answer;
cin.ignore(80, '\n');
total++;
if (answer == randnum1 * randnum2) {
cout << endl;
cout << "Correct! \n";
multiscore++;
} else {
cout << "Incorrect\n ";
}
}
}
int division(int divscore) {
int iRandom;
// initialize random seed:
srand(time(NULL));
int answer;
cout << "You have chosen Division\n";
int randnum1, randnum2;
int total = 0;
while (total != 5) {
randnum1 = rand() % 13 + 1;
randnum2 = rand() % 13 + 1;
if (randnum1 % randnum2 == 0) {
cout << randnum1 << " " << "/" << " " << randnum2 << " " << "= ";
cin >> answer;
cin.ignore(80, '\n');
total++;
if (answer == randnum1 / randnum2) {
cout << endl;
cout << "Correct! \n";
divscore++;
} else {
cout << "Incorrect\n ";
}
}
}
}
int endtest(int & addscore, int & subscore, int & multiscore, int & divscore) {
int total = 0;
cout << endl << endl;
cout << "Addition" << " " << addscore++ << endl;
cout << "Subtraction" << " " << subscore << endl;
cout << "Multiplication" << " " << multiscore << endl;
cout << "Division" << " " << divscore << endl;
cout << "Total" << " " << total << endl;
}
To clarify the code, declare an enum
enum { ADDITION=0, SUBTRACTION, MULTIPLICATION, DIVISION };
You could have global arrays
int used[] = { 0,0,0,0 }; // not used
int total[] = { 0,0,0,0 }; // total questions per operation
int score[] = { 0,0,0,0 }; // score per operation
Then, doing it for 'addition', in the do {
if ( ! used[ADDITION]) cout << "A- " << "Addition\n";
then in the switch
case 'A':
case 'a':
used[ADDITION] = 1; // tells addition was used
addition(addscore);
break;
The addition code becomes
int addition(int addscore) {
int iRandom;
// initialize random seed:
srand(time(NULL));
int answer;
cout << "You have chosen addition\n";
int randnum1, randnum2;
total[ADDITION] = 0; // should not be
score[ADDITION] = 0; // ...necessary (but in case you call it again)
for (int i = 0; i < 5 ; i++) {
randnum1 = rand() % 15 + 1;
randnum2 = rand() % 15 + 1;
cout << randnum1 << " " << "+" << " " << randnum2 << " " << "= ";
cin >> answer;
cin.ignore(80, '\n');
total[ADDITION]++; // total is incremented for Addition
if (answer == randnum1 + randnum2) {
cout << "Correct! \n";
score[ADDITION]++; // score is incremented
} else {
cout << "Incorrect \n";
}
}
}
endtest becomes
int endtest() {
cout << endl << endl;
if (used[ADDITION]) cout << "Addition" << " " << score[ADDITION] << " / " << total[ADDITION] << endl;
// same for SUBTRACTION ...
int grandtotal = total[ADDITION] + ....;
cout << "Grand Total" << " " << grandtotal << endl;
}
endtest is declared, and called like that
int endtest();
...
endtest();
Since this is C++, you could also make a class Operation, then one subclass per operation, holding the name of the operation and overriding a 'execute' method that does the specific operation, then declare an array of the parent class that holds an instance of each operation ...
Based on OP's comments, after the do {
// initialize variables to zero
int addscore=0, subscore=0, multiscore=0, divscore=0;
in the switch
case 'A':
case 'a':
used[ADDITION] = 1; // tells addition was used
addition( &addscore ); // <== gives the pointer to that variable
break;
then in addition code
int addition(int *addscore) { // <== gets a pointer
int iRandom;
// initialize random seed:
srand(time(NULL));
int answer;
cout << "You have chosen addition\n";
int randnum1, randnum2;
total[ADDITION] = 0; //
score[ADDITION] = 0; // using your way, this is not necessary anymore
for (int i = 0; i < 5 ; i++) {
randnum1 = rand() % 15 + 1;
randnum2 = rand() % 15 + 1;
cout << randnum1 << " " << "+" << " " << randnum2 << " " << "= ";
cin >> answer;
cin.ignore(80, '\n');
total[ADDITION]++; // total is incremented for Addition
if (answer == randnum1 + randnum2) {
cout << "Correct! \n";
// score[ADDITION]++; // score is incremented (see above)
*addscore++; // <== increment variable pointed to by pointer
} else {
cout << "Incorrect \n";
}
}
}
Related
Simple mathgame.cpp.
case(2) is the subtraction of two random integers, but the **results cant be less than 0.
** I need 'subOp' to only hold the values of 'ranNum1' - 'ranNum2' when they equal >= 0
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <string>
using namespace std;
int main()
{
// Initialize random number generator.
srand(time(0));
// Declares variable as a random integer between 0-12
// int ranNum1 = rand() % 13;
// int ranNum2 = rand() % 13;
int menuChoice = 0;
int userAns=0;
// String variable for the game menu
string mathMenu =
"\n\n\tMATH GAME SELECTION\n\t-------------------\n\t1) Addition\n"
"\t2) Subtraction\n\t3) Multiplication\n\t4) -EXIT-\n";
// Answer responses
string cAns = "\n\tCORRECT ANSWER!" , wAns = "\n\tWRONG ANSWER!";
// While the user doesnt request to exit...
while (menuChoice != 4)
{
int ranNum1 = rand() % 13;
int ranNum2 = rand() % 13;
switch (menuChoice)
{
case (0):
break;
case (1): { int addOp = ranNum1 + ranNum2;
cout << "\tWhat is " << ranNum1 << " + " << ranNum2 << " = [?]\n \tYour answer: ";
cin >> userAns;
if (userAns == addOp) {
cout << cAns; }
else{
cout << wAns << "\n\tThe correct answer was " << addOp; }
break; }
case (2): { int subOp = ranNum1 - ranNum2;
do {
cout << "\tWhat is " << ranNum1 << " - " << ranNum2 << " = [?]\n\tYour answer: ";
cin >> userAns;
if (userAns == subOp) {
cout << cAns;
}
else {
cout << wAns << "\n\tThe correct answer was " << subOp;
}
} while (subOp < 0);
break; }
case (3):{ int multOp = ranNum1 * ranNum2;
cout << "\tWhat is " << ranNum1 << " x " << ranNum2 << " = [?]\n \tYour answer: ";
cin >> userAns;
if (userAns == multOp) {
cout << cAns; }
else {
cout << wAns << "\n\tThe correct answer was " << multOp; }
break; }
//Error message/ validator for integers between 1-4.
default: cout << "\t**Invalid menu selection** \n\a";
}
//Error message/ validator for input other than integer.
cout << mathMenu << endl << " Please Select An Option (1-4): ";
while (!(cin >> menuChoice)) {
cin.clear();
cin.ignore(1000, '\n');
cout << "Please ONLY select the given options (1-4): \a";
}
cout << endl;
}
system("pause");
return 0;
}
I've tried several different loops and bools. How do I loop through the equation until the results will only equal >0?
You can make a conditional statement to check whether the second value is greater than the first one. If so, then swap between them.
if (ranNum1 < ranNum2)
std::swap(ranNum1, ranNum2);
int subOp = ranNum1 - ranNum2;
Now, the value of ranNum1 is always greater than ranNum2.
I am writing a yahtzee game for my c++ programming class. One of my difficulties I have ran into is the scoring system for different categories. I think I have figured out how to do it for adding 1s, 2s etc but I do not know how to have the program determine when a 3 of a kind, 4 of a kind, etc has been rolled. Here is my code so far.
#include "stdafx.h"
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
#include <vector>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
//Declare variables
int players;
int turn = 1;
vector<string> names;
string playerName;
int dice[5];
int finalScore = 0;
char reroll[5];
char rollDice;
int tries = 1;
const int DICE = 5;
int roll[DICE];
int scorecard;
int scoreThisTurn(int scorecard);
int turnScore = 0;
//Introduction, get number of players.
cout << "Hello, welcome to Yahtzee! How many players are there?" << endl;
cin >> players;
if (players > 4) {
cout << "Sorry, the maximum number of players is 4." << endl;
cout << "How many players are there?" << endl;
cin >> players;
}
//Get player names
string getNames();
for (int i = 0; i < players; i++) {
cout << "Hello player " << i + 1 << ", please enter your name" << endl;
cin >> playerName;
names.push_back(playerName);
}
srand(time(NULL)); //random seed
cout << "Welcome to Yahtzee!\n";
while (turn <= 13) { //roll dice
cout << "Press 'r' to roll" << endl;
cin >> rollDice;
if (rollDice == 'r') {
for (int i = 0; i < DICE; i++) {
roll[i] = rand() % 6 + 1;
}
}
cout << "You rolled: " << roll[0] << ", " << roll[1] << ", " <<
roll[2] << ", " << roll[3] << ", " << roll[4] << endl;
cout << "Type y to reroll or n to keep. For example yynnn would keep the first three dice" << endl;
cin >> reroll[0] >> reroll[1] >> reroll[2] >> reroll[3] >> reroll[4];
for (int i = 0; i < DICE; i++) {
if (reroll[i] == 'y') {
roll[i] = rand() % 6 + 1;
}
else if (reroll[i] == 'n') {
roll[i];
}
else cout << "Sorry you entered an invalid letter." << endl;
}
cout << "Your second roll is: " << roll[0] << ", " << roll[1] << ", " <<
roll[2] << ", " << roll[3] << ", " << roll[4] << endl;
cout << "Type y to reroll or n to keep. For example yynnn would keep the first three dice" << endl;
cin >> reroll[0] >> reroll[1] >> reroll[2] >> reroll[3] >> reroll[4];
for (int i = 0; i < DICE; i++) {
if (reroll[i] == 'y') {
roll[i] = rand() % 6 + 1;
}
else if (reroll[i] == 'n') {
roll[i];
}
else cout << "Sorry you entered an invalid letter." << endl;
}
cout << "Your third roll is: " << roll[0] << ", " << roll[1] << ", " <<
roll[2] << ", " << roll[3] << ", " << roll[4] << endl;
//displays scorecard categories
cout << "Which category would you like to score this in" << endl;
cout << "1 - ones: " << endl;
cout << "2 - twos: " << endl;
cout << "3 - threes: " << endl;
cout << "4 - fours: " << endl;
cout << "5 - fives: " << endl;
cout << "6 - sixes: " << endl;
cout << "7 - 3 of a kind: " << endl;
cout << "8 - 4 of a kind: " << endl;
cout << "9 - small straight: " << endl;
cout << "10 - large straight: " << endl;
cout << "11 - full house: " << endl;
cout << "12 - yahtzee: " << endl;
cout << "13 - chance: " << endl;
//asks player to choose where to score
cout << "\nEnter 1-14 to choose a category." << endl;
cin >> scorecard;
//assigns points
for (int i = 0; i < DICE; i++) {
turnScore = 0;
if (scorecard == 1) {
if (roll[i] == 1) {
turnScore = turnScore + 1;
}
}
if (scorecard == 2) {
if (roll[i] == 2) {
turnScore = turnScore + 2;
}
}
if (scorecard == 3) {
if (roll[i] == 3) {
turnScore = turnScore + 3;
}
}
if (scorecard == 4) {
if (roll[i] == 4) {
turnScore = turnScore + 4;
}
}
if (scorecard == 5) {
if (roll[i] == 5) {
turnScore = turnScore + 5;
}
}
if (scorecard == 6) {
if (roll[i] == 6) {
turnScore = turnScore + 6;
}
if (scorecard == 7) {
if (roll[i] == 2) {
turnScore = turnScore + 2;
}
}
}
cout << scorecard << endl;
turn++;
}
system("pause");
return 0;
}
As you can see I've set up the scoring for the first 6 categories but don't know how to proceed.
I do not know how to have the program determine when a 3 of a kind, 4 of a kind, etc has been rolled.
Create a variable to help you keep track of the number of dice that have a given number.
int diceCount[DICE] = {0};
and fill up the array with:
for (int i = 0; i < DICE; i++) {
diceCount[roll[i-1]]++
}
Create helper functions to determine whether five, four, or three of a kind have been thrown.
int getNOfAKind(int diceCount[], int N)
{
// This will need moving DICE out of `main`
// making it a global variable.
for ( int i = 0; i < DICE; ++i )
{
if (diceCount[i] == N )
{
return i+1;
}
}
return -1;
}
int getFiveOfAKind(int diceCount[])
{
return getNOfAKind(diceCount, 5);
}
int getFourOfAKind(int diceCount[])
{
return getNOfAKind(diceCount, 4);
}
int getThreeOfAKind(int diceCount[])
{
return getNOfAKind(diceCount, 3);
}
and use it as:
int fiveCount = getFiveOfAKind(diceCount);
if ( fiveCount != -1 )
{
}
int fourCount = getFourOfAKind(diceCount);
if ( fourCount != -1 )
{
}
int threeCount = getThreeOfAKind(diceCount);
if ( threeCount != -1 )
{
}
My assignment is to create a guessing game where the computer guesses my number and one where I guess the computer's number. I have my code written out but It's incomplete. I want to create a menu so the user can choose which game he wants to play, and also i am having trouble joining the two programs into one so they both will run. Can someone please assist me. The first game works fine but from there I really don't know what I'm doing.
#include<iostream>
#include<ctime>
#include<cstdlib>
using namespace std;
int main()
{
const int min = 1;
const int max = 100;
int num = 2 && 3 && 4;
int prevMin = 0;
int prevMax = 0;
int tries = 0;
int guess = 0;
int userNum = 0;
int userGuess = 0;
int systemNum = 0;
int systemGuess = 0;
cout << "Welcome to the Guessing Game!" << endl;
cout << "Enter a 2 to play Game 1 or a 3 to play Game 2. Or enter 4 to quit." << endl;
cin >> num;
if (num == 2)
{
cout << "Welcome to Game 1!" << endl;
cout << "User, enter a random number between " << min << " and " << max << " : "; cin >> userNum;
}
srand(time(0));
systemGuess = rand() % 100 + 1;
do
{
cout << "System, guess the user's number between " << min << " and " << max << ": " << systemGuess << endl;
cin.get();
++tries;
if (systemGuess > max || systemGuess<min)
{
cout << "I said guess a number between " << min << " and " << max << " stupid." << endl;
}
if (systemGuess > userNum)
{
cout << "Too high. Guess lower." << endl;
prevMax = systemGuess;
systemGuess = rand() % (prevMax - prevMin) + prevMin;
if (systemGuess == prevMin)
systemGuess++;
}
else if (systemGuess < userNum)
{
cout << "Too low. Guess higher." << endl;
prevMin = systemGuess;
systemGuess = rand() % (prevMax - prevMin) + prevMin;
if (systemGuess == prevMin)
systemGuess++;
}
} while (systemGuess != userNum);
cout << systemGuess << endl;
cout << " I guessed it right! It took me " << tries << " guess(es). " << endl;
srand(time(0));
systemNum = rand()% 100 + 1;
//Beginning of second game
do
{
if (num == 3)
{
cout << "Welcome to Game 2!" << endl;
cout << "User, try to guess the computer's number that's between " << min << " and " << max << " . " << endl;
}
cout << "Enter your guess." << endl;
cin >> userGuess;
tries++;
if (userGuess > systemNum)
{
cout << "Too high. Guess lower." << endl;
cin >> userGuess;
}
else if (userGuess < systemNum)
{
cout << "Too low. Guess higher." << endl;
cin >> userGuess;
}
else
{
cout << "Correct! It took you long enough! Lol... " << endl;
cin >> userGuess;
}
while (userGuess != systemNum);
}
system("pause");
return 0;
}
Make the two games two distinct functions (each with its own local variables), create a third one that is a loop that prompts for the games (and break when "quit" is selected), and call the corresponding function, then call the third one from main.
You can use if...else :
cout << "Welcome to the Guessing Game!" << endl;
cout << "Enter a 2 to play Game 1 or a 3 to play Game 2. Or enter 4 to quit." << endl;
cin >> num;
if (num == 2)
{//game1
}
else if(num==3)
{//game2
}
else if(num==4)
{return 0;
}else
{
cout<<"Invalid choice!";
system("pause");
return 1;
}
//print highscores
system("pause");
return 0;
Your whole code:
#include<iostream>
#include<ctime>
#include<cstdlib>
using namespace std;
int main()
{
const int min = 1;
const int max = 100;
int num = 2 && 3 && 4;
int prevMin = 0;
int prevMax = 0;
int tries = 0;
int guess = 0;
int userNum = 0;
int userGuess = 0;
int systemNum = 0;
int systemGuess = 0;
srand(time(0));
cout << "Welcome to the Guessing Game!" << endl;
cout << "Enter a 2 to play Game 1 or a 3 to play Game 2. Or enter 4 to quit." << endl;
cin >> num;
if (num == 2)
{
cout << "Welcome to Game 1!" << endl;
cout << "User, enter a random number between " << min << " and " << max << " : "; cin >> userNum;
systemGuess = rand() % 100 + 1;
do
{
cout << "System, guess the user's number between " << min << " and " << max << ": " << systemGuess << endl;
cin.get();
++tries;
if (systemGuess > max || systemGuess<min)
{
cout << "I said guess a number between " << min << " and " << max << " stupid." << endl;
}
if (systemGuess > userNum)
{
cout << "Too high. Guess lower." << endl;
prevMax = systemGuess;
systemGuess = rand() % (prevMax - prevMin) + prevMin;
if (systemGuess == prevMin)
systemGuess++;
}
else if (systemGuess < userNum)
{
cout << "Too low. Guess higher." << endl;
prevMin = systemGuess;
systemGuess = rand() % (prevMax - prevMin) + prevMin;
if (systemGuess == prevMin)
systemGuess++;
}
} while (systemGuess != userNum);
cout << systemGuess << endl;
cout << " I guessed it right! It took me " << tries << " guess(es). " << endl;
}
systemNum = rand()% 100 + 1;
//Beginning of second game
if (num == 3)
{
do
{
cout << "Welcome to Game 2!" << endl;
cout << "User, try to guess the computer's number that's between " << min << " and " << max << " . " << endl;
cout << "Enter your guess." << endl;
cin >> userGuess;
tries++;
if (userGuess > systemNum)
{
cout << "Too high. Guess lower." << endl;
cin >> userGuess;
}
else if (userGuess < systemNum)
{
cout << "Too low. Guess higher." << endl;
cin >> userGuess;
}
else
{
cout << "Correct! It took you long enough! Lol... " << endl;
cin >> userGuess;
}}
while (userGuess != systemNum);
}else if(num==4)return 0;
else{
cout<<"Invalid choice!";
system("pause");
return 1;
}
//print highscores
system("pause");
return 0;
}
At this point in my program, I am writing for every time a random number is generated, the contents of that number in an array is written to a text file. The code:
#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
#include <time.h>
#include "inFrench.h"
using namespace std;
class student
{
int m_studentNumber;
public:
string nameFirst;
string nameLast;
string nameFull;
int getStudentNumber() { return m_studentNumber; }
void setStudentNumber(int studentNumber) { m_studentNumber = studentNumber; }
};
ostream& operator<<(ostream& os, const student& s)
{
return os << s.nameFirst << ' ' << s.nameLast;
}
student typeName()
{
student bar;
cout << "Type in a student's first name: ";
cin >> bar.nameFirst;
cout << "Type in that student's last name: ";
cin >> bar.nameLast;
bar.nameFull = bar.nameFirst + " " + bar.nameLast;
return bar;
}
void displayStudents(student listOfStudents[50], int studentHeadCount)
{
for (int i = 0; i < studentHeadCount; i++)
{
cout << listOfStudents[i].nameFull << endl;
cout << listOfStudents[i].getStudentNumber() << endl;
cout << "\n";
}
}
void generateGroups(int numberOfGroups, int maxStudents, int studentsPerGroup, int remainder, student theStudents[], int studentsBeenAssigned, ofstream studentGroups)
{
int k;
numberOfGroups = maxStudents / studentsPerGroup;
cout << numberOfGroups << endl;
srand(time(NULL));
studentGroups.open("studentGroups.txt");
while (studentsBeenAssigned < maxStudents && studentsBeenAssigned < maxStudents - remainder)
{
for (int j = 0; j < maxStudents - remainder; j++)
{
k = rand() % maxStudents;
cout << theStudents[k].nameFull << endl;
studentGroups << theStudents[k].nameFull << endl;
studentsBeenAssigned++;
if (studentsBeenAssigned == studentsPerGroup)
{
cout << "\n";
studentGroups << theStudents[k].nameFull << endl;
studentsBeenAssigned = 0;
}
}
}
if (remainder < studentsPerGroup && remainder > 0)
{
for (int l = 0; l < remainder; l++)
{
cout << theStudents[k].nameFull << endl;
}
}
}
void languageChoices()
{
cout << "Select your language from the following:\n";
cout << "a) English\n";
cout << "b) French\n";
cout << "\n";
}
void options()
{
cout << "Select what you want to do:\n";
cout << "1) Exit application\n";
cout << "2) Enter a Student\n";
cout << "3) Display Students\n";
cout << "4) Display Groups\n";
cout << "5) Output groups as text file\n";
cout << "6) Reset number of students in class\n";
cout << "7) Reset number of students per group\n";
cout << "\n";
}
int main()
{
char selectedLanguage;
languageChoices();
cin >> selectedLanguage;
switch (selectedLanguage)
{
case 'a':
{
student allStudents[50]; // Having 50 students alone is ridiculous!
bool endProg = 0;
int maxStudents;
int studentsPerGroup;
int optionSelect;
int studentHeadCount = 0;
int remainder = 0;
int numberOfGroups = 0;
int studentsBeenAssigned = 0;
ofstream studentGroups;
cout << "GroupPicker 1.0\n";
cout << "Note: This version of the program is intended for purposes of education only, "
<< "specifically for teacher use in a classroom.\n\n";
cout << "How many students are in the class?\n" << "(Note: You cannot have more than 50 in this program)\n";
cin >> maxStudents;
if (maxStudents > 50)
{
cerr << "Too many students!\n" << "Exiting program...\n";
system("PAUSE");
exit(1);
}
if (maxStudents >= 35 && maxStudents <= 50)
{
cout << maxStudents << " students? You are a pro!\n";
}
cout << "How many students per group?\n";
cin >> studentsPerGroup;
if (studentsPerGroup >= maxStudents || studentsPerGroup <= 1)
{
cerr << "You're kidding, right?\n" << "Exiting program...\n";
system("PAUSE");
exit(1);
}
while (endProg == 0) {
options();
cin >> optionSelect;
switch (optionSelect) {
case 1:
endProg = 1;
break;
case 2:
{
if (studentHeadCount == maxStudents)
{
cerr << "You can't enter more than " << maxStudents << " students\n";
}
else
{
allStudents[studentHeadCount] = typeName();
allStudents[studentHeadCount].setStudentNumber(studentHeadCount);
cout << "Student (" << allStudents[studentHeadCount].nameFull << ") entered.\n";
cout << "\n";
studentHeadCount++;
}
break;
}
case 3:
cout << "Current list of students:\n\n";
displayStudents(allStudents, studentHeadCount);
break;
case 4:
{
if (studentHeadCount < studentsPerGroup || studentHeadCount < maxStudents)
{
cerr << "Invalid group parameters.\n" << "Returning to main menu...\n\n";
break;
}
else
{
cout << "Here are the groups:\n\n";
studentGroups.open("studentGroups.txt");
generateGroups(numberOfGroups, maxStudents, studentsPerGroup, remainder, allStudents, studentsBeenAssigned, studentGroups);
}
break;
}
case 5:
{
cout << "Saving groups to file...\n";
studentGroups.close();
break;
}
case 6:
{
cout << "How many students are in the class?\n" << "(Note: You cannot have more than 50 in this program)\n";
cin >> maxStudents;
if (maxStudents > 50)
{
cerr << "Too many students!\n" << "Try again...\n";
}
if (maxStudents >= 35 && maxStudents <= 50)
{
cout << maxStudents << " students? You are a pro!\n";
}
break;
}
case 7:
{
cout << "How many students per group?\n";
cin >> studentsPerGroup;
if (studentsPerGroup >= maxStudents || studentsPerGroup <= 1)
{
cerr << "You're kidding, right?\n" << "Try again...\n";
}
break;
}
}
}
break;
}
case 'b':
{
mainInFrench();
}
}
}
When I compile, I get the following errors:
error C2248: 'std::basic_ofstream<_Elem,_Traits>::basic_ofstream' : cannot access private member declared in class 'std::basic_ofstream<_Elem,_Traits>'
IntelliSense: "std::basic_ofstream<_Elem, _Traits>::basic_ofstream(const std::basic_ofstream<_Elem, _Traits>::_Myt &_Right) [with _Elem=char, _Traits=std::char_traits<char>]" (declared at line 1034 of "C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\fstream") is inaccessible
How do I resolve these issues (Both on Line 181) and make sure that no number is generated more than once?
std::ofstream cannot be copied. It looks like you need a reference:
void generateGroups(int numberOfGroups, int maxStudents,
int studentsPerGroup, int remainder,
student theStudents[], int studentsBeenAssigned,
ofstream& studentGroups)
{// ^
You are passing stream by value to generateGroups() function which results in copy constructor call. Standard streams cannot be copied.
As for the second part of your question, you cannot expect PRNG to always return unique numbers, let alone when you limit the range with modulus. You'll need to write a bit more code which would check if the number you've got was already generated before, or in other words that some student has already been assigned.
The seed used by rand() won't repeat until 2^32 cycles or more on most implementations, but (seed>>xx)%n will repeat at least every n cycles and more frequently if n is not relatively prime to the numbers used by the linear congruential generator used by rand().
You could make an array of n bytes and use them to track if there's been a duplicate.
I am doing an assignment where I am supposed to write a program to test the user's math skills. Here's the code i have right now:
using namespace std;
void addition()
{
int Value, Value2, Answer;
bool gotAnswer;
for (int i=1; i<=10; i++)
{
Value = 1 + (rand()%10);
Value2 = 1 + (rand()%10);
gotAnswer = false;
cout << Value << " + " << Value2 << " = ";
for (int a=1; (a<=3 && gotAnswer==false); a++)
{
cin >> Answer;
if (Answer==(Value+Value2))
{
cout << "CORRECT" << endl << endl;
gotAnswer = true;
}
else
{
cout << "WRONG Try again." << endl;
if (a==3)
{
cout << "You have missed 3 times. The answer is " << Value+Value2 << "." << endl << endl;
}
}
}
}
}
void substraction()
{
int Value, Value2, Answer;
bool gotAnswer;
for (int i=1; i<=10; i++)
{
Value = 1 + (rand()%10);
Value2 = 1 + (rand()%10);
gotAnswer = false;
cout << Value << " - " << Value2 << " = ";
for (int a=1; (a<=3 && gotAnswer==false); a++)
{
cin >> Answer;
if (Answer==(Value-Value2))
{
cout << "CORRECT" << endl << endl;
gotAnswer = true;
}
else
{
cout << "WRONG Try again." << endl;
if (a==3)
{
cout << "You have missed 3 times. The answer is " << Value-Value2 << "." << endl << endl;
}
}
}
}
}
void Multiplication()
{
int Value, Value2, Answer;
bool gotAnswer;
for (int i=1; i<=10; i++)
{
Value = 1 + (rand()%10);
Value2 = 1 + (rand()%10);
gotAnswer = false;
cout << Value << " x " << Value2 << " = ";
for (int a=1; (a<=3 && gotAnswer==false); a++)
{
cin >> Answer;
if (Answer==(Value*Value2))
{
cout << "CORRECT" << endl << endl;
gotAnswer = true;
}
else
{
cout << "WRONG Try again." << endl;
if (a==3)
{
cout << "You have missed 3 times. The answer is " << Value*Value2 << "." << endl << endl;
}
}
}
}
}
int main()
{
int number;
cout << "Enter the number for the problem type desired:"<<endl;
cout << " 1. Addition"<<endl;
cout << " 2. Subtraction"<<endl;
cout << " 3. Multiplication"<<endl;
cin >> number;
if (number == 1)
{
addition();
}
else if(number == 2)
{
substraction();
}
else if (number ==3)
{
Multiplication();
}
}
The program runs fine. However, there should be a score component where the user gets 10 points on the first try, 5 point on second try, and 0 on third try/wrong. I have no idea how to blend the score component in and the display at the end of 10 questions. Hints please?
All thanks in advance.
You should keep a score variable in each of your functions, add to the score variable as necessary and then return the score variable.
So those function are no longer going to be voids, they'll be ints. You can then get the score at the end and print it out.
I'm not going to write any code for you, since it is for an assignment :P