Help with array...

This is a discussion on Help with array... within the C++ Programming forums, part of the General Programming Boards category; I'm new to C++, if anyone can help me, i'll be grateful. The question is a follow: User will be ...

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    14

    Help with array...

    I'm new to C++, if anyone can help me, i'll be grateful.

    The question is a follow:
    User will be asked to key in the number of players (maximum 5 players) that challenge each other for top ranking. Their name will then be taken in one by one. Assume the names will not exceed 29 characters including spaces.

    My question is how to store the name into an array. I only know how to store one player's name into it. And why is it always start with the 2nd participants instead of the first one??

    Here is my code:
    Code:
    #include<iostream>
    using namespace std;
    int main(void)
    {
    	int no_participants;
    	int crtl_1;
    	char name_participants[30];
    
    	cout<<"Please enter the number of participants (1-5):";
    	cin>>no_participants;
    	
    	for (crtl_1=1; crtl_1<=no_participants; crtl_1++)
    	{
    		cout<<"Please enter the name of participants "<<crtl_1 <<":";
    		cin.getline (name_participants, 30);
    		cout<<endl;
    	}
    	cout<<name_participants;
    	cout<<endl;
    	return 0;
    }

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,262
    Declare name_participants as a two dimensional array (char name_participants[5][30] declares an array of five elements, each of which has 30 char). Then supply name_participants[some_index] to the cin.getline() call. (I'll leave it to you to work out what some_index should be in your code).

    You will also need a loop to print out the name_participants array.

    Keep in mind that array indexing starts from 0, not 1.
    Right 98% of the time, and don't care about the other 3%.

  3. #3
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    And being C++, you would benefit from looking at the Standard Template Library, specifically std::vector and std::map.
    Last edited by AndrewHunter; 08-17-2011 at 09:19 AM. Reason: Fixed links
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  4. #4
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    Quote Originally Posted by AndrewHunter View Post
    And being C++, you would benefit from looking at the Standard Template Library, specifically std::vector and std::map.
    Shouldn't a std::array<std::string,5> be less complicated ?
    or just a std::string names[5]; //if no C++11 support..
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  5. #5
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,425
    Shouldn't a std::array<std::string,5> be less complicated ?
    or just a std::string names[5]; //if no C++11 support..
    I think its because the study question posted suggests they were instructed to use c style arrays, or have perhaps been studying them, or i would have thought similar

    Assume the names will not exceed 29 characters including spaces.
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  6. #6
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Actually, std::map is the way to go with this one for the simplest solution if ranking the names is something that the OP is going to need to consider later in his program. I don't normally do this, however my new pet peeve is seeing C code in C++, so:
    Code:
    #include <iostream>
    #include <string>
    #include <map>
    
    int main(){
    
    	std::map<int, std::string>NameList;
    	std::map<int,std::string>::iterator ii;
    	std::string Name;
    	int rank;
    
    	for(int i=0;i<5;i++){
    		std::cout<<"Enter name: ";
    		std::getline(std::cin, Name);
    		std::cout<<"Enter rank: ";
    		std::cin >> rank;
    		std::cin.ignore();
    		NameList.insert(std::pair<int,std::string>(rank,Name));
    	}
    
    	for(ii=NameList.begin(); ii!= NameList.end(); ii++){
    		std::cout<<ii->first <<" "<<ii->second<<std::endl;
    	}
    
    	std::cin.get();
    	return(0);
    }
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  7. #7
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Quote Originally Posted by rogster001 View Post
    I think its because the study question posted suggests they were instructed to use c style arrays, or have perhaps been studying them, or i would have thought similar
    And I don't doubt that one bit, I am just tired of broken curriculums.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 08-23-2010, 02:31 PM
  2. Replies: 9
    Last Post: 04-07-2010, 10:03 PM
  3. Replies: 1
    Last Post: 10-21-2007, 07:44 AM
  4. Replies: 6
    Last Post: 11-09-2006, 02:28 AM
  5. Replies: 1
    Last Post: 04-25-2006, 12:14 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21