Continuing the golf odyssey, the program is not printing the first letter.
Code:
#include <iostream>
#include <string>
#include <cstring>
#include "../Headers/golf.h"
int main()
{
using std::cout;
using std::cin;
using std::strlen;
using std::string;
using sport::showgolf;
using sport::setgolf;
using sport::golf;
string fullname;
int players, handicap;
cout << "How many players are playing? ";
while(!(cin >> players))
{
cin.clear();
while(cin.get() != '\n')
continue;
cout << "Please enter how many players are playing: ";
}
cin.get();
golf *golfers = new golf[players];
for(int i = 0; i < players; i++)
{
cout << "Enter your full name: ";
if(cin.get() == '\n')
break;
getline(cin, fullname);
cout << "Enter the handicap: ";
(cin >> handicap).get();
setgolf(golfers[i], fullname, handicap);
}
for(int i = 0; i < players; i++)
{
showgolf(golfers[i]);
}
}
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;
if(cin.get() == '\n')
return 0;
cout << "Please enter handicap: ";
cin >> g.handicap;
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:
#ifndef GOLF_H_
#define GOLF_H_
#include <string>
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:
How many players are playing? 2
Enter your full name: thames
Enter the handicap: 6
Enter your full name: quack
Enter the handicap: 8
Your name: hames
Handicap: 6
Your name: uack
Handicap: 8