hey there, im new on C++ and have to do an assignment where i have to display names of soccer players and the number of goals and cards received. im busy with my first function, and if someone can help me i would realy apreciate it.

//question 1 assignment 4

#include <iostream>
using namespace std;

void inputinformation(int& goals)
{   for (int i =1;i <= 12; i++)
    {   cout << " Enter the number of goals scored by the player : ";
         cin >> goals;
     }   
}

int main ()
{   const int NR_PLAYERS = 12;
    string names[ ] = {"Tiny Nakedi" , "Cecil Mametsa" , "Chris Rooney" , "Mario Thomas" , "Frank Ntini" , "Charlie Ashley",
                      "Morio Zondo" , "Khabo Mosimane" , "Pitso Hack" , "Lloyd Mokoena" , "Rick Smith" , "Kabelo Ramago"};
    
    int goals;
    int cards;
    string namesMore;
    
    cout << names[goals] << endl;
    inputinformation(goals);    
    
    return 0;
}

the output :

Tiny Nakedi
Enter the number of goals scored by the player : 3
Enter the number of goals scored by the player : 4
Enter the number of goals scored by the player : 3

it must show the players name on each line for the goals. here it only shows the first one.

im first trying to figure out just putting in the goals. what am i doing wrong :)
thank you in advance. hope someone helps me quick :)

commented: Good +0

Recommended Answers

All 11 Replies

So you want the program to print out the name of every player and you need to enter the goals at every player?

you need one for loop in your main and I think you need to remove the one in the function. You also need to put the function call before the cout... something like this perhaps

for (int i =1;i <= 12; i++)
{
        inputinformation(goals);
        cout << names[goals] << endl;
}

alternatively you can do this to the loop in your function

for (int i =1;i <= 12; i++)
{ 
      cout << " Enter the number of goals      scored by the player : ";
      cin >> array[i];
}

Use another for loop to display in the main. Hope that helps

let me state the first function like in the question asked : the information about goals are input here. the function should contain a for loop, every time the loop is executed the name of the player should be displayed and the the number of goals scored have to be entered..
can i put one in the main then aswell?

yes the name should be displayed of the player and i have to enter the number of goals scored

well then you would need another for loop in the main function that would move through the player names.

for (int i =1;i <= 12; i++)
{
     cout << names[i] << endl;
     inputinformation(goals);
}

it doesnt move through.. i does the same as my first one, just displaying only the second name now

it doesnt move through.. i does the same as my first one, just displaying only the second name now

To loop through all the 12 names, you can use ...

for(int ii = [B]0[/B]; ii < sizeof(names)/sizeof(names[0]); ++ii)
{
    cout << "player: " << names[ii] << endl;
}

Note that the first index is zero (not 1) and the last is 11. You must not access names[12] because that would be out of bounds.

One dimentional arrays :
i have to write a program to display the names of soccer players who received more (yellow/red) cards than the number of goals scored by them. the names of the players are given, but the number of goals and cards have to be input.
there is 4 functions, and one main program.
im still struggling with my first function which is :
the input of the goals and cards. the function should contain a for loop. everytime the loop is executed,the name of a player should be displayed and then the number of goals scored and cards received have to be entered.. i got this far >>>

#include <iostream>
using namespace std;

void inputinformation(int& goals, int& cards)
{   for (int i =1;i <= 12; i++)
    {   cout << " Enter the number of goals scored by the player : ";
        cin >> goals;
        cout << " Enter the number of cards received by the player : ";
        cin >> cards;
    }   
}

int main ()
{   const int NR_PLAYERS = 12;
    string names[ ] = {"Tiny Nakedi" , "Cecil Mametsa" , "Chris Rooney" , "Mario Thomas" , "Frank Ntini" , "Charlie Ashley",
                      "Morio Zondo" , "Khabo Mosimane" , "Pitso Hack" , "Lloyd Mokoena" , "Rick Smith" , "Kabelo Ramago"};
    
    int goals;
    int cards;
    string namesMore;
    
    cout << names[goals] << endl;
    inputinformation(goals,cards);
 
    return 0;
}

the only problem is it only displaying the first name, and then runs trough the loop with the goals and cards.not displaying the other names first. what am i doing wrong? can someone help me please.

my output looks as follows :

Tiny Nakedi
Enter the number of goals scored by the player : 3
Enter the number of cards received by the player : 3
Enter the number of goals scored by the player : 4
Enter the number of cards received by the player : 2

whereas it should look like :
Tiny Nakedi
Enter the number of goals scored by the player : 3
Enter the number of cards received by the player : 3
Cecil Mametsa
Enter the number of goals scored by the player : 4
Enter the number of cards received by the player : 2

gloas and cards have to be arrays also so that names[0] is associated with glals[0] and cards[0]

Create the arrays in main() like you did with names, then pass all three arrays to the other functions so that the other functions have access to them, like this

void inputinformation(string names[], int goals[], int cards[])
{

}

line 22: you don't use goals as an index into names

for(i = 0; i < 4; i++)
   cout << names[i] << " " << goals[i] << " " << cardes[i] << "\n";

Read some of the suggestions in your other post

Read some of the suggestions in your other post

Thanks for pointing out that there were two threads about the same thing. I merged the two threads into one to avoid confusion and duplicate posting.

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.