Yes, you can do something like that with for loops instead of that long if
condition.
 
bool check_isbn_2(string isbn)
{
        if((isbn.size() == 13) && isalpha(isbn[12])){
            for(int i=0,c=0;(isbn.size() == 13)&& i<3 ; i++,c++){
                for(int i=0; i<3 ; i++,c++)
                        if(!(isdigit(isbn[c]))) return false;
                if(!(isbn[c]== '-')) return false;
                }return true;   
        }return false;
}
 
Cheers,
 
Firat KARAKUSOGLU
  _____  

From: [email protected] [mailto:[EMAIL PROTECTED] On Behalf Of
Robert Ryan
Sent: 24 Mart 2007 Cumartesi 00:49
To: [email protected]
Subject: [c-prog] if to for loop
 
is it possible to use a for loop with this code

#include <iostream>
using namespace std;
class Book {
string ISBN;
string title;
string author;
int copyright_date;
bool checked;
public:
Book(string isbn, string t, string a, int c, bool ch):ISBN(isbn), title(t),
author(a), copyright_date(c), checked(ch){}
string get_isbn() {return ISBN;}
string get_title() {return title;} 
string get_author() {return author;} 
int get_copyright() {return copyright_date;} 
bool get_checked() {return checked;}
};
bool check_isbn(string isbn) {
if (isbn.size() == 13) {
if (
isdigit(isbn[0]) && 
isdigit(isbn[1]) && 
isdigit(isbn[2]) &&
isbn[3] == '-' &&
isdigit(isbn[4]) &&
isdigit(isbn[5]) &&
isdigit(isbn[6]) &&
isbn[7]== '-' &&
isdigit(isbn[8]) &&
isdigit(isbn[9]) &&
isdigit(isbn[10]) &&
isbn[11] == '-' &&
isalpha(isbn[12]) ) {
return true;
}
} else return false;

}
int main() {
Book b("123-321-238-x", "The Water", "Grisham", 1980, true);
cout << b.get_isbn()<<endl;
cout << b.get_title() << endl;
cout << b.get_author() << endl;
cout << b.get_copyright() << endl;
cout << check_isbn(b.get_isbn()) << endl;
if (b.get_checked() == true)
cout << "checked out\n";
else cout << "not checked out" << endl;
}

---------------------------------
Food fight? Enjoy some healthy debate
in the Yahoo! Answers Food & Drink Q&A.

[Non-text portions of this message have been removed]
 


[Non-text portions of this message have been removed]

Reply via email to