Need help in C++ program


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: Need help in C++ program

  1. #1
    Join Date
    May 2005
    Posts
    50

    Need help in C++ program

    :mad:
    I was trying a simple program but it doesn't work can someone help me

    here it is:
    ----------------------------------------------------------------------


    #include <iostream.h>


    void main(void)
    {

    int a;
    int b;
    int h[10];
    int i;



    struct hi {
    int name[100];
    int id;
    } ss[10];

    for(i = 0; i < 10;i++)
    {
    cout << " ID: " << endl;
    cin >> a;
    cin.ignore(250, '\n');

    cout << " Name:" << endl;
    cin.getline(ss.name);
    if(!cin){
    cerr << " ERORR " << endl;

    }
    }
    }

  2. #2
    Join Date
    May 2005
    Posts
    50

    help

    Please help me

  3. #3
    Join Date
    Dec 2003
    Posts
    3,366
    //#include <iostream.h> //this is a C header
    #include <iostream>
    using namespace std;

    //void main(void) //this is not legal, no matter what your compiler says
    int main()
    {
    //these names are totally meaningless. Try to give names that let
    //the reader understand what is happening.
    //and you didnt use most of them.
    int a;
    int b;
    int h[10];
    int i;

    struct hi
    {
    char name[100]; //char not int, or wide char if you like
    int id;
    }; //this is C style to put things here.

    hi ss[10]; //better, "type variable" C++ style variable creation just like "int x"

    for(i = 0; i < 10;i++)
    {
    cout << "Enter the ID: " << endl;
    cin >> ss[i].id;

    cout << "Enter the Name:" << endl;
    cin.getline(ss[i].name); //this was the 'big' problem -- you were trying to
    //read into it without the array dereference

    if(cin.fail()) //I think this is what you wanted
    {
    cerr << " ERORR " << endl;
    }
    }
    return 0; //0 means no error to the OS
    }
    Last edited by jonnin; 05-21-2005 at 12:57 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center