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:
  #1  
Old November 28th, 2006, 04:05 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: 12
Help with storing data in structure

ok, i need help writing a program that stores data about a basketball player in a structue. I need to create a structure for players name, players number, and by points the player scored. I need to keep an array of 5 of these structures. Which each element is for a different player on a team. The program should ask the user to enter data for each player. It should then show a table of each players number, name, and points scored and finally it should calculate and display the total points earned by the team.

so far i have written this but some how i think im doing it wrong if anyone could help thanks in advance.

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

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
    Players 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.name[SIZE];

Reply With Quote
  #2  
Old November 28th, 2006, 06:21 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
Have a look at 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.ignore(); 
		cin.getline( players[index].name, 25 );
	}
	
	// Delete the memory.
	delete [] players;

	return 0;
}

Reply With Quote
  #3  
Old November 28th, 2006, 06:57 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: 12
now thats what im looking for now inside the same for loop would i repeat the process to ask the players number and score and the if so then for the players number i put this
cin.getline( players[index].playNum); is that right?

Reply With Quote
  #4  
Old November 28th, 2006, 07:02 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
Yes you just go through your loop. You don't need the cin.getline for ints doubles etc, just use cin >> .

Reply With Quote
  #5  
Old November 28th, 2006, 07:12 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
Do your other two entries like this:

Code:
cout << "Enter the players number: ";
( cin >> players[index].playNum ).get();


Use the .get() at the end otherwise the newline will be left in the input queue from the cin.

Reply With Quote
  #6  
Old November 28th, 2006, 07:20 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: 12
ok when i do it i get that playNum is not been declared.
so i changed the code to be players[index].playNum and that worked but the loop didnt work then
Code:
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.playNum;
        cout << "Please enter points scored by player: ";
        cin >> players.Points;
        
    }

Reply With Quote
  #7  
Old November 28th, 2006, 07:26 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
Becaue the compiler does not know which playNum you mean. You have to do the square brackets aswell.

Code:
cout << "Enter the players number: ";
( cin >> players[index].playNum ).get();

cout << "Enter the players points: ";
( cin >> players[index].Points ).get();

Reply With Quote
  #8  
Old November 28th, 2006, 07:43 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: 12
yeah but i tried that and now after entering the second name it comes up with a bunch of nonsense i have a screen print if you want to see.

Reply With Quote
  #9  
Old November 28th, 2006, 07:54 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
Post your code so far and i'll have a look at where you are going wrong.

Reply With Quote
  #10  
Old November 28th, 2006, 08:02 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: 12
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();
        
    }
	
	
	// Delete the memory.
	delete [] players;

	
         
    
    
    
    
    
    
    
    
    system("PAUSE");
    return EXIT_SUCCESS;
}

Reply With Quote
  #11  
Old November 28th, 2006, 08:17 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
Works fine for me.

Reply With Quote
  #12  
Old November 28th, 2006, 08: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: 12
really keep in mind im using dev-c++ dose that make a difference

Reply With Quote
  #13  
Old November 28th, 2006, 08:46 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
Works in Dev-cpp aswell. And works in codeblocks. The only thing i can think of is if you are entering a double for the int or int for double. That messes it up.

Reply With Quote
  #14  
Old November 28th, 2006, 08:50 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: 12
what do you mean?

Reply With Quote
  #15  
Old November 28th, 2006, 08:55 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
Well, if your playerNum is expecting an int and you enter a double thinking it's the playerSCore. Post the screenshot. It's hard to figure out why it's going wrong at your end when it works on three compilers that i have tried it on. Visual C++ Express, Dev-cpp, and codeblocks, it works fine on them all. No errors, no warnngs, memory gets deleted when debugged.

Reply With Quote
  #16  
Old November 28th, 2006, 09:01 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: 12
ok i will but how do you supply a screen shot threw dev forums?

Reply With Quote
  #17  
Old November 28th, 2006, 09:06 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
When you type your reply, scroll down the page a bit and there is a button called manage attachments. Click it and search for the file to upload. Don't forget to read the valid extension bit.

Reply With Quote
  #18  
Old November 28th, 2006, 09:20 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: 12
i looked and did find that but let me explain it will compile and it starts by asking the players names but instead of doing all 5 players then moving on to asking the five players numbers and then onto the 5 players scores instead it is letting me enter two names and after pressing enter after the second name it looks like it loops like 5 times or so.

Reply With Quote
  #19  
Old November 28th, 2006, 09:38 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
So you want to enter all the players names then all the numbers and then all the scores? If you are trying to enter all the players names then it will go wrong like you have it set up now.

Reply With Quote
  #20  
Old November 28th, 2006, 09:41 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: 12
ok now is it possible to write a code that will ask the name, player number, and score like for instance name: dan
number: 825
score: 500
then loop around and then ask for the next players info up to 5 players?

Reply With Quote
  #21  
Old November 28th, 2006, 09:48 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
That's what it does:
Attached Images
File Type: jpg 2006-11-28_154649.jpg (54.4 KB, 197 views)

Reply With Quote
  #22  
Old November 28th, 2006, 11:40 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: 12
thats exactly what i want it to do so the code i gave you supose to do this if so can you send me your code that does it that after compiling maybe i have something enter incorrectly.

Reply With Quote
  #23  
Old November 28th, 2006, 11:44 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
It's your code that i used. I just copied the code from post #10 into the ide and built it. It should work.

Reply With Quote
  #24  
Old November 28th, 2006, 11:45 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: 12
and you use dev c++

Reply With Quote
  #25  
Old November 28th, 2006, 11:50 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
Yes, dev-cpp, codeblocks, and visualC++ express, all work fine. I don't know why it is not working for you.

Reply With Quote
  #26  
Old November 28th, 2006, 11:51 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: 12
becasue i just check with that code again and its not doing through dev c++

Reply With Quote
  #27  
Old November 28th, 2006, 11:55 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: 12
i dont know but is code blocks a free software maybe i will try with that one?

Reply With Quote
  #28  
Old November 28th, 2006, 11:59 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
Yes it's free. Get the recent nightly build from here: Nightly

Reply With Quote
  #29  
Old November 28th, 2006, 12:10 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: 12
just so we are clear this is what my code should look like

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();
        
    }
	
	
	// Delete the memory.
	delete [] players;

	
         
    
    
    
    
    
    
    
    
    system("PAUSE");
    return EXIT_SUCCESS;
}

Reply With Quote
  #30  
Old November 28th, 2006, 12:23 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, as it is now it works.

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