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.
Related
#include <iostream>
using namespace std;
struct Author {
int id ;
string fullname;
string nickname;
int age ;
};
struct Book {
int id ;
string title;
int year;
float price ;
};
void menu (){
cout << "Add New Author (1) \n" ;
cout << "Display Author List (2)\n " ;
cout << "Add New Book (3) \n" ;
cout << "Display Book List (4) \n" ;
cout << "Edit Author nickname (5) \n" ;
cout << "Quit The Program (0) \n\n" ;
cout << "Please Enter The Number : " ;
}
Author addauthor(Author *v ){
cout << "Enter ID: ";
cin >> v->id;
cout << "Enter Full Name: ";
cin.ignore();
getline(cin ,v->fullname) ;
cout << "Enter nickname: ";
cin >> v->nickname;
cout << "Enter Age: ";
cin >> v->age;
cout << endl ;
return *v ;
}
void displayauthor(Author *v ){
cout << "ID: " << v->id << endl;
cout << "Full Name: " << v->fullname << endl;
cout << "Nickname: " << v->nickname << endl ;
cout << "Age: " << v->age << endl << endl;
}
Book addbook(Book *v ){
cout << "Enter ID: ";
cin >> v->id;
cout << "Enter Title: ";
cin.ignore();
getline(cin ,v->title);
cout << "Enter Year: ";
cin >> v->year;
cout << "Enter Price : RM";
cin >> v->price;
cout << endl ;
return *v ;
}
void displaybook(Book *v ){
cout << "ID: " << v->id << endl;
cout << "Title: " << v->title << endl;
cout << "Year: " << v->year << endl ;
cout << "Price: RM" << v->price << endl << endl;
}
int main()
{
int n ;
Author a[1000];
Book b[1000];
int i, j , num1 , num2 ,id ;
string sr, rp;
do {
menu();
cin >> n ;
{
if (n==1) {
cout << "Please Enter the number of Author (Max 100): ";
cin >> num1;
cout << endl ;
for(i = 0 ;i < num1 ; i++ )
{
a[i]= addauthor(&a[i]);
}
}
else if (n==2) {
cout << "\nAuthor Information." << endl << endl;
for(i = 0 ;i < num1 ; i++ )
{
displayauthor(&a[i]);
}
}
else if (n==3) {
cout << "Please Enter the number of Book (Max 100): ";
cin >> num2;
cout << endl ;
for( i = 0 ;i < num2 ; i++ )
{
b[i]= addbook(&b[i]);
}
}
else if (n==4) {
cout << "\nBook Information." << endl << endl;
for(i = 0 ;i < num2 ; i++ )
{
displaybook(&b[i]);
}
}
else if (n==5) {
cout << "Please Enter Old Nickname : " << endl ;
cin >> sr ;
cout << "Please Enter Author New Nickname : " << endl ;
cin >> rp ;
for (int j = 0 ; j < num1 ; j++){
if (a.nickname[j] == sr){
cout << " The nickname has been Found." ;
a.nickname[j]=rp;
}
else {
cout << " Nickname not found ." ;
}
}
}
else {
cout << "The number you've enter is not available"<< endl << endl ;
}
}
}while (n !=0);
cout << "Program end" ;
return 0;
}
I tried to replace a nickname for the selected author which happen on this part
else if (n==5) {
cout << "Please Enter Old Nickname : " << endl ;
cin >> sr ;
cout << "Please Enter Author New Nickname : " << endl ;
cin >> rp ;
for (int j = 0 ; j < num1 ; j++){
if (a.nickname[j] == sr){
cout << " The nickname has been Found." ;
a.nickname[j]=rp;
}
else {
cout << " Nickname not found ." ;
}
}
}
So basically, the user tried to enter a new nickname for the Author by having the user to enter an old one and the error is "error: request for member ‘nickname’ in ‘a’, which is of non-class type ‘Author [1000]’" which happened in this 2 lines :
if (a.nickname[j] == sr){
a.nickname[j]=rp;
The problem you're facing is just the order of operations you're performing.
a is an array of Authors, nickname is the member variable you're trying to access.
you first need to decide which array entry you want to select by using a[j], then afterwards access the member variable 'nickname' by appending .nickname.
this results in the statement a[j].nickname instead of a.nickname[j]
So I want to search my array for my data and move back and forth, so this is what i came up with, however this isn't working, can there be a better way to do this ? I tried to google some tips however I wasn't finding a suitable way to do this...
Can binary search be used to do this same thing ?
struct student
{
int stid = NULL;
char stname[15] = "NULL";
int grades = NULL;
string date = NULL;
};
int main()
{
int choose = 0;
int stid = 0;
int array = 1;
struct student s[1] = { 001, "Sara", "123456", "Canada", "02/01/2019" };
while (choose != 4)
{
cout << "1. Add\n";
cout << "2. View\n";
cout << "3. Search\n";
cout << "4. Quit\n";
cin >> choose;
if (choose == 3)
{
cout << "How would you like to find the Student Details?" << endl;
cout << "1 - By student id " << endl;
int choice = 0;
cin >> choice;
if (choice == 1)
{
cout << "Student ID: ";
cin >> stid;
for (int i = 0; i < array; i++)
{
if (s[i].stid == stid)
{
cout << "ID: " << s[i].stid << "\n";
cout << "Name: " << s[i].stname << "\n";
cout << "Grades: " << s[i].grades << "\n";
cout << "Date joined school: " << s[i].date<< "\n";
while (true)
{
cout << "Which record do you wish to see?" << endl;
cout << " 1 : Forth " << endl;
cout << " 2 : Back " << endl;
int choice = 0;
cin >> choice;
if (choice == 1)
{
stid = stid + 1;
for (int i = 0; i == stid; i++)
{
if (s[i].stid == stid)
{
cout << "ID: " << s[i].stid << "\n";
cout << "Name: " << s[i].stname << "\n";
cout << "Grades: " << s[i].grades << "\n";
cout << "Date joined school: " << s[i].date<< "\n";
}
}
}
else if (choice == 2)
{
stid = stid - 1;
for (int i = 0; i == stid; i++)
{
if (s[i].stid == stid)
{
cout << "ID: " << s[i].stid << "\n";
cout << "Name: " << s[i].stname << "\n";
cout << "Grades: " << s[i].grades << "\n";
cout << "Date joined school: " << s[i].date<< "\n";
}
}
}
Thank you for helping.
I'm making a code where users can type in numbers that will be stored in an array which is inside a struct. However, sometimes, the value that I input is not the same as the one that is stored/display. This problem is not very consistent. For example, when I input 10, it could either be shown as: 6384644, 6382852, or actually 10. I am very confused for this, I've tried changing the array data type using int, long, and double, but to no avail.
#include <iostream>
#include <iomanip>
using namespace std;
int main () {
int n,x,totalAge = 0,maxAge = 0,avgAge = 0,maxGoals = 0,bestPlayer = 0, oldest = 0;
cout << "Input the number of players: ";
cin >> n;
cout << "Input the number of games: ";
cin >> x;
struct players {
string name;
string currTeam;
int shirtn;
int age;
float height;
float weight;
int totalGoals;
long goals[];
};
players goals[x];
players playerList[n];
for (int i = 0; i < n; i++) {
cout << "Player " << (i+1) << endl;
cout << "Input player's name: ";
cin.ignore();
getline (cin, playerList[i].name);
cout << "Input player's current team: ";
getline (cin, playerList[i].currTeam);
cout << "Input player's shirt number: ";
cin >> playerList[i].shirtn;
cout << "Input player's age: ";
cin >> playerList[i].age;
cout << "Input player's height (m): ";
cin >> playerList[i].height;
cout << "Input player's weight (kg): ";
cin >> playerList[i].weight;
cout << endl;
for (int a = 0; a < x; a++) {
playerList[i].goals[a] = 0;
playerList[i].totalGoals = 0;
}
for (int a = 0; a < x; a++) {
cout << "Game " << (a+1) << "'s number of goals: ";
cin >> playerList[i].goals[a];
playerList[i].totalGoals += playerList[i].goals[a];
}
if (playerList[i].totalGoals > maxGoals) {
maxGoals = playerList[i].totalGoals;
bestPlayer = i;
}
if (playerList[i].age > maxAge) {
maxAge = playerList[i].age;
oldest = i;
}
totalAge += playerList[i].age;
cout << endl;
}
cout << endl;
for (int i = 0; i < n; i++) {
cout << playerList[i].name << endl;
cout << "--------------------" << endl;
cout << "Current team: " << playerList[i].currTeam << endl;
cout << "Shirt Number: " << playerList[i].shirtn << endl;
cout << "Age: " << playerList[i].age << endl;
cout << "Height: " << playerList[i].height << " m" << endl;
cout << "Weight: " << playerList[i].weight << " kg" << endl;
cout << endl;
for (int a = 0; a < x; a++) {
cout << "Game " << (a+1) << "'s number of goals: " << playerList[i].goals[a] << endl;
}
cout << endl << endl;
}
avgAge = totalAge / n;
cout << "Average age of players: " << avgAge << endl;
cout << "Oldest Player: " << playerList[oldest].name << " (" << maxAge << ") ";
cout << "Player who got the most goals: " << playerList[bestPlayer].name << ", shirt number: " << playerList[bestPlayer].shirtn << ". With total goals of: " << playerList[bestPlayer].totalGoals << endl;
}
Your code's corrected version would be this:
#include<string>
#include<iostream>
#include<vector> //for vector
using namespace std;
struct players {
string name;
string currTeam;
int shirtn;
int age;
float height;
float weight;
int totalGoals;
vector<long> goals; //used vector instead of array
};
int main () {
int N, X;
int totalAge = 0, maxAge = 0, avgAge = 0, maxGoals = 0, bestPlayer = 0, oldest = 0;
cout << "Input the number of players: ";
cin >> N;
cout << "Input the number of games: ";
cin >> X;
players player[X];
for (int i = 0; i < N; i++) {
cout << "Player " << (i+1) << endl;
cout << "Input player's name: ";
cin>>player[i].name;
cout << "Input player's current team: ";
cin>>player[i].currTeam;
cout << "Input player's shirt number: ";
cin >> player[i].shirtn;
cout << "Input player's age: ";
cin >> player[i].age;
cout << "Input player's height (m): ";
cin >> player[i].height;
cout << "Input player's weight (kg): ";
cin >> player[i].weight;
cout << endl;
player[i].totalGoals = 0;
for (int a = 0; a < X; a++) {
long G;
cout << "Game " << (a+1) << "'s number of goals: ";
cin >> G;
player[i].goals.push_back(G);
player[i].totalGoals += G;
}
if (player[i].totalGoals > maxGoals) {
maxGoals = player[i].totalGoals;
bestPlayer = i;
}
if (player[i].age > maxAge) {
maxAge = player[i].age;
oldest = i;
}
totalAge += player[i].age;
cout << endl;
}
cout << endl;
for (int i = 0; i < N; i++) {
cout << player[i].name << endl;
cout << "--------------------" << endl;
cout << "Current team: " << player[i].currTeam << endl;
cout << "Shirt Number: " << player[i].shirtn << endl;
cout << "Age: " << player[i].age << endl;
cout << "Height: " << player[i].height << " m" << endl;
cout << "Weight: " << player[i].weight << " kg" << endl;
cout << endl;
for (int a = 0; a < X; a++) {
cout << "Game " << (a+1) << "'s number of goals: " << player[i].goals[a] << endl;
}
cout << endl << endl;
}
avgAge = totalAge / N;
cout << "Average age of players: " << avgAge << endl;
cout << "Oldest Player: " << player[oldest].name << " (" << maxAge << ") ";
cout << "Player who got the most goals: " << player[bestPlayer].name << ", shirt number: " << player[bestPlayer].shirtn << ". With total goals of: " << player[bestPlayer].totalGoals << endl;
return 0;
}
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";
}
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;
}
}