Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
Can someone explain to me these issues I have with iostream?
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Portage & Programming
View previous topic :: View next topic  
Author Message
Rucker
n00b
n00b


Joined: 30 Oct 2002
Posts: 25

PostPosted: Tue Jan 28, 2003 5:26 pm    Post subject: Can someone explain to me these issues I have with iostream? Reply with quote

I am doing a problem for an assignment in school that uses two structs:

Code:

struct Address
{
   char city[15];
   char state[3];
};

struct EmployeeInfo
{
   char name[20];
   int age;
   float payrate;
   Address address;
};


I also have a function that inputs data into an array of three EmployeInfos:

Code:

void Initialize(EmployeeInfo Employees[])
{
   for(int x = 0; x < 3; x++)
   {
      cout << "Enter name for employee " << x + 1 << ": ";
      cin.getline(Employees[x].name, 20);
      cout << "Enter age for employee " << x + 1 << ": ";
      cin >> Employees[x].age;
      cout << "Enter payrate for employee " << x + 1 << ": ";
      cin >> Employees[x].payrate;
      cin.ignore();
      cout << "Enter city for employee " << x + 1 << ": ";
      cin.getline(Employees[x].address.city, 15);
      cout << "Enter state for employee " << x + 1 << ": ";
      cin.getline(Employees[x].address.state, 3);
   }
}


When I include iostream this way:
#include <iostream>
using namespace std;


then everything works as expected.

If instead I simply do #include <iostream.h> then I get this:

Quote:
Enter name for employee 1: Mike Rucker
Enter age for employee 1: 20
Enter payrate for employee 1: 10
Enter city for employee 1: Cottage Grove
Enter state for employee 1: MN
Enter name for employee 2: Enter age for employee 2:


To get it to work correctly I have to put a cin.ignore(); statement as the last line of the for loop.

Can someone explain to me what causes this difference in behavior?
Back to top
View user's profile Send private message
ryan83vt
Guru
Guru


Joined: 28 Oct 2002
Posts: 370
Location: Blacksburg, VA

PostPosted: Tue Jan 28, 2003 11:00 pm    Post subject: Reply with quote

all the .h headers are no longer in the standard.

what used to be #include<iostream.h> is now #include<iostream> and the code is put in a namespace (namely, std).

A namespace makes it so that you only get the code you want. It groups code together into a unit. Before, if you said #include<iostream.h> you get the code for the WHOLE include file. Now, you can just say
Code:

#include<iostream>
using std::cin;
using std::cout;

Alternatively,
Code:

#include<iostream>
.....
.....
//somewhere in your program
std::cin>>a_variable;
std::cout<<"hello world";
//this way, however, you must prefix everything from iostream inside the namespace with std::

and you will only get the code for cin and cout. You can also do what you were doing and use
Code:

using namespace std;

and this will include all the code in the namespace called std.

I hope this answers your question. Basically, don't use iostream.h or any other .h header. Many of the old c-style headers (ie math.h) have been converted also -- so look for those by using #include<cmath> (c + the old name)

Happy coding!
Back to top
View user's profile Send private message
BigRedDot
n00b
n00b


Joined: 29 Oct 2002
Posts: 67
Location: Austin

PostPosted: Tue Jan 28, 2003 11:10 pm    Post subject: Reply with quote

Quote:
all the .h headers are no longer in the standard.


This implies that they were ever in the standard to begin with; they were not.
_________________
Even if you are one-in-a-million, there are still 6000 people just like you.
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Portage & Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum