•
•
•
•
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
![]() |
•
•
Join Date: Oct 2007
Posts: 2
Reputation:
Rep Power: 0
Solved Threads: 0
Which is the most suitable source code? I need a programmer with a large experience on C++ to answer this question.
2nd:
3rd
5th
6th
Thanx for your time
#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.
•
•
Join Date: Aug 2007
Location: South Dakota
Posts: 980
Reputation:
Rep Power: 6
Solved Threads: 97
•
•
Join Date: Oct 2007
Posts: 2
Reputation:
Rep Power: 0
Solved Threads: 0
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 ?
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 ?
![]() |
•
•
•
•
•
•
•
•
DaniWeb C++ Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
- User Input: Strings and Numbers [C++] (C and C++ Tutorials)
- TSP read input file problem (C++)
- Checkbox Input Problem (ASP)
- noob char input problem (C++)
- Point to Point3D Inheritance (C++)
- C problem in strings and recursive check (C)
- TMT Pascal Input Console problem (Pascal and Delphi)
- Weird memory problem in Windows XP (Windows NT / 2000 / XP / 2003)
- Encryption of numbers.... (C)
Other Threads in the C++ Forum
- Previous Thread: Cprg
- Next Thread: Array problem in standard deviation program



Linear Mode