User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 455,970 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,773 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 749 | Replies: 3 | Solved
Reply
Join Date: Oct 2007
Posts: 2
Reputation: blackslash13 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
blackslash13 blackslash13 is offline Offline
Newbie Poster

Input check problem with numbers

  #1  
Nov 25th, 2007
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
Last edited by blackslash13 : Nov 25th, 2007 at 12:48 pm.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Aug 2007
Location: South Dakota
Posts: 980
Reputation: vmanes is a jewel in the rough vmanes is a jewel in the rough vmanes is a jewel in the rough vmanes is a jewel in the rough 
Rep Power: 6
Solved Threads: 97
vmanes's Avatar
vmanes vmanes is offline Offline
Posting Shark

Re: Input check problem with numbers

  #2  
Nov 25th, 2007
I prefer #3.

Are we grading a contest or homework?

Val
I am in mourning for my country.
Reply With Quote  
Join Date: Oct 2007
Posts: 2
Reputation: blackslash13 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
blackslash13 blackslash13 is offline Offline
Newbie Poster

Re: Input check problem with numbers

  #3  
Nov 26th, 2007
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 ?
Reply With Quote  
Join Date: Dec 2005
Posts: 3,834
Reputation: Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of 
Rep Power: 23
Solved Threads: 436
Colleague
Salem's Avatar
Salem Salem is offline Offline
banned

Re: Input check problem with numbers

  #4  
Nov 26th, 2007
I prefer number 5
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C++ Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 9:11 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC