Thread: First letter is missing

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Tears of the stars thames's Avatar
    Join Date
    Oct 2012
    Location
    Rio, Brazil
    Posts
    193

    Question First letter is missing

    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
    Last edited by thames; 12-20-2012 at 09:57 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 12
    Last Post: 11-24-2012, 04:10 AM
  2. How To Make Capital Letter To Small Capital Letter??
    By jason07 in forum C++ Programming
    Replies: 3
    Last Post: 10-10-2005, 04:37 PM
  3. Big Letter became small letter
    By cogeek in forum C Programming
    Replies: 27
    Last Post: 12-13-2004, 02:04 PM
  4. I got a letter from the IRS.
    By Cheeze-It in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 09-26-2002, 03:53 PM
  5. Letter
    By drdroid in forum C++ Programming
    Replies: 1
    Last Post: 02-27-2002, 09:12 PM

Tags for this Thread