Which is the most suitable source code? I need a programmer with a large experience on C++ to answer this question.

#include <string>
#include <iostream>
#include <limits>

int main()    {
    using namespace std;
    const int THISYEAR = 2007;
    string yourName;
    int birthYear,bad_input;
    
    cout << "What's your name ? " << flush;
    getline(cin, yourName);

   
    do {
      
        bad_input=0;
        cout <<"\nWhat year were you born :" << flush ;
        cin >> birthYear;
        if(!cin.good())   
        {
          
            bad_input=1;
            cin.clear();   
           
       
            cin.ignore(numeric_limits<streamsize>::max(),'\n');
            cout << "This is not valid value" << endl;
        }
    }while(bad_input);
       
    cout << "Your name is " << yourName
         << " and you are approximately "
         << (THISYEAR-birthYear)
         << " years old. " << endl;
   
    return 0;
}

2nd:

#include <string>
#include <iostream>
#include <limits>

int main()	{
	using namespace std;
	const int THISYEAR = 2007;
	string yourName;
	int birthYear;
	
	cout << "What's your name ? " << flush;
	getline(cin, yourName);
			
	cout <<"What year were you born? " << flush;
	while(!(cin>>birthYear))	
	{
		cin.clear();	
		cin.ignore(numeric_limits<streamsize>::max(),'\n');
				
		cout << "No valid value. ";
		cout <<"What year were you born? " << flush;
	}
			
	cout << "Your name is " << yourName
		 << " and you are approximately "
		 << (THISYEAR-birthYear)
		 << " years old. " << endl;
	
	return 0;
}

3rd

#include <string>
#include <iostream>
#include <cstdlib>

int main()    {
    using namespace std;
    const int THISYEAR = 2007;
    string yourName;
    int birthYear;
    char temp[100];
   
    cout << "What's your name ? " << flush;
    getline(cin, yourName);
   
    do    {
        cout <<"What year were you born? " << flush;
        cin.getline(temp,100);   
             
        birthYear=atoi(temp);
        if (birthYear==0)    {
            cout << "Invalid valude..." << flush;
        }
    } while(birthYear==0);
   
   

       
    cout << "Your name is " << yourName
         << " and you are approximately "
         << (THISYEAR-birthYear)
         << " years old. " << endl;
   
    return 0;
}

5th

#include <string>
#include <iostream>
#include <sstream>       // for istringstream convert()
using namespace std;

int main()
{
    const int THISYEAR = 2007;
    string yourName;
    int birthYear;
    string birth;               //για το έτος γέννησης
   
    cout << "What's your name ? " << flush;
    getline(cin,yourName);
    for(;1;)    {
             cout << "What year were you born? ";
             getline(cin,birth);
             // μετατροπή string -----> int
             istringstream convert(birth);
             // τοποθέτηση στην int birthYear
             convert >> birthYear;
                 // αν δεν πετύχει σημαίνει ότι δεν είναι αριθμός
                 if(!convert    {
                    cout << " This is not valid.Try again...." << endl;
                     continue;
                } else {
                    break;
                }
    }
   
    cout << "Your name is " << yourName
             << " and you are approximately "
             << (THISYEAR-birthYear)
             << " years old. " << endl;
   
  
   
    return 0;
}

6th

#include <string>
    #include <iostream>
    #include <cstdlib>        //Για να κάνεις calls σε commands από το prompt π.χ. την PAUSE, επίσης έχει την atoi
    #pragma argsused

    //Ελέγχει εαν το string περιέχει αριθμό
    // εαν ναι επιστρέφει true
    // αλλιώς επιστρέφει false
    bool CheckStr(const std::string& s)
    {
       for (int i = 0; i < s.length(); i++) {
           if (!std::isdigit(s[i]))
    		   return false;
       }
       return true;
    }

    //Παίρνει το όνομα
    void GetName(std::string& yourName)
    {
    	using namespace std;
    	cout << "What's your name ? " << flush;
    	getline(cin, yourName);
    }

    //Παίρνει την ημ. γέννησης
    void GetBirthdate(std::string& birthYear)
    {
    	using namespace std;
    	cout << "What year were you born? ";
    	getline(cin, birthYear);
    }

    int main(int argc, char* argv[])
    {
    	using namespace std;
    	const int THISYEAR = 2007;
    	string yourName;
    	string birthYear;
    	int intYear;
    	bool exit = false;

    	//Μέχρι να δώσει σωστή ημ. ο χρήστης το πρόγραμμα κάνει loop
    	while (!exit)
    	{
    		GetName(yourName);			//Παίρνει το όνομα
    		GetBirthdate(birthYear);	//Παίρνει την ημ. γέννησης


    		if ( CheckStr(birthYear) ) { 	//Εάν η ημερομηνία είναι σωστή
    			  intYear = atoi(birthYear.c_str() ); 	//Μετατρέπει το string σε integer
    			  cout << "Your name is " << yourName
    				<< " and you are approximately "
    				<< (THISYEAR-intYear)
    				<< " years old. " << endl;
    				exit = true;
    		}
    		else {
    			  cerr << "This is not valid."
    				<< " Try again..." << endl;
    		}
    	}

    	system("PAUSE");
    	return 0;
    }

Thanx for your time

I prefer #3.

Are we grading a contest or homework?

Val

Basically your are not grading. Just advising a beginner in C++. I am trying to find out one way that is used universally among the C++ scripts, for checking the input stream.

So I found two ways.
1 --> using cin.good() , cin.clear() and cin.ignore()
2 --> using a string variable followed by a conversion to an integer.

I think that the 2nd method is not very good (eg you can't check for buffer overflow). Also you need more RAM....

So i prefer the 1st one. Do you ?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.