How can I display vector correct? [duplicate] - c++

This question already has answers here:
How to include array in vector [closed]
(2 answers)
Closed 9 years ago.
I have a little problem displaying my vector correct in C++.
It prints like this now:
Spiller navn: A
Score: 1
Spiller navn: A
Score: 2
Spiller navn: A
Score: 3
Spiller navn: B
Score: 1
Spiller navn: B
Score: 2
...
...and so on.
But i want it to print "Spiller" only once, and the "Score" multiple time, so it will look like this:
Spiller navn: A
Score:
1
2
3
Spiller navn: B
Score:
1
2
3
Here is my fill vector function:
void fyldVector(vector<Beregning>& nySpiller) {
string navn;
int score;
cout << "Indtast antal spillere: ";
int antal;
cin >> antal;
//nySpiller.reserve( nySpiller.size() + antal );
for (int i = 0; i < antal; i++) {
cout << "Indtast spiller navn: ";
cin >> navn;
for (int j = 0; j < 3; j++) {
cout << "Indtast score: ";
cin >> score;
Beregning nyBeregning(navn, score);
nySpiller.push_back(nyBeregning);
}
}
cout << endl;
}
And my print vector function:
void printVector(const vector<Beregning>& nySpiller) {
unsigned int size = nySpiller.size();
cout << nySpiller.size() << endl;
for (unsigned int i = 0; i < size; i++ ) {
cout << "Spiller navn: " << nySpiller[i].getNavn() << endl;
cout << "Score: " << nySpiller[i].getScore() << endl;
cout << endl;
}
}

As you did not say what does function getScore and what is its return type then I use the function as it is used in your example.
void printVector( const vector<Beregning> &nySpiller )
{
string navn;
for ( const Beregning &b : nySpiller )
{
if ( navn != b.getNavn() )
{
navn = b.getNavn();
cout << "\nSpiller navn: " << navn << endl;
cout << "Score: " << endl;
}
cout << b.getScore() << endl;
}
}
If your compiler does not support the range based for loop then you can write
void printVector( const vector<Beregning> &nySpiller )
{
string navn;
for ( vector<Beregning>::size_type i = 0; i < nySpiller.size(); i++ )
{
if ( navn != nySpiller[i].getNavn() )
{
navn = nySpiller[i].getNavn();
cout << "\nSpiller navn: " << navn << endl;
cout << "Score: " << endl;
}
cout << nySpiller[i].getScore() << endl;
}
}

Related

error: request for member ‘find’ in ‘sentence’, which is of non-class type ‘char*’ [duplicate]

This question already has answers here:
What is array to pointer decay?
(11 answers)
Array Size Member Function Compile Error
(3 answers)
Closed 15 days ago.
I get an error saying "'find' in ‘sentence’, which is of non-class type ‘char*’".
So the instruction was to enter a string and identify which in the string is a noun, pronoun, adjectives and a linking verb. Everything seems to work except the ".find" part. I really need help.
(btw, nvm the other unecessary things. I made the code shorter to put focus more on the problem)
#include <iostream>
#include <cstring>
#include <cctype>
#include <string>
using namespace std;
string student_id, first_name, last_name, middle_name, suffix;
char sentence[100][100];
char history1[100][100];
int h = 0, s= 0;
char noun[20][20] = {"Ervin", "Rafi", "Peco", "luis", "Edgar", "Benj", "Rias", "Aki", "Naruto", "Jhay", "Josh", "Jopay", "Sasuke", "Joshua", "Trump", "Benjo", "Alice", "Janelle", "Samantha", "Jairah"};
char pronoun[20][20] = {"he", "she", "i", "it", "you"};
char verb[20][20] = {"is", "was", "are", "as", "am"};
char adj[20][20] = {"big", "small", "racist", "fat", "Funny", "gay", "black", "white", "rainbow", "tiny", "smart", "bi", "pan", "stupid", "idiot", "buang", "retard", "gaymer", "special", "talented"};
void display_dictionary();
void open_checker();
bool hasElement(char sentence[], char elements[], int size);
int main()
{
int option;
cout << endl << "Account Menu" <<endl;
cout << "[1] Open checker" << endl;
cout << "[2] Display Dictionary" << endl;
cout << "Option: ";
cin >> option;
switch (option){
case 1: open_checker(); break;
case 2: display_dictionary(); break;
}
return 0;
}
void display_dictionary(){
cout << "List of Nouns: ( ";
for (int n = 0; n < 20; n++){
cout << noun[n] << " " ;
}
cout << ")" << endl << endl;
cout << "List of Pronouns: ( ";
for (int n = 0; n < 5; n++){
cout << pronoun[n] << " " ;
}
cout << ")" << endl << endl;
cout << "List of Linking Verbs: ( ";
for (int n = 0; n < 5; n++){
cout << verb[n] << " " ;
}
cout << ")" << endl << endl;
cout << "List of Adjectives: ( ";
for (int n = 0; n < 20; n++){
cout << adj[n] << " " ;
}
cout << ")" << endl << endl;
}
void open_checker(){
int option;
cout << "Enter Sentence: ";
cin.ignore();
cin.get(sentence[s], 100);
if (hasElement(sentence[s], noun[s], 20)){
cout << " Is a noun." << endl;
}
if (hasElement(sentence[s], adj[s], 20)){
cout << " Is an Adjectives." << endl;
}
if (hasElement(sentence[s], verb[s], 5)){
cout << " Is a Linking Verb." << endl;
}
if (hasElement(sentence[s], pronoun[s], 5)) {
cout << " Is a Linking Verb." << endl;
}
else {
cout << "Sentence does not have all required elements." << endl;
}
strcpy(history1[h], sentence[s]);
history1[h][h] = sentence[s][s];
h++;
s++;
cout << "Type 1 to go back: ";
cin >> option;
if (option == 1){
main();
}
}
bool hasElement(char sentence[], char elements[], int size){
for (int i = 0; i < size; i++) {
if (sentence.find(elements[i]) != string::npos) {
cout << elements[i];
return true;
}
}
return false;
}```

How to only print out highest/smallest array values in c++

Good day, I'm having difficulty on the last two parts of my program where it's supposed to only output players who got maximum/minimum scores, I need help on how to do it because I'm really confused. If it's also alright to provide some explanations I'd really appreciate it.
I tried this approach:
#include <iostream>
using namespace std;
int main() {
double lrgst, lrgst2, lrgst3;
int numbers[5];
lrgst = lrgst2 = lrgst3;
for (int i = 0; i < 5; i++) {
cin >> numbers[i];
}
for (int i = 0; i < 5; i++) {
if (numbers[i] > lrgst) {
lrgst3 = lrgst2;
lrgst2 = lrgst;
lrgst = numbers[i];
} else if (numbers[i] > lrgst2) {
lrgst3 = lrgst2;
lrgst2 = numbers[i];
} else if (numbers[i] > lrgst3) {
lrgst3 = numbers[i];
}
}
cout << "largest are: " << lrgst << " " << lrgst2 << " " << lrgst3;
}
this is my actual code:
#include <iostream>
using namespace std;
struct playerdata {
char name[50];
int age, score1, score2;
double average;
};
int main() {
int choice, i = 1, j = 1, z = 1, backtomain2;
char backtomain;
playerdata p1[10];
do {
for (int a = 0; a < 47; a++) {
cout << "=";
}
cout << "\n";
for (int b = 0; b < 22; b++) {
cout << " ";
if (b == 21) {
cout << "MENU \n";
}
}
for (int c = 0; c < 47; c++) {
ocut << "=";
}
cout << " "
"\n1. Add record\n"
"2. View players records\n"
"3. Compute for the average\n"
"4. Show the player(s) who gets the max average.\n"
"5. Show the player(s) who gets the min average.\n"
"6. Exit\n"
"Enter your choice:";
cin >> choice;
if (choice == 1) {
cout << "Add player data" << endl;
do {
cout << "Enter player " << i << " nickname:";
cin >> p1[i].name;
cout << "Enter player " << i << " age:";
cin >> p1[i].age;
cout << "Enter player " << i << " score 1:";
cin >> p1[i].score1;
cout << "Enter player " << i << " score 2:";
cin >> p1[i].score2;
cout << "Enter again? (Y/N)";
cin >> backtomain;
i++;
}
while (backtomain != 'N' && backtomain != 'n' && i < 7);
if (choice == 2) {
cout << "Player records" << endl;
cout << "Player nickname "
<< "Player age "
<< " player score 1"
<< "
player score 2\n ";
for (z = 1; z <= i - 1; z++) {
cout << p1[z].name << " " << p1[z].age << "" << p1[z].score1 << ""
<< p1[z].score2 << "\n";
}
cout << "Press 1 to go back to main menu\n";
cin >> backtomain;
}
if (choice == 3) {
cout << "Computing for average...\n";
for (int d = 1; d <= i - 1; d++) {
p1[d].average = (p1[d].score1 + p1[d].score2) / 2.0;
cout << "\n" << p1[d].average << "\n";
}
cout << "Press 1 to go back to main menu\n";
cin >> backtomain;
}
if (choice == 4) {
cout << "Player(s) who got the max average:\n";
cout << "\nPress 1 to go back to main menu";
cin >> backtomain;
}
if (choice == 5) {
cout << "player(s) who got the min average: \n";
cout << "Press 1 to go back to main menu";
cin >> backtomain;
}
}
while (choice != 6);
}
You can simply sort the array of players for that
int n = sizeof(p1)/ sizeof(p1[0]);
sort(p1, p1+n, compPlayer);
//larget at pl[0]
//smallest at pl[9]
where
bool compPlayer(playerdata p1, playerdata p2) {
return (p1.score1+p1.score2) > (p2.score1+p2.score2);
//use score incase average has not been calculated for all players yet
}

Finding the max in an array of Data-Structures

I have a text file of cars:
2014 Toyota Tacoma 115.12 1
2012 Honda CRV 85.10 0
2015 Ford Fusion 90.89 0
2013 GMC Yukon 110.43 0
2009 Dodge Neon 45.25 1
2011 Toyota Rav4 65.02 1
2012 Mazda CX5 86.75 1
2016 Subaru Outback 71.27 0
2015 Ford F150 190.83 1
2010 Toyota Corolla 50.36 1
I am trying to find the max which is the float but I am having trouble finding it as well as finding the rental cost of the cars. I have this so far but am still having trouble.
#include <iostream>
#include <fstream>
using namespace std;
struct car
{
int year;
char make[10];
char model[10];
float price;
int available;
} ;
void menu();
// Main Function
int main ()
{
// declare variables
int carAmount = 10;
int choice;
car carLib[carAmount];
char filename[10];
ifstream carInData;
float mostExpensive = 0;
int MostExpensiveIndex;
int count = 0;
int days;
int rentalCost = 0;
//prompt user for input file
cout << " Enter file name: ";
cin >> filename;
menu();
carInData.open(filename);
cin >> choice;
if(carInData.is_open());
{
// read list of names into array
for(cout; count < carAmount; count++){
carInData >> carLib[count].year >> carLib[count].make >> carLib[count].model >> carLib[count].price >> carLib[count].available;
switch (choice){
case 1:
if(carLib[count].available == 1)
cout << " Available ";
else
cout << " Unavailable ";
cout << carLib[count].year << " " << carLib[count].make << " " << carLib[count].model << " " << carLib[count].price << " " << "\n";
break;
case 2:
cout << " Enter car number and how many days " << "\n";
cout << " Days: ";
cin >> days;
cout << "\n" << "Car: ";
cin >> carLib[count].price;
rentalCost += days * count;
cout << " Rental Cost for " << days << " days is " << rentalCost;
break;
case 3:
MostExpensiveIndex = count;
for(size_t carIndex = 0; carIndex < count; ++carIndex){
if(carLib[carAmount].price <= mostExpensive) continue;
mostExpensive = carLib[carIndex].price;
MostExpensiveIndex = carIndex;
}
const car & carI = carLib[MostExpensiveIndex];
cout << " Most Expensive car is: " << MostExpensiveIndex << " " << carI.year << " " << carI.make << " " << carI.model << " " << carI.price << "\n";
break;
}
}
}
return 0;
}
void menu(){
cout << " 1 - Show Cars\n";
cout << " 2 - Rental Cost\n";
cout << " 3 - Most Expensive Car\n";
}
My output is displaying all my cars instead of the maximum one. This is the screenshot of the output
Replace:
if(carLib[carAmount].price <= mostExpensive) continue;
with
if(carLib[carIndex].price <= mostExpensive) continue;
Correct code is:
#include <iostream>
#include <fstream>
using namespace std;
struct car {
int year;
char make[10];
char model[10];
float price;
int available;
} ;
void menu();
// Main Function
int main ()
{
// declare variables
int carAmount = 10;
int choice;
car carLib[carAmount];
char filename[10];
ifstream carInData;
float mostExpensive = 0;
int MostExpensiveIndex;
int count = 0;
int days;
float rentalCost = 0;
//prompt user for input file
cout << " Enter file name: ";
cin >> filename;
menu();
carInData.open(filename);
cin >> choice;
if (carInData.is_open()) {
// read list of names into array
for (; count < carAmount; count++) {
carInData >> carLib[count].year >> carLib[count].make >> carLib[count].model >> carLib[count].price >> carLib[count].available;
}
}
switch (choice) {
case 1:
for (int carIndex = 0; carIndex < carAmount; ++carIndex){
if (carLib[carIndex].available == 1)
cout << " Available ";
else
cout << " Unavailable ";
cout << carLib[carIndex].year << " " << carLib[carIndex].make << " " << carLib[carIndex].model << " " << carLib[carIndex].price << " " << "\n";
}
break;
case 2:
cout << " Enter car number and how many days " << "\n";
cin >> count;
cout << " Days: ";
cin >> days;
cout << "\n" << "Car: ";
rentalCost = days * carLib[count].price;
cout << " Rental Cost for " << days << " days is " << rentalCost << endl;
break;
case 3:
MostExpensiveIndex = 0;
for (size_t carIndex = 0; carIndex < carAmount; ++carIndex) {
if (carLib[carIndex].price <= mostExpensive) continue;
mostExpensive = carLib[carIndex].price;
MostExpensiveIndex = carIndex;
}
const car & carI = carLib[MostExpensiveIndex];
cout << " Most Expensive car is: " << MostExpensiveIndex << " " << carI.year << " " << carI.make << " " << carI.model << " " << carI.price << "\n";
break;
}
return 0;
}
void menu()
{
cout << " 1 - Show Cars\n";
cout << " 2 - Rental Cost\n";
cout << " 3 - Most Expensive Car\n";
}

How to put variables into an array

I am making a Basketball Scoreboard that can determine the winner of the game in each quarter and the the main game.
How can i store the values of my variables in an array?
I want to put the values of "Q1teamOne, Q2teamOne, Q3teamOne, Q4teamOne" in an array and also the values of "Q1teamTwo, Q2teamTwo, Q3teamTwo ,Q4teamTwo" or make them elements of an array.
#include <iostream>
#include <string>
using namespace std;
int main()
{
string Team1;
string Team2;
double OTscore1;
double OTscore2;
int Q1teamOne, Q2teamOne, Q3teamOne, Q4teamOne;
int Q1teamTwo, Q2teamTwo, Q3teamTwo ,Q4teamTwo;
int Q2TeamOneTotal, Q3TeamOneTotal, Q4TeamOneTotal;
int Q2TeamTwoTotal, Q3TeamTwoTotal, Q4TeamTwoTotal;
double teamOneScore[4];
double teamTwoScore[4];
int index;
double sumOne, sumTwo;
cout << "BASKETBALL SCOREBOARD:\n" << endl;
cout << "Enter Team 1 name: ";
getline (cin, Team1);
cout << "Enter Team 2 name: ";
getline (cin, Team2);
//FIRST QUARTER
cout << "\nQUARTER 1:\n\n";
cout << "Team " << Team1 << " Score: ";
cin >> Q1teamOne;
cout << "Team " << Team2 << " Score: ";
cin >> Q1teamTwo;
if (Q1teamOne > Q1teamTwo)
{
cout <<"****" << "Team " << Team1 << " is Leading.****\n\n";
}
else if (Q1teamOne < Q1teamTwo)
{
cout <<"****" << Team2 << " is Leading.****\n\n";
}
else if (Q1teamOne = Q1teamTwo)
{
cout <<"****We Have a Tie!!****\n\n";
}
//SECOND QUARTER
cout << "\nQUARTER 2:\n\n";
cout << "Team " << Team1 << " Score: ";
cin >> Q2teamOne;
Q2TeamOneTotal = Q1teamOne + Q2teamOne;
cout <<"Total Score: "<< Q2TeamOneTotal <<endl;;
cout << "Team " << Team2 << " Score: ";
cin >> Q2teamTwo;
Q2TeamTwoTotal = Q1teamTwo + Q2teamTwo;
cout <<"Total Score: " << Q2TeamTwoTotal;
if (Q2TeamOneTotal > Q2TeamTwoTotal)
{
cout <<"\n****" << Team1 << " is Leading.****\n\n";
}
else if (Q2TeamOneTotal < Q2TeamTwoTotal)
{
cout <<"\n****" << Team2 << " is Leading.****\n\n";
}
else if (Q2TeamOneTotal = Q2TeamTwoTotal)
{
cout <<"\n****We Have a Tie!!****\n\n";
}
//THIRD QUARTER
cout << "\nQUARTER 3:\n\n";
cout << "Team " << Team1 << " Score: ";
cin >> Q3teamOne;
Q3TeamOneTotal = Q1teamOne + Q2teamOne + Q3teamOne;
cout <<"Total Score: "<< Q3TeamOneTotal <<endl;;
cout << "Team " << Team2 << " Score: ";
cin >> Q3teamTwo;
Q3TeamTwoTotal = Q1teamTwo + Q2teamTwo + Q3teamTwo;
cout <<"Total Score: " << Q3TeamTwoTotal;
if (Q3TeamOneTotal > Q3TeamTwoTotal)
{
cout <<"\n****" << Team1 << " is Leading.****\n\n";
}
else if (Q3TeamOneTotal < Q3TeamTwoTotal)
{
cout <<"\n****" << Team2 << " is Leading.****\n\n";
}
else if (Q3TeamOneTotal = Q3TeamTwoTotal)
{
cout <<"\n****We Have a Tie!!****\n\n";
}
//FOURTH QUARTER
cout << "\nQUARTER 4:\n\n";
cout << "Team " << Team1 << " Score: ";
cin >> Q4teamOne;
Q4TeamOneTotal = Q1teamOne + Q2teamOne + Q3teamOne + Q4teamOne;
cout <<"Total Score: "<< Q4TeamOneTotal <<endl;
cout << "Team " << Team2 << " Score: ";
cin >> Q4teamTwo;
Q4TeamTwoTotal = Q1teamTwo + Q2teamTwo + Q3teamTwo + Q4teamTwo;
cout <<"Total Score: " << Q4TeamTwoTotal;
if (Q4TeamOneTotal > Q4TeamTwoTotal)
{
cout <<"\n****" << Team1 << " is Leading.****\n\n";
}
else if (Q4TeamOneTotal < Q4TeamTwoTotal)
{
cout <<"\n****" << Team2 << " is Leading.****\n\n";
}
else if (Q4TeamOneTotal = Q4TeamTwoTotal)
{
cout <<"\n****We Have a Tie!!****\n\n";
}
For example
#include <functional>
//...
std::reference_wrapper<int> teamOne[] = { Q1teamOne, Q2teamOne, Q3teamOne, Q4teamOne };
std::reference_wrapper<int> teamTwo[] = { Q1teamTwo, Q2teamTwo, Q3teamTwo ,Q4teamTwo };
Here is a demonstrative program
#include <iostream>
#include <functional>
int main()
{
int Q1teamOne, Q2teamOne, Q3teamOne, Q4teamOne;
std::reference_wrapper<int> teamOne[] =
{
Q1teamOne, Q2teamOne, Q3teamOne, Q4teamOne
};
int i = 0;
for ( auto &x : teamOne ) x.get() = i++;
for ( const auto &x : teamOne ) std::cout << x << ' ';
std::cout << std::endl;
return 0;
}
The program output is
0 1 2 3
Or if the link between the original values and the array is not need then you could write simply
double teamOneScore[] = { Q1teamOne, Q2teamOne, Q3teamOne, Q4teamOne };
Also you could use initializer list in the range based for statement without declaring any array. For example
for ( int x : { Q1teamOne, Q2teamOne, Q3teamOne, Q4teamOne } ) std::cout << x << ' ';
std::cout << std::endl;
what about:
teamOneScore[ 0 ] = Q1teamOne;
teamOneScore[ 1 ] = Q2teamOne;
teamOneScore[ 2 ] = Q3teamOne;
teamOneScore[ 3 ] = Q4teamOne;
teamTwoScore[ 0 ] = Q1teamTwo;
teamTwoScore[ 1 ] = Q2teamTwo;
teamTwoScore[ 2 ] = Q3teamTwo;
teamTwoScore[ 3 ] = Q4teamTwo;
But consider:
the arrays teamOneScore and teamTwoScore are arrays of double and your scores are int, so:
change the type of the arrays to be ints or
cast the assignment as: teamOneScore[ 0 ] = static_cast< double >( Q1teamOne );
Also, just for your information, this comparison is not correct:
else if (Q4TeamOneTotal = Q4TeamTwoTotal)
It should be:
else if (Q4TeamOneTotal == Q4TeamTwoTotal)
As a final note, you can use the arrays to store the scores from the cin and avoid the use of the Q1teamOne, ... vars.

Score component in beginner C++ program

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