C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 



Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #31  
Old November 28th, 2006, 02:09 PM
joelw joelw is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2006
Posts: 67 joelw User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 16 h 55 m 47 sec
Reputation Power: 13
ok works now. now if i want to re display them on my screen in another loop such as example...name:
number:
score:
how would i do that?

Reply With Quote
  #32  
Old November 28th, 2006, 02:18 PM
Paul820 Paul820 is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2006
Location: United Kingdom
Posts: 428 Paul820 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 Days 7 h 55 m 4 sec
Reputation Power: 13
Yes, use another loop

Code:
cout << players[i].name << endl;


etc..

Reply With Quote
  #33  
Old November 28th, 2006, 02:23 PM
joelw joelw is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2006
Posts: 67 joelw User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 16 h 55 m 47 sec
Reputation Power: 13
sorta like this?
Code:
using namespace std;

const int SIZE = 25;

struct Players
{
       char name[SIZE];        //Player's Name
       int playNum;            //Player's Number
       double Points;          //Point's Scored
};

int main()
{
    const int NUM_PLAYERS = 5; //Number of Players
	// Dynamically allocate the memory needed.
    Players *players = new Players[NUM_PLAYERS];      //Array of sturctures
    int index;                 //Loop

    // Get Player data.
    cout << "Enter the players by ";
    cout << " players, numbers, and their scores.\n";
    for (index = 0; index < NUM_PLAYERS; index++)
    {
        cout << "Please enter players name: ";
        cin.ignore();
		cin.getline( players[index].name, 25 );
        cout << "Please enter players number: ";
        ( cin >> players[index].playNum ).get();
        cout << "Please enter points scored by player: ";
        ( cin >> players[index].Points ).get();

    }

    // Display the players data
    cout << "Here is the players data: ";
    for (index = 0; index < NUM_PLAYERS; index++)
    {
        cout << players[index].name << endl;
        




	// Delete the memory.
	delete [] players;





    return 0;

Reply With Quote
  #34  
Old November 28th, 2006, 02:33 PM
Paul820 Paul820 is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2006
Location: United Kingdom
Posts: 428 Paul820 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 Days 7 h 55 m 4 sec
Reputation Power: 13
Yes, just like the loop to get the data.

Reply With Quote
  #35  
Old November 28th, 2006, 03:50 PM
joelw joelw is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2006
Posts: 67 joelw User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 16 h 55 m 47 sec
Reputation Power: 13
ok this is what i got but the names are cut of what do you think i did wrong?
Code:
#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

const int SIZE = 25;

struct Players
{
       char name[SIZE];        //Player's Name
       int playNum;            //Player's Number
       double Points;          //Point's Scored
};

int main()
{
    const int NUM_PLAYERS = 5; //Number of Players
	// Dynamically allocate the memory needed.
    Players *players = new Players[NUM_PLAYERS];      //Array of sturctures
    int index;                 //Loop

    // Get Player data.
    cout << "Enter the players by ";
    cout << " players, numbers, and their scores.\n";
    for (index = 0; index < NUM_PLAYERS; index++)
    {
        cout << "Please enter players name: ";
        cin.ignore();
		cin.getline( players[index].name, 25 );
        cout << "Please enter players number: ";
        ( cin >> players[index].playNum ).get();
        cout << "Please enter points scored by player: ";
        ( cin >> players[index].Points ).get();

    } 

	//Display the players data
	cout << "Here is the players data:\n";
	for (index = 0; index < NUM_PLAYERS; index++)
	{
		cout << "Name: " << players->name << endl;
		cout << "Number: " << players->playNum << endl;
		cout << "Score: " << players->Points << endl;
	}
	// Delete the memory.
	delete [] players;






    return 0;



}

Reply With Quote
  #36  
Old November 28th, 2006, 04:04 PM
Paul820 Paul820 is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2006
Location: United Kingdom
Posts: 428 Paul820 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 Days 7 h 55 m 4 sec
Reputation Power: 13
Because you are using the wrong operator. That's the indirect membership operator. The ARROW thing.

Code:
cout << "Name: " << players->name << endl;


That is only used when using pointers to the structure through functions.

You want the membership operator. The DOT.

Code:
cout << "Name: " << players[index].name << endl;


Is there any specific reason you are trying to use it like that? Are you having a function?

Reply With Quote
  #37  
Old November 29th, 2006, 04:33 AM
joelw joelw is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2006
Posts: 67 joelw User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 16 h 55 m 47 sec
Reputation Power: 13
that worked except like if i enter say John will it is displayed back to me it says ohn cutting off the the first letter is that my compiler or am i still something wrong here is the code
Code:
//Display the players data
	cout << "Here is the players data:\n";
	for (index = 0; index < NUM_PLAYERS; index++)
	{
		cout << "\n\nName: " << players[index].name << endl;
		cout << "Number: " << players[index].playNum << endl;
		cout << "Score: " << players[index].Points << endl;
	}
	// Delete the memory.
	delete [] players;

Reply With Quote
  #38  
Old November 29th, 2006, 08:57 AM
Paul820 Paul820 is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2006
Location: United Kingdom
Posts: 428 Paul820 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 Days 7 h 55 m 4 sec
Reputation Power: 13
Here you go, use that:

Code:
#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

const int SIZE = 25;

struct Players
{
       char name[SIZE];        //Player's Name
       int playNum;            //Player's Number
       double Points;          //Point's Scored
};

int main()
{
    const int NUM_PLAYERS = 5; //Number of Players
	// Dynamically allocate the memory needed.
    Players *players = new Players[NUM_PLAYERS];      //Array of sturctures
    int index;                 //Loop

    // Get Player data.
    cout << "Enter the players by ";
    cout << " players, numbers, and their scores.\n";
    for (index = 0; index < NUM_PLAYERS; index++)
    {
        cout << "Please enter players name: ";
	cin.getline( players[index].name, SIZE );
        cout << "Please enter players number: ";
        cin >> players[index].playNum;
        cout << "Please enter points scored by player: ";
        ( cin >> players[index].Points ).get();
    }

	//Display the players data
	cout << "Here is the players data:\n";
	for (index = 0; index < NUM_PLAYERS; index++)
	{
		cout << "Name: " << players[index].name << endl;
		cout << "Number: " << players[index].playNum << endl;
		cout << "Score: " << players[index].Points << endl;
	}
	// Delete the memory.
	delete [] players;

    return 0;
}

Reply With Quote
  #39  
Old November 29th, 2006, 09:21 AM
joelw joelw is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2006
Posts: 67 joelw User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 16 h 55 m 47 sec
Reputation Power: 13
that worked great now how would i throw a caculation in there that will total up the scores and display a message saying something like the total points from the team score is...

Reply With Quote
  #40  
Old November 29th, 2006, 09:27 AM
Paul820 Paul820 is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2006
Location: United Kingdom
Posts: 428 Paul820 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 Days 7 h 55 m 4 sec
Reputation Power: 13
Just declare a double variable initialized to zero somewhere called total. Then inside the loop for printing out the information, at the bottom put:

Code:
total += players[index].points;

Reply With Quote
  #41  
Old November 29th, 2006, 10:18 AM
joelw joelw is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2006
Posts: 67 joelw User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 16 h 55 m 47 sec
Reputation Power: 13
that worked thanks

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > Help with storing data in structure


Developer Shed Advertisers and Affiliates


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.

© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap