0

Anyone here know C++ or programming in general

I need help with this program. I made a structure, and an array out of that structure. When I try entering a name as a string, an infinite loop ensures. What is the problem?

    #include <iostream>
    #include <string>

    const int size = 12;

    struct soccer
    {
        std::string name;
    float points, jersey;   
};


void input(soccer []);

int main()
{
    soccer info[size];
    float total;

    input(info);
}

void input(soccer info [])
{
    for (int i = 0 ; i < size ; i++)
    {
        std::cout << "Enter the name of soccer player #" << i+1 << ": ";
        std::cin >> info[i].name;
        std::cout << "Enter the jersey number for this player:";
        std::cin >> info[i].jersey;
        while (info[i].jersey < 0)
        {
            std::cout << "The jersey number cannot be a negative number. Please enter a value number for jersey: ";
            std::cin >> info[i].jersey;
        }
        std::cout << "Enter the number of points scored by this player: ";
        std::cin >> info[i].points;
        while (info[i].points < 0)
        {       
            std::cout << "Points scored cannot be a negative number. Please enter a valid number for points: ";
            std::cin >> info[i].points;
        }
    }
}
5
  • @Merlin You may not emter a string that contains embedded blanks. Or use function getline instead of the operator >>. Commented Oct 31, 2014 at 4:00
  • by infinite you mean 12? Commented Oct 31, 2014 at 4:03
  • When I enter the first/last name, it goes into a loop that repeats the error messages. I'm working with an android tablet so this is what i'm going off. Commented Oct 31, 2014 at 4:10
  • how would you use getline in this case? Commented Oct 31, 2014 at 4:11
  • What exactly makes you think that there is an infinite loop? Perhaps the output you generate using cout << ... simply doesn't appear on the screen because you don't flush buffers. cout.flush() or cout << std::endl; may help. Commented Oct 31, 2014 at 4:15

1 Answer 1

1

It seems that you are entering more than one word in data member name using operator >>. Either enter only one word or use standard function std::getline( std::cin, name ) instead of the operator >>. Do not forget to use member function ignore before using std::getline that to remove the new line character from the stream buffer after entering points.

For example

#include <limits>

//...

std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' ); 
std::getline( std::cin, info[i].name );

Another approach is to use operator >> as before but to add one more operator that to enter first name and last name. Then you could simply concatenate these two names.

std::string first_name;
std::string last_name;

//...

std::cout << "Enter the first name of soccer player #" << i+1 << ": ";
std::cin >> first_name;

std::cout << "Enter the last name of soccer player #" << i+1 << ": ";
std::cin >> last_name;

info[i].name = first_name + " " + last_name;
Sign up to request clarification or add additional context in comments.

2 Comments

Right, I keep forgetting cin >> doesnt read after a blank space. I understand the std::getline, but could you explain what's happening in the std::cin.ignore statement?
@jon getline reads until the new line character. After entering points the new line character will be in the inputs buffer and the next getline will enter an empty string. You have to remove this character from the input buffer using member function ignore.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.