Code:
getline(cin, g[i].fullname)
Good evening. Getline is ignoring the input. What should I do?
Code:
#ifndef GOLF_H_
#define GOLF_H_
const int LEN = 40;
namespace sport {
struct golf {
std::string fullname;
int handicap;
};
// non-interactive version:
// function sets golf structure to provided name, handicap
// using values passed as arguments to the function
void setgolf(golf & g, const std::string name, int hc);
// interactive version:
// function solicits name and handicap from user
// and sets the members of g to the values entered
// returns 1 if name is entered, 0 if name is empty string
int setgolf(golf & g);
// function resets handicap to new value
void handicap(golf & g, int hc);
// function displays contents of golf structure
void showgolf(const golf & g);
}
#endif
Code:
#include <iostream>
#include <string>
#include <cstring>
#include "../Headers/golf.h"
namespace sport {
void setgolf(golf &g, const std::string name, int hc)
{
g.fullname = name;
g.handicap = hc;
}
int setgolf(golf &g)
{
using std::string;
using std::cout;
using std::cin;
using std::strlen;
cout << "Please enter full name: ";
cin >> g.fullname;
cout << "Please enter handicap: ";
cin >> g.handicap;
if(g.fullname.length() == 0)
return 0;
else
return 1;
}
void handicap(golf &g, int hc)
{
g.handicap = hc;
}
void showgolf(const golf &g)
{
using std::cout;
using std::endl;
cout << "Your name: " << g.fullname << endl;
cout << "Handicap: " << g.handicap << endl;
}
}
Code:
#include <iostream>
#include <string>
#include <cstring>
#include "../Headers/golf.h"
int main()
{
using std::string;
using std::cout;
using std::cin;
using std::strlen;
using sport::golf;
int players;
cout << "How many players are playing? ";
while(!(cin >> players))
{
cin.clear();
while(cin.get() != '\n')
continue;
cout << "Please enter how many players are playing: ";
}
golf *g = new golf[players];
for(int i = 0; i < players; i++)
{
cout << "Enter your full name: ";
getline(cin, g[i].fullname);
if(g[i].fullname.empty())
break;
}
delete [] g;
}