GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 03-Dec-2005, 22:11
fryup fryup is offline
New Member
 
Join Date: Dec 2005
Posts: 3
fryup is on a distinguished road
Question

C++ console programming using cbuilder4


I have a problem with C++ console programming using cbuilde4.

I input data through overloaded operators of istream and write to a file through ofstream. Its working alright without validations.
I have a problem of how to validate inputs of numeric fields and get back to the istream and write to the file.

See below a part of code where I have tried this and relavent coding.

If any body can help on this thats great.

Thanks in advance.

CPP / C++ / C Code:
istream& operator>> (istream& instr, const Details& details){
details.inputData(instr); 
}

stream& Details::inputData(istream& instr){
int keyval=0;

cout << "ID Number : " << idNumber<<endl;
cout << "Name : ";
instr.ignore();
instr.getline(name,19);
cout << "Address1 : ";
instr.getline(address1,19);
cout << "Address2 : ";
instr.getline(address2,19);
char *inputString;
cout << "Postal Code : ";

//validate numeric field postalCode ***************?????

for (int i=0; i<8;i++){
keyval = getch();
if (keyval==13){ i=8;}
cout<<(char)keyval;

if(isdigit(keyval))
{
inputString[i]==(char)keyval;
}
}

*inputString >> postalCode ; //????problem is here

cout <<endl<< "City : ";
instr.ignore();
instr.getline(city,19);
cout << "Fax No : ";
instr >> fax ;
instr.ignore();

return instr;
}
Last edited by LuciWiz : 04-Dec-2005 at 07:21. Reason: Please insert your C++ code between [c++] & [/c++] tags
  #2  
Old 03-Dec-2005, 23:32
Paramesh's Avatar
Paramesh Paramesh is offline
Regular Member
 
Join Date: Sep 2005
Location: The Milky Way
Posts: 929
Paramesh is a jewel in the roughParamesh is a jewel in the roughParamesh is a jewel in the rough

Re: C++ console programming using cbuilder4


Hi fryup,

Welcome to GIDForums.

If you can provide your class Details, that would be nice.
I cant guess what you are doing now.

This:
CPP / C++ / C Code:
*inputString >> postalCode ; //????problem is here
is certainly wrong.

To validate postalCode, you can use fail, clear and ignore. Like this:
CPP / C++ / C Code:
do
{
        instr >> postalCode;

        if( instr.fail( ) )         //if invalid input is given, get out of the loop.
        {
            instr.clear();           //return the cin stream to good state
            instr.ignore(10, '\n');  //Remove from the stream the input that caused the problem
            cout<<"Enter Numbers only...";
        }
        else
            break;         //If the input is ok, break the loop.
}while(1);
Suppose the user enters a character instead of a number, it will ask again for input.
Else, it will break the loop.

Regards,
Paramesh.
__________________

Don't walk in front of me, I may not follow.
Don't walk behind me, I may not lead.
Just walk beside me and be my friend.
  #3  
Old 04-Dec-2005, 02:37
fryup fryup is offline
New Member
 
Join Date: Dec 2005
Posts: 3
fryup is on a distinguished road

Re: C++ console programming using cbuilder4


Hi Paramesh,

Thanks for your advice. I tried your coding. stream.fail() works, as clear() and ignor() not working it gives an endless loop. Anyway I feel you can help me.
Have to find a way to go back to the original cursor position to input postal code. I will send the class below. If you need futher details I am ready to provide.

Thanks

CPP / C++ / C Code:
----------------------------------------------------------------------
// Details class has all the common attributes and methods to other classes down the hierarchy. This class act as a base class to other classes.
//------------------------------------------------------------------------#ifndef DetailsH
#define DetailsH
#include <string.h>
#include <iomanip.h>
#include <iostream.h>
#include <fstream.h>
#include <ostream.h>
#include <istream.h>
#include <strstream.h>
#include <conio.h>
#include <sstream.h>

class Details{
    private:

        int idNumber;
        char name[20];
        char address1[20];
        char address2[20];
        long postalCode;
        char city[20];
        long fax;
    protected:
      virtual istream& inputData(istream&);
      virtual ostream& display(ostream&);
      virtual ofstream& writeFile(ofstream&);
      virtual ifstream& readFile(ifstream&);

    public:

        Details(int, char*, char*, char*, long, char*, long);
        Details(){};
        setID(int id){idNumber= id;};
        int getID(){return idNumber;};
        char* getName(){return name ;};
        virtual ostringstream& fields(ostringstream&);
        virtual ~Details(){};

        friend istream& operator >> (istream&, const Details&);
        friend ostream& operator << (ostream&, const Details&);
        friend ofstream& operator << (ofstream&, const Details&);
        friend ifstream& operator >> (ifstream&, Details&);
      //  char* validateInput();
};
Last edited by LuciWiz : 04-Dec-2005 at 07:22. Reason: Please insert your C++ code between [c++] & [/c++] tags
  #4  
Old 04-Dec-2005, 05:08
Paramesh's Avatar
Paramesh Paramesh is offline
Regular Member
 
Join Date: Sep 2005
Location: The Milky Way
Posts: 929
Paramesh is a jewel in the roughParamesh is a jewel in the roughParamesh is a jewel in the rough

Re: C++ console programming using cbuilder4


Hi,

clear() and ignore didnt give me any error, nor any endless loop.
But it has a disadvantage:
If the input is "abc", it works.
But if the input is "123adf", it treats 123 as postal code and adf as city.
Do you understand?

So, lets forget clear, ignore and all that things.

Lets create our own validation code.
First, we'll create a string, as you had earlier. say:
CPP / C++ / C Code:
        char inputString[30];
Note the difference between *inputString and inputString[30].
The former is not initialised, and just a pointer, whereas the latter is initialized to 30 characters.

Next, we'll have a status variable, say :
CPP / C++ / C Code:
bool status;
This indicates whether the input is valid or not.
Initially, the status is false, indicating that there is no error in input.

Next, we'll have a do-while loop, to get the input again if it fails.
So, here is the format:
CPP / C++ / C Code:
        do
        {
            status = false;

            // some code here....

        }while(status == true);

Then, inside the do while, loop we are getting the input by using getline function, like this:
CPP / C++ / C Code:
            cout << "Postal Code : ";
            instr.getline(inputString, 29);
So, we have got the inputString.

We should now check whether all the characters in the string are numbers.
So, in a loop, if inputString[i] is less than '0' or if inputString[i] is greater than '1', put the status as true, and break the inner loop.
So, this will take care of invalid input.

But how to convert the string to a long?
we can use the atol function, which is readily available.
Like this:
CPP / C++ / C Code:
        details.postalCode = atol(inputString);

So, here is the prototype:
CPP / C++ / C Code:
        char inputString[30];
        bool status;
        do
        {
            status = false;
            cout << "Postal Code : ";
            instr.getline(inputString, 29);

            for(int i = 0; /* CHECK SOME CONDITION */; i++)
                if(/* CHECK SOME CONDITION */)
                {
                    cout<<"Invalid entry..."<<endl;
                    status = true;
                    break;
                }
          
        }while(status == true);

        details.postalCode = atol(inputString);

Ah! I forgot to tell you one thing:
You should not just use the class variables like this, inside the function:
CPP / C++ / C Code:
instr.getline(name,19);
// OR like this
instr.getline(address1,19);
// OR like this
instr.getline(address2,19);
This should be changed to:
CPP / C++ / C Code:
        instr.getline(details.name,19);

        instr.getline(details.address1,19);

        instr.getline(details.address2,19);

And, when you are including iostream, there is no need to include istream and ostream separately.
iostream is the combination of istream and ostream.

And, instead of using another method to get the input, like this:
CPP / C++ / C Code:
friend istream& operator >> (istream&, const Details&);
// AND:
istream& Details::inputData(istream& );
You can write all the code in the former function itself.
There is no need for the inputData function! Just remove it.

BTW, which compiler are you using?

Regards,
Paramesh.
__________________

Don't walk in front of me, I may not follow.
Don't walk behind me, I may not lead.
Just walk beside me and be my friend.
  #5  
Old 04-Dec-2005, 07:07
fryup fryup is offline
New Member
 
Join Date: Dec 2005
Posts: 3
fryup is on a distinguished road

Re: C++ console programming using cbuilder4


Hi dear,

Thats great your explanations. I could do the problem well. I appreciate your comments regarding my codings.

Thank you so much.
 
 

Recent GIDBlogToyota - 2009 May Promotion by Nihal

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Read/ Write EXCEL files using C/C++ programming confused_pig C++ Forum 4 25-Nov-2005 01:27
Bloodshed Dev C++ Project Options JdS C++ Forum 6 11-Nov-2005 18:23
[Tutorial] GUI programming with FLTK dsmith FLTK Forum 10 03-Oct-2005 16:41
Network programming pointer C++ Forum 2 20-May-2005 15:36
GUI programming crystalattice C++ Forum 5 14-Sep-2004 13:17

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 15:39.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.