I am having issues with trying to figure out how to declare and call my functions. I had this working without using a class, but my assignment calls for a class, romanType, to be used. Can someone please help me figure out my issues?
Here are my errors:
Severity Code Description Project File Line Suppression State
Error (active) E0020 identifier "getRoman" is undefined Garant_Project9 C:\Users\Andre\OneDrive\Desktop\C++\Garant_Project9\Garant_Project9\Garant_Project9.cpp 119
Severity Code Description Project File Line Suppression State
Warning C6001 Using uninitialized memory 'sum'. Garant_Project9 C:\Users\Andre\OneDrive\Desktop\C++\Garant_Project9\Garant_Project9\Garant_Project9.cpp 81
Severity Code Description Project File Line Suppression State
Error C3861 'getRoman': identifier not found Garant_Project9 C:\Users\Andre\OneDrive\Desktop\C++\Garant_Project9\Garant_Project9\Garant_Project9.cpp 119
Here is my code:
#include<iostream>
#include<iomanip>
#include<iostream>
#include<iomanip>
#include<string>
using namespace std;
class romanType
{
public:
void printValue(char numeral[15]);
void Menu();
void getRoman();
private:
};
void romanType::printValue(char numeral[15])
{
int RomanValues[7] = { 1000,500,100,50,10,5,1 };
char Roman[7] = { 'M','D','C','L','X','V','I' };
int number = 0, ck[15] = { 0 }, len = 0;
bool ok = false;
for (int x = 0; x < 15; x++)
if (numeral[x] != '\0')
len++; //Finding out how long the input is
else
x = 15;
for (int x = 0; x < len; x++)
{
ok = false;
for (int y = 0; y < 7; y++)
{
if (numeral[x] == Roman[y])
ok = true; // Making sure the input is all Roman numerals
}
if (!ok)
{
cout << "Sorry, the '" << numeral[x] << "' is NOT a Roman Numeral." << endl << "Try
again.." << endl << endl;
return;
}
}
for (int x = 0; x < len; x++)
{
for (int y = 0; y < 7; y++)
{
if (numeral[x] == Roman[y])
{
ck[x] = RomanValues[y];// Fill array ck with decimal value of inputted Roman #
}
}
}
for (int x = 0; x < len; x++)
{
if ((ck[x + 1] > ck[x]))
{
number += (ck[x + 1] - ck[x]); // If number is lower than next number, subtract
// So IV is 4 and VI is 6
x++;
}
else
number += ck[x]; // else add
}
cout << number << endl << endl;
}
void romanType::Menu()
{
cout << "\tRoman Numeral Values" << endl << endl;
cout << "\t\tM = 1000" << endl;
cout << "\t\tD = 500" << endl;
cout << "\t\tC = 100" << endl;
cout << "\t\tL = 50" << endl;
cout << "\t\tX = 10" << endl;
cout << "\t\tV = 5" << endl;
cout << "\t\tI = 1" << endl;
}
void romanType::getRoman()
{
char numeral[15];
do
{
Menu();
cout << endl << "Please enter a Roman Numeral.\n(Enter 'Q', to end program)" << endl;
cin >> numeral;
for (int x = 0; x < 15; x++)
numeral[x] = toupper(numeral[x]);
if (numeral[0] == 'Q')
cout << "Program terminating!" << endl;
else
printValue(numeral);
} while (numeral[0] != 'Q');
}
int main()
{
getRoman();
return 0;
}
Related
I'm very new and attempting to create a deck that will be filled at random from cardpool, permterm will also be filled with the same cards. This will give you duplicates of some cards and singles of others, as intended. Then you can use a card or draw a card and it will sort hand() and deckArr() correctly.
The problem comes when you draw and use a card back to back. When a card is in your hand, the corresponding trigger in cardActivation will turn to 1. (Meaning it's in your hand and cannot be drawn again) So if you draw, it handles the activation for that one card and shifts the deck accordingly. If you use the card, it is the opposite. It will turn the trigger to 0 (Meaning its available to draw and inside the deck) and shift the hand accordingly.
It seems that it randomly decides not to run the if statement within draw() [if (cardActivation[x] == 0 && hand[handSize-1] == permterm[x])] and will not trigger the activation, thus throwing off the rest of the program, but the next iteration of the loop correctly runs the if statement. When I get to the end of all the testing the activation is off by 1 because of the one time it missed the if statement. (There should be nine 1s instead of eight.)
I've fiddled and changed my code 100000 times but can't figure out why randomly it decided to skip the if statement, as far as I can tell it should still be true.
I've left my notes and tests within my code so hopefully it can help others understand my thinking process, sorry it's such a mess. It was MUCH cleaner before I started bug fixing. The HP bar information can be ignored, it's apart of something else. Hopefully I explained well enough for some help, I'm losing my mind. Thank you in advance!
#include <iostream>
#include <string>
#include <ctime>
using namespace std;
class healthBar
{
private:
int hp;
public:
healthBar()
{
hp = 100;
}
int damage(int dmg)
{
hp-=dmg;
if (hp == 0)
{
cout << "Youve Died " << endl;
resetHP();
return 0;
}
else
{
printHP();
return 1;
}
}
void heal(int regen)
{
hp+=regen;
}
void printHP()
{
cout << "HP is: " << hp << endl;
}
void resetHP()
{
hp = 100;
}
};
class myDeck
{
private:
int deckArr[30];
int tempDeck[30];
int permterm[30];
int hand[10] = {0,0,0,0,0,0,0,0,0,0};
int handSize = 0;
int cardPool[25] = {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};
int cardActivation[30];
public:
myDeck()
{
//Attaches Deck
int x;
srand(time(0));
for (int y = 0; y < 30; y++)
{
x = rand()%25;
deckArr[y] = cardPool[x];
cout << deckArr[y] << endl;
}
cout << "Permterm:" << endl;
for (int z = 0; z < 30; z++)
{
permterm[z] = deckArr[z];
cardActivation[z] = 0;
cout << permterm[z] << endl;
}
/*If you wanted duplicates of cards but
also be able to reject if that specific card has
been drawn already, you'd need an
activation array lined up with the permterm. 0=Not Active (Dont Skip) 1= In Hand (Skip)
It will need to search for the number.*/
}
void shuffle()
{
int z = 0;
for (int x = 0; x<30; x++)
{
tempDeck[x] = deckArr[x];
}
srand(time(0));
int y;
for (int x = 0; x<30; x++)
{
y = rand()%30;
if (y != z)
{
deckArr[x] = tempDeck[y];
}
z = y;
}
cout << "\nShuffled!" << endl;
}
void drawHand()
{
handSize=7;
for (int x = 0; x < 7; x++)
{
hand[x] = deckArr[x];
}
for (int x = 0; x<30; x++)
{
deckArr[x] = deckArr[x+7];
}
for (int x = 23; x < 30; x++)
{
deckArr[x] = 0;
}
//Switch On and Search
for (int y = 0; y < handSize; y++)
{
for (int x = 0; x < 30; x++)
{
if (hand[y] == permterm[x] && cardActivation[x] == 0)
{
cardActivation[x] = 1;
break;
}
}
}
cout << "\nHand Drawn!" << endl;
}
void viewDeck()
{
for (int x = 0; x<30; x++)
{
cout << permterm[x] << " " << cardActivation[x] << endl;
}
}
void viewHand()
{
for (int x = 0; x<handSize; x++)
{
cout << hand[x] << endl;
}
}
void resetDeck()
{
int resetCard;
int handTemp = handSize;
for (int y = 0; y < 30-handTemp; y++)
{
for (int x = 0; x<30;x++)
{
if (cardActivation[x] != 1)
{
deckArr[y] = permterm[x];
break;
}
}
}
cout << "Reset!"<< endl;
}
void draw()
{
if (handSize < 10)
{
cout << "\nDrawing..." << endl;
hand[handSize] = deckArr[0];
cout << "You drew: " << deckArr[0] << endl;
handSize++;
for (int x = 0; x < 30; x++)
{
deckArr[x] = deckArr[x+1];
}
deckArr[29] = 0;
//Activation
for (int x = 0; x < 30; x++)
{
if (cardActivation[x] == 0 && hand[handSize-1] == permterm[x])
{
cardActivation[x] = 1;
viewDeck();
break;
}
}
}
else
{
cout << "\nYour hand is full!" << endl;
}
//Test to see if deck needs to be reset
int sum = 0;
for (int x = 0; x < 30; x++)
{
sum+=deckArr[x];
}
//Checks to see if sending to resetDeck.
if (sum == 0)
{
viewDeck();
resetDeck();
}
}
void useCard()
{
int input;
cout << "\nWhich card would you like to use?" << endl;
for (int y = 0; y < handSize; y++)
{
cout << y+1 << ". " << hand[y] << endl;
}
cin >> input;
cout << "\nYou used: " << hand[input-1] << endl;
//Deactivate
for (int x = 0; x < 30; x++)
{
if (cardActivation[x] == 1 && hand[input-1] == permterm[x])
{
cardActivation[x] = 0;
break;
}
}
viewDeck();
for (int y = input-1; y<handSize; y++)
{
if (y+1 == 10)
{
hand[y] = 0;
}
else
hand[y] = hand[y+1];
}
handSize--;
}
};
int main()
{
/*Objective: Create a game where you draw from a deck of cards, to have 7 cards in hand.*/
healthBar health;
myDeck deck;
deck.viewDeck();
cout << "\nShuffling..." << endl;
deck.shuffle();
deck.viewDeck();
cout << "\nDrawing Hand..." << endl;
deck.drawHand();
deck.viewHand();
cout << "\nIs the Deck missing 7 cards?" << endl;
deck.viewDeck();
cout << "Can you draw? Hand:" << endl;
deck.draw();
deck.viewHand();
cout << "\nIs the deck missing 8 cards?" << endl; deck.viewDeck();
cout << "\nCan you draw until hand is full?" << endl;
deck.draw();
deck.viewHand();
deck.draw();
deck.viewHand();
deck.draw();
deck.viewHand();
cout << "\nCan you use a card?" << endl;
deck.useCard();
deck.draw();
deck.useCard();
deck.draw();
deck.useCard();
deck.draw();
deck.useCard();
for (int x = 0; x <10; x++)
{
deck.draw();
deck.useCard();
}
cout << "\nDoes resetting exclude your hand?" << endl;
cout << "Hand: " << endl;
deck.viewHand();
cout << "Deck: " << endl;
deck.viewDeck();```
My program is working as I want it to work (It is on Bosnian language so the cout of the program is not that important). The program is about reading some text from file and then couting what I want to
The only problem I have with this program is that for some reason it is showing the two 0 0 at the end of the text file even though I do not have it in my text file
Here is the code:
#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>
using namespace std;
struct proizvod
{
char naziv[100];
char proizvodac[100];
int cijena = 0;
int kolicina = 0;
};
bool poredjenje(proizvod a, proizvod b)
{
if (a.cijena != b.cijena )
return a.cijena > b.cijena;
}
void sortiranje(proizvod a[], int n)
{
sort(a, a+n, poredjenje);
}
int main()
{
ifstream datoteka;
datoteka.open("proizvodi.txt.txt");
int brojStvari = 0;
int sumaProizvoda = 0;
int ukupnaVrijednost = 0;
char* spisakProizvoda[100];
int brojFIAT = 0;
int spisakCijena = 0;
proizvod automobili[100];
if (datoteka.fail())
{
cout << "Ne postojeca datoteka";
exit(1);
}
while (datoteka.good() && !datoteka.eof())
{
datoteka >> automobili[brojStvari].naziv >> automobili[brojStvari].proizvodac >> automobili[brojStvari].cijena >> automobili[brojStvari].kolicina;
++brojStvari;
++sumaProizvoda;
}
for (int i = 0; i < brojStvari; i++)
{
cout << automobili[i].naziv << " " << automobili[i].proizvodac << " " << automobili[i].cijena << " " << automobili[i].kolicina << endl;
}
for (int i = 0; i < brojStvari; i++)
{
ukupnaVrijednost += automobili[i].cijena;
if (automobili[i].kolicina == 0)
{
spisakProizvoda[i] = automobili[i].proizvodac;
}
else if (automobili[i].proizvodac == "FIAT")
{
brojFIAT++;
}
}
char pomocna[100];
cout << endl;
cout << "Ukupan broj proizvoda u datoteci: " << sumaProizvoda << endl;
cout << "Ukupan vrijednost proizvoda u datoteci: " << ukupnaVrijednost << endl;
cout << "Spisak automobila sa cijenom 0 su: ";
for (int i = 0; i < brojStvari; i++)
{
if (!spisakProizvoda[i])
{
cout << "Ne postoje ti proizvodi " << endl;
break;
}
else
cout << spisakProizvoda[i] << endl;
}
cout << "Broj prozivoda koji proizvodi FIAT: " << brojFIAT << endl;
cout << "Sortirani proizvodi prema cijeni: " << endl;
sortiranje(automobili, brojStvari);
for (int i = 0; i < brojStvari; i++)
{
cout << automobili[i].proizvodac << endl;
}
return 0;
}
And here is the cout
Golf Volskwagen 5000 5
AudiRS5 Audi 50000 3
0 0
Ukupan broj proizvoda u datoteci: 3
Ukupan vrijednost proizvoda u datoteci: 55000
Spisak automobila sa cijenom 0 su: Ne postoje ti proizvodi
Broj prozivoda koji proizvodi FIAT: 0
Sortirani proizvodi prema cijeni:
Audi
Volskwagen
Can anybody tell me what is the problem ?
P.S : Sorry if you do not understand the program itself I apologize sincerely
You are seeing the extra 0 0 at end most likely due to an extra empty line at end of proizvodi.txt.txt file. This happens because the new line is also accepted as input but since there are no entries, the entries in struct proizvod retain default 0 values for cijena and kolicina which gets printed out as 0 0.
Update your main() code to reading file like follow and it will work as expected:
while (datoteka >> automobili[brojStvari].naziv >>
automobili[brojStvari].proizvodac >> automobili[brojStvari].cijena >>
automobili[brojStvari].kolicina) {
++brojStvari;
++sumaProizvoda;
}
I am working on this university project and I have 2-3 issues. I want to get them done 1 by 1.
My first question is about one of my getter/setter functions.
Here are the class's attributes:
I got getters and setters for each attribute of the class, and a view function that shows the attributes of a class object
class Game{
private:
string gameName;
float gamePrice;
int gameNoPlatforms;
string *gamePlatforms;
int gameNoSitesRating;
int *gameRatings;
int gameSales[19];
string gameLaunchers[5];
}
Those 2 are my get/set function for the atribute * string gamePlatforms:
void setGamePlatforms(string *newGamePlatforms, int newGameNoPlatforms)
{
if (this->gamePlatforms != NULL)
{
delete[] this->gamePlatforms;
}
if (newGameNoPlatforms > 0 )
{
this->gameNoPlatforms = newGameNoPlatforms;
this->gamePlatforms = new string[this->gameNoPlatforms];
for (int i = 0; i < this->gameNoPlatforms; i++)
{
this->gamePlatforms[i] = newGamePlatforms[i];
}
}
else cout << "The number of new game platforms can't be negative. "<< endl;
}
string *getGamePlatforms()
{
return this->gamePlatforms;
}
And this is the input that i tried to test on:
int main(){
Game g1;
int noPlatforms = 10;
string*model = new string[noPlatforms];
for (int i = 0; i < noPlatforms; i++)
{
model[i] = "Platform " + to_string(i+10);
}
g1.setGamePlatforms(model, noPlatforms); //-> not working
g1.setGamePlatforms(model, noPlatforms);
cout << g1.getGamePlatforms();
return 0;
}
And for me it is returning a weird value. I think it is an address. What have I done wrong?
Edit: the entire class:
class Game{
private: string gameName;
float gamePrice;
int gameNoPlatforms;
string *gamePlatforms;
int gameNoSitesRating;
int *gameRatings;
int gameSales[19];
string gameLaunchers[5];
public:
// Constructor1 fara parametrii pt game
Game():gameReleaseYear(2000)
{
this->gameName = "Counter-Strike";
this->gamePrice = 20;
this->gameNoPlatforms = 10;
this->gamePlatforms = new string[this->gameNoPlatforms];
for (int i = 0; i < this->gameNoPlatforms; i++)
{
this->gamePlatforms[i] = "Platform" + to_string(i+1);
}
this->gameNoSitesRating = 5;
this->gameRatings = new int[this->gameNoSitesRating];
for (int i = 0; i < this->gameNoSitesRating; i++)
this->gameRatings[i] = i + 1;
for (int i = 1; i < 19; i++)
{
this->gameSales[i] = i + 2;
}
for (int i = 1; i < 5; i++)
{
this->gameLaunchers[i] = "Launcher" + to_string(i);
}
this->noGames++; //incrementare nr games
}
// Constructor cu parametrii pt game
Game(string gameNameP, float gamePriceP, int gameNoPlatformsP, string *gamePlatformsP, int gameNoSitesRatingP, int *gameRatingsP, int gameSalesP[19], string gameLaunchersP[5]) :gameReleaseYear(2005)
{
this->gameName = gameNameP;
this->gamePrice = gamePriceP;
this->gameNoPlatforms = gameNoPlatformsP;
this->gamePlatforms = new string[this->gameNoPlatforms];
for (int i = 0; i < this->gameNoPlatforms; i++)
{
gamePlatforms[i] = gamePlatformsP[i];
}
this->gameNoSitesRating = gameNoSitesRatingP;
this->gameRatings = new int[this->gameNoSitesRating];
for (int i = 0; i < this->gameNoSitesRating; i++)
{
gameRatings[i] = gameRatingsP[i];
}
for (int i = 0; i < 19; i++)
{
gameSales[i] = gameSalesP[i];
}
for (int i = 0; i < 5; i++)
{
gameLaunchers[i] = gameLaunchersP[i];
}
}
// Destructor pt game
~Game()
{
if (this->gamePlatforms != NULL)
delete[] this->gamePlatforms;
if (this->gameRatings != NULL)
delete[] this->gameRatings;
noGames--; // decrementare nr games
}
// Functie de afisare pentru game
void view()
{
cout << "For the game: " <<' '<< this->gameName << " we have the following details: " << endl;
cout << endl;
cout << "The release year for the game was: " << this->gameReleaseYear << endl;
cout << endl;
cout << "The game is sold at a full price of: " << this->gamePrice << " euroes" << endl;
cout << endl;
cout << "The number of sites that are rating this game is: " << this -> gameNoSitesRating << " and the ratings are: " << endl;
cout << endl;
for (int i = 0; i < this->gameNoSitesRating; i++)
{ if(this->gameRatings[i]+i >10)
cout << "The no " << i + 1 << " site is rating the game as " << 10 << " stars out of 10 " << endl;
else
cout << "The no " << i+1 << " site is rating the game as " << this->gameRatings[i] << " stars out of 10 " << endl;
}
cout << endl;
cout << "The sales of the game from the release year since now are: " << endl;
for (int i = 1; i < 19; i++)
{
cout << "For the year " << i << " the sales estimate at " << gameSales[i] << " millions" << endl;
}
cout << endl;
cout << "The launchers that support the game are: " << endl;
for (int i = 1; i < 5; i++)
{
cout << gameLaunchers[i] << ' ' << endl;
}
cout << "The game is currently running on " << this->gameNoPlatforms << " number of platforms, and those platforms are: " << endl;
cout << endl;
for (int i = 0; i < this->gameNoPlatforms; i++)
{
cout << this->gamePlatforms[i] << endl;
}
}
// functiile accesor getters
string getGameName()
{
return this->gameName;
}
float getGamePrice()
{
return this->gamePrice;
}
int getGameNoPlatforms()
{
return this->gameNoPlatforms;
}
int getNoSitesRating()
{
return this->gameNoSitesRating;
}
string *getGamePlatforms()
{
return this->gamePlatforms;
}
int *getGameRatings()
{
return this->gameRatings;
}
string *getGameLaunchers()
{
return this->gameLaunchers;
}
//functiile accesor setters
void setGameName(string newGameName) //testat pt input valid si invalid
{ if(newGameName != gameName)
this->gameName = newGameName;
else cout << "This is the actual name of the game, no modify is required.";
}
void setGamePrice(float newPrice) //testat pt input valid si invalid;
{ if(newPrice>0)
this->gamePrice = newPrice;
else cout << "The price can't be negative." << endl;
}
void setGameNoPlatforms(int newNoPlatforms) //testat pentru input valid, input mai mare ca 5 si negativ
{ if(newNoPlatforms>0 && newNoPlatforms <5)
this->gameNoPlatforms = newNoPlatforms;
else cout << "The number of platforms can't be negative and must be less than 5, since this is the maximum number of existing platforms." << endl;
}
void setGameNoSitesRating(int newNoSites) //testat pentru input valid, input negativ, input mai mare decat 15
{ if(newNoSites>0 && newNoSites<15)
this->gameNoSitesRating = newNoSites;
else cout << "The number of sites can't be negative nor greater than 15 since this is the maximum number of sites that our game is rated on." << endl;
}
void setGamePlatforms(string *newGamePlatforms, int newGameNoPlatforms) //testat pt input valid, returneaza adresa
{
if (this->gamePlatforms != NULL)
{
delete[] this->gamePlatforms;
}
if (newGameNoPlatforms > 0 ){
this->gameNoPlatforms = newGameNoPlatforms;
this->gamePlatforms = new string[this->gameNoPlatforms];
for (int i = 0; i < this->gameNoPlatforms; i++)
{
this->gamePlatforms[i] = newGamePlatforms[i];
}
}
else cout << "The number of new game platforms can't be negative. "<< endl;
}
void setGameRatings(int *newGameRatings, int newNoRatings) //testat pt valori valide, testat pt neNoRatings negativ;
{
if (newNoRatings < 0)
cout << "The new number of sites that are rating the game can't be negative";
else {
if (this->gameRatings != NULL)
{
delete[] this->gameRatings;
}
this->gameNoSitesRating = newNoRatings;
this->gameRatings = new int[this->gameNoSitesRating];
for (int i = 0; i < this->gameNoSitesRating; i++)
{
this->gameRatings[i] = newGameRatings[i];
}
}
}
void setGamesSales(int *newSales)
{
if (newSales != NULL)
{
for (int i = 0; i < 19; i++)
{
gameSales[i] = newSales[i];
}
}
else cout << "The sales can't be null." << endl;
}
void setLaucnhers(string *newLaunchers)
{
if (newLaunchers != NULL)
{
for (int i = 0; i < 5; i++)
{
gameLaunchers[i] = newLaunchers[i];
}
}
else cout << "The name of the new launchers can't be null." << endl;
}
//variabile static/const
const int gameReleaseYear;
static int noGames;
};
int Game::noGames = 0;
Try this:
int Game::getNumberOfPlatforms() const
{
return gameNoPlatforms;
}
And print them:
for(int i = 0; i < g1.getNumberOfPlatforms(); ++i)
cout << g1.getGamePlatforms()[i] << endl;
I am using C++ and want to do a 2-dimensional array. 10 rows and 3 columns. First column is(1 through 10). For Second column, user enters his/her choice of a number from (1-10) resulting in a times table displaying the results as follows: In this example the user's choice is '4':
1x4=4
2x4=8
3x4=12
4x4=16
5x4=20
6x4=24
7x4=28
8x4=32
9x4=36
10x4=40
I can't get the user's input to calculate correctly when using the for loop.
Well you can try this to get that output
#include<iostream>
using namespace std;
int main()
{
int n; //To take input
int table[10][3]; // Table
cout << "Input a number: ";
cin >> n;
// Generating Output
for (int i = 0; i < 10; i++)
{
table[i][0] = i + 1;
table[i][1] = n;
table[i][2] = table[i][0] * table[i][1];
}
for (int i = 0; i < 10; i++)
{
cout << table[i][0] << " * " << table[i][1] << " = " << table[i][2]<<endl;
}
return 0;
}
Output
SOLVED: Everything seems to be working now!! Here's the code:
#include <iostream>
#include<cstdlib>
#include<iomanip>
#include <ctime>
using namespace std;
void displayTable(int table[10][3]);
bool testMe(int testTable[10][3]);
void createTables(int testTable[10][3], int ansTable[10][3], int
usersChoice);
bool AllAnswersAreTested(bool tested[10]);
void gradeMe(int testTable[10][3], int ansTable[10][3]);
void displayMenu();
int main()
{
srand(time(NULL));
int userInput = 0;
int tableChoice = 0;
int myTable[10][3] = {0};
int testTable[10][3];
int ansTable[10][3];
bool tested = false;
do
{
displayMenu(); //Display the menu of choices
cin >> userInput;
cout << endl;
switch (userInput) //Validate menu choices 1-4
{
case 1: //Display a users choice of table
displayTable(myTable);
break;
case 2: //Test user on users choice of table
cout << "What times table test would you like to take? > ";
cin >> tableChoice;
createTables(testTable, ansTable, tableChoice);
tested = testMe(testTable);
if (tested)
{
gradeMe(testTable, ansTable);
}
break;
case 3: //Display a new table of the users choice
displayTable(myTable);
break;
case 4: //Quit program menu option
cout << "Program ending.\n";
return 0;
default: //Invalid entry
cout << "You entered an invalid item number. Please enter a number from 1 to 4.\n";
cout << endl;
}
} while (userInput != 4);
return 0;
}
void displayTable(int myTable[10][3])
{
int num; //initialize local variables
//Ask the user what times table they would like to review
cout << "What times table would you like to review?" << endl;;
cout << "Please enter a value from 1 to 12 > \n";
cout << "\n";
cin >> num;
cout << endl;
for (int i = 0; i < 10; i++)
{
myTable[i][0] = i + 1;
myTable[i][1] = num;
myTable[i][2] = myTable[i][0] * myTable[i][1];
}
for (int i = 0; i < 10; i++)
{
cout << setw(3)<< myTable[i][0] << " * " << myTable[i][1] << " = " << myTable[i][2] << endl;
}
cout << endl;
}
void createTables(int testTable[10][3], int ansTable[10][3], int usersChoice)
{
for (int i = 0; i < 10; i++)
{
testTable[i][0] = i + 1;
testTable[i][1] = usersChoice;
testTable[i][2] = 0;
ansTable[i][0] = i + 1;
ansTable[i][1] = usersChoice;
ansTable[i][2] = usersChoice * (i + 1);
}
}
bool testMe(int testTable[10][3])
{
bool tested[10] = { false, false, false, false, false,false, false, false, false, false };
while (!AllAnswersAreTested(tested))
{
int index = rand() % 10;
if (tested[index] == false)
{
int randomNum = testTable[index][0];
int tableChoice = testTable[index][1];
int answer;
cout << "What is " << randomNum << " X " << tableChoice << " = ";
cin >> answer;
testTable[index][2] = answer;
tested[index] = true;
}
}
return true;
}
bool AllAnswersAreTested(bool tested[10])
{
for (int i = 0; i < 10; i++)
{
if (tested[i] == false)
{
return false;
}
}
return true;
}
void gradeMe(int testTable[10][3], int ansTable[10][3])
{
int correctAnswers = 0;
for (int i = 0; i<10; i++)
{
if (testTable[i][2] == ansTable[i][2])
{
correctAnswers++;
}
}
int score = (correctAnswers * 10);
if (score == 100)
{
cout << "You passed the test! PERFECT SCORE!!" << endl;
cout << endl;
}
else if (score >= 70)
{
cout << "You passed the test. Your Score is: ";
cout << score;
cout << endl;
}
else if (score < 70)
{
cout << "You did not pass the test. Your Score is: ";
cout << score;
cout << endl;
}
}
//Display the menu function
void displayMenu()
{
cout << " Multiplication Tables" << endl;
cout << endl;
cout << " 1. Review MyTable" << endl;
cout << " 2. Test Me" << endl;
cout << " 3. Enter a New Multiplication Table (1-12)";
cout << " 4. Quit" << endl;
cout << " Enter a Menu Item > ";
}
#include <iostream>
using namespace std;
int main()
{
int a[100][100];
for(int i=1;i<10;i++){
for(int j=1;j<10;j++){
a[i][j] = (i)*(j);
cout<<a[i][j]<<" ";
}
cout<<endl;
}
return 0;
}
There is how the output looks like:
i have a problem on this code
the errors says ISO c++ forbids comparison between pointer and integer
here is the exercise problem
-the answers to a true false test are as follows TTFFT given a two dimensional answer array in which each row corresponds to the answers provided on one test, write a function that accepts the two dimensional array and the number of tests as parameters and returns a one dimensional array containing the grades for each test.(each question is worth 5 points so that the maxinum possible grade is 25)
any answers will help.
this is the code
#include <iostream>
using namespace std;
int numberofgrades(char [][5], int []);
int main()
{
char testanswers[5][5] = {0};
int testgrades[5] = {0};
int counting1, counting2, counting3;
counting1 = 1;
counting2 = 1;
counting3 = 1;
cout << "this program will record the testgrades you entered: "
<< endl;
for(int i = 0; i < 5; i++)
{
for(int k = 0; k < 5; k++)
{
cout << "enter answers for test no. " << counting1 << ": ";
cin >> testanswers[i][k];
}
counting1++;
}
numberofgrades(testanswers,testgrades);
cout << "testing..." << endl;
cout << endl;
for(int o = 0; o < 5; o++)
{
cout << "test no. " << counting2;
for(int l = 0; l < 5; l++)
{
cout << " " << testanswers[o][l];
}
counting2++;
cout << endl;
}
cout << "testing...." << endl;
cout << endl;
cout << "the total number of scores per test no. is: "
<< endl;
for(int t = 0; t < 5; t++)
{
cout << "test no. " << t << ": " << testgrades[t] << endl;
}
return 0;
}
int numberofgrades(char t1a [][5], int tg1[])
{
for(int j = 0; j < 5; j++)
{
for(int i = 0; i < 5; i++)
{
if(t1a[i] == 't') //this is the problem
{
tg1[j] = tg1[j] + 5;
}
else if(t1a[i] == 'T')
{
tg1[j] = tg1[j] + 5;
}
else if(t1a[i] == 'f')
{
tg1[j] = tg1[j] + 0;
}
else if(t1a[i] == 'F')
{
tg1[j] = tg1[j] + 0;
}
else{
cout << "invalid letter exiting the program"
<< endl;
return 0;
}
}
}
return tg1[5];
}