-5

first of all thnx for trying to help me.....this is a part of my program......i want to know how to put validations while inputting the data in main()....thnx

#include<stdio.h>
#include<iostream.h>
#include<conio.h>
  int i;

 class product           //start of class
    {


            int itemno;
            char name[100];
            char itemtype[50];
            float price;
            float quantity;
            float total;


            public:

            void addprod() ;
            void calculate();
            void accept();
            void display()   ;




     }    ;                 //end of class




     void product::addprod()   //starting of addproduct()
        {
            cout<<"enter the serial number";
            cin>>itemno;

            cout<<"enter the name of the poduct:";
            gets(name)   ;

            cout<<"enter its type:";
            gets(itemtype);

            ***cout<<"enter its price:";
            cin>>price;**
        }*                                       //end of addproduct()



     void product::accept()           //starting of accept()
     {
            cout<<"enter the item name:";
            gets(name)  ;


            cout<<"enter the quantity:";
            cin>>quantity;

     }




     void    product::calculate()
        {
                    total=price*quantity;
         }



     void product::display()
        {
                cout<<"\nName";
                cout<<name;

                cout<<"\nPrice";
                cout<<price ;
                cout<<"\nquantity";
                 cout<<quantity;
                 cout<<"\ntotal\n\n\n\n\n";
                cout<<total;

        }





        void main()
        {
         int ch;
         product s1[3];

         a:

         cout<<"\n      1.      Add product one by one";
         cout<<"\n     2.      Add products in bulk";
         cout<<"\n     3.      Make Bill";
         cout<<"\n     4.      Display Bill";
         cout<<"\n     0.      Exit";
         cout<<"\n     Enter your choise(1,2,3,9)"     ;
         cin>>ch;


         switch(ch)
         {

         **case 1:          cout<<"\n press n to exit\n\n";
                                char con='y';
                                while(con=='y')
                                {
                                s1[i].addprod();
                                i++;
                                cout<<"do u wanna continue(y/n)";
                                cin>>con;
                                        if(con=='n')
                                        {
                                        goto a;
                             }
                                }
                                break;
            }**

This is my school project so need help as soon as possible. like if a person enters a character(a,b,c) so what should i do to make him aware its a wrong input and asks the user to input the correct form

Jonathon Reinhart
  • 132,704
  • 33
  • 254
  • 328
rahul yadav
  • 23
  • 1
  • 7
  • 8
    Start by using a conforming signature for `main`, not using `goto`, and making the code formatting readable. – chris Jan 05 '13 at 05:33
  • 2
    Please give a specific question. SO is not a "dump your homework and run" site. – Linuxios Jan 05 '13 at 05:33
  • 1
    And please fix your indentation. – Linuxios Jan 05 '13 at 05:33
  • 1
    I also see a compiler error in the case label that you'll have to resolve before tackling working validation. – chris Jan 05 '13 at 05:35
  • 1
    -1 no effort in presentation – Cheers and hth. - Alf Jan 05 '13 at 05:35
  • Sorry friends but in my school i havent been thought classes or structures at all so i am doing and learning it all by myself – rahul yadav Jan 05 '13 at 05:36
  • 2
    @rahulyadav No one mentioned using a `class` or `struct`. They're saying your code is ugly. – Jonathon Reinhart Jan 05 '13 at 05:38
  • Chris i am new to C++ so i did not understood your first comment – rahul yadav Jan 05 '13 at 05:39
  • hi Jonathon Reinhart....what are the errors in my code pls may u explain – rahul yadav Jan 05 '13 at 05:40
  • 3
    @rahulyadav There are too many errors to fix all at once. If you are learning by yourself, you might want to start over with a better book. The headers such as `` and `` are obsolete; they indicate your learning resources are outdated. See http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list – Potatoswatter Jan 05 '13 at 05:46
  • 1
    A conforming signature for `main` is `int main()` or `int main(int, char **)`. `void main()` is not included in that. `goto` can always be replaced by something else. Very rarely is there a situation where you'd actually use `goto` to make the code better instead of worse. The situation does exist, but this is not it. Lastly, the code formatting thing should be answered when it looks readable. That includes proper indentation instead of things being at a somewhat random horizontal position. – chris Jan 05 '13 at 05:49
  • and are in my syllabus so i need to work on them only – rahul yadav Jan 05 '13 at 05:49
  • 2
    @rahulyadav, They shouldn't be. Neither of those headers is part of the C++ standard library. It does have ``, but `conio.h` is Windows-specific, and deprecated at that. If I was stuck using tools that old, I would take it up with whoever is in charge of software upgrades. – chris Jan 05 '13 at 05:50
  • @chris i use Turbo C++ by borland....i know conio.h is borland specific – rahul yadav Jan 05 '13 at 05:53
  • 1
    @rahulyadav Guess what compiler is not used by any employers or software professionals. – Potatoswatter Jan 05 '13 at 05:56
  • @Potatoswatter, Hard to believe C++Builder came out of the same company. It looks much better than Turbo. – chris Jan 05 '13 at 05:58

1 Answers1

-2

You can test for input success with an if:

if (cin >> ch)
    ...

To ask the user to enter input again, you'll need a loop, and you also need to call cin.clear() to restore the stream's state:

cout << "\n     Enter your choice(1,2,3,9)":

cin >> ch;
while (!cin)
{
    cout << "Invalid input. Please try again." << endl;

    cin.clear();
    cin >> ch;
}

That's how you'd handle the first input item in main. You can do something similar for the others.

user1610015
  • 6,561
  • 2
  • 15
  • 18
  • cin.clear() uses which header file – rahul yadav Jan 05 '13 at 06:14
  • Well it's a member of **cin**, so obviously it's declared in the same header where **cin** (or **basic_istream** actually) is declared. Just include iostream or iostream.h as you have done. – user1610015 Jan 05 '13 at 06:57