I have to store my input values from function 1 into another array caled namesMore in function 2. Not to sure how to store into second array

#include <iostream>
#include <string>
using namespace std;


const int NR_PLAYERS = 4;


void inputInformation(int goalsP[], int cardsR[], string names[])
{
for(int i =0; i < NR_PLAYERS; i++)
{
cout << "Player: " << names << endl;
cout << "Enter the number of goals scored: ";
cin >> goalsP;
cout << "Enter the number of cards received: ";
cin >> cardsR;
}
}


void findPlayersWithMoreCardsThanGoals(int goalsP[], int cardsR[], string names[])
{
for(int namesMore = 0; namesMore <= NR_PLAYERS; namesMore++)
{
if(cardsR > goalsP)
names = namesMore;
}
}
void displayInfo(string namesMore)
{
cout << "The following Players got more cards than goals: " << namesMore << '\t' << endl;
}


int main( )
{


string names[] =  {"Tiny Nakedi", "Cecil Mametsa", "Chris Rooney", "Mario Thomas" };
int goals[NR_PLAYERS];
int cards[NR_PLAYERS];
string namesMore;


inputInformation(goals, cards, names);
findPlayersWithMoreCardsThanGoals(goals, cards, names);
displayInfo(namesMore);


return 0;
}

Recommended Answers

All 9 Replies

Sorry, here it is with code tags I hope.


#include <iostream>
#include <string>
using namespace std;

const int NR_PLAYERS = 4;            

void inputInformation(int goalsP[], int cardsR[], string names[])
{
    for(int i =0; i < NR_PLAYERS; i++)
    {
        cout << "Player: " << names[i] << endl;
        cout << "Enter the number of goals scored: ";
        cin >> goalsP[i];
        cout << "Enter the number of cards received: ";
        cin >> cardsR[i];
    }
}

void findPlayersWithMoreCardsThanGoals(int goalsP[], int cardsR[], string names[])
{
    for(int namesMore = 0; namesMore <= NR_PLAYERS; namesMore++)
    {
        if(cardsR > goalsP)
           names = namesMore; 
           }
}
void displayInfo(string namesMore)
{
    cout << "The following Players got more cards than goals: " << namesMore << '\t' << endl;
}

int main( )
{
    
     string names[] =  {"Tiny Nakedi", "Cecil Mametsa", "Chris Rooney", "Mario Thomas" };
    int goals[NR_PLAYERS];
    int cards[NR_PLAYERS];
    string namesMore;
    
        inputInformation(goals, cards, names);
        findPlayersWithMoreCardsThanGoals(goals, cards, names);
        displayInfo(namesMore);
        
        return 0;
}

If you want to assign the values of one array to another maybe you can do something like this

for(int i=0;i<max;i++)
{
     array2[i]=array1[i];
}

use the following
names = (string*)namesMore;
instead of
names = namesMore;

I tried both. Changed code as follow:

void findPlayersWithMoreCardsThanGoals(int goalsP[], int cardsR[], string names[], const string namesMore[])
{
    for(int j = 0; j <= NR_PLAYERS; j++)
    {
        if(cardsR[j] > goalsP[0, 1, 2, 3])
           names[j] = namesMore[j]; 
           }}
void displayInfo(string namesMore[])
{
    for (int j = 0; j < NR_PLAYERS; j++)
    cout << "The following Players got more cards than goals: " << namesMore[j] << '\t' << endl;
}

int main( )
{
    
     string names[] =  {"Tiny Nakedi", "Cecil Mametsa", "Chris Rooney", "Mario Thomas" };
    int goals[NR_PLAYERS];
    int cards[NR_PLAYERS];
    string namesMore[NR_PLAYERS];
    
        inputInformation(goals, cards, names);
        findPlayersWithMoreCardsThanGoals(goals, cards, names, namesMore);
        displayInfo(namesMore);
        
        return 0;
}

Output remains as follow:

Player: Tiny Nakedi
Enter the number of goals scored: 1
Enter the number of cards received: 2
Player: Cecil Mametsa
Enter the number of goals scored: 1
Enter the number of cards received: 2
Player: Chris Rooney
Enter the number of goals scored: 2
Enter the number of cards received: 2
Player: Mario Thomas
Enter the number of goals scored: 2
Enter the number of cards received: 2
The following Players got more cards than goals:
The following Players got more cards than goals:
The following Players got more cards than goals:
The following Players got more cards than goals:
Press any key to continue . . .

I think your getting no output because you are overwriting the data in the array names[]. You want to assign the values of name[] to namesMore[]. Change your assignment statement to something like this

namesMore[j]=names[j]

my prevoius posy only help you to remove the error. this will works fine

#include <iostream>
#include <string>
using namespace std;

const int NR_PLAYERS = 4;

void inputInformation(int goalsP[], int cardsR[], string names[])
{
  for(int i =0; i < NR_PLAYERS; i++) {
	cout << "Player: " << names[i] << endl;
	cout << "Enter the number of goals scored: ";
	cin >> goalsP[i];
	cout << "Enter the number of cards received: ";
	cin >> cardsR[i];
  }
}
void findPlayersWithMoreCardsThanGoals(int goalsP[], int cardsR[], string names[])
{
  for(int namesMore = 0; namesMore < NR_PLAYERS; namesMore++) {
    if(cardsR[namesMore] > goalsP[namesMore]) {
      cout<<"\nPlayer " <<names[namesMore] << " got more cards than goals";
    }
  }
}
int main( )
{
string names[] = {"Tiny Nakedi", "Cecil Mametsa", "Chris Rooney", "Mario Thomas" };
int goals[NR_PLAYERS];
int cards[NR_PLAYERS];
string namesMore;
string ss[1000] ;
inputInformation(goals, cards, names);
findPlayersWithMoreCardsThanGoals(goals, cards, names);
return 0;
}

I get the following error if I assign it that way round.

passing `const std::string' as `this' argument of `std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::operator=(const std::basic_string<_CharT, _Traits, _Alloc>&) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]' discards qualifiers

Thanks Renjith, it is working now

I think your getting no output because you are overwriting the data in the array names[]. You want to assign the values of name[] to namesMore[]. Change your assignment statement to something like this

namesMore[j]=names[j]

A fixed and saved into array with your assinment as above, thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.