LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   G++ --> iostream.h: No such file or directory (https://www.linuxquestions.org/questions/programming-9/g-iostream-h-no-such-file-or-directory-873824/)

EzioAuditore 04-08-2011 02:17 PM

G++ --> iostream.h: No such file or directory
 
Hello guys,

I recently switched to ubuntu, so kinda new to it. I installed g++ by

Code:

sudo apt-get install build-essential
and now when i'm trying to compile this:

Code:

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
class garments
{
char gcode[20];
char gtype[20];
int gsize;
char gfabric[20];
float gprice;
float assign()
{
if (strcmp(gfabric,"Cotton")==0)
{
if (strcmp(gtype,"Trouser")==0)
gprice=1300;
else
if (strcmp(gtype,"Shirt")==0)
gprice=1100;
else if(strcmp(gtype,"Trouser")==0)
gprice=1300-1300*0.1;
else if(strcmp(gtype,"Shirt")==0)
gprice=1100-1100*0.1
 }
return(gprice)
}
public:
void input()
{
cout<<"Enter garment;s code:";
gets(gcode);
cout<<"Entetr garment's type:";
gets(gtype);
cout<<"Entet garments size:";
cin>>gsize;
cout<<"Enter garments fabric:";
gets(gfabric);
int tprice=assign();
}
void display()
{
cout<<"Garments code";
puts(gcode);
cout<<"Garments type";
puts(gtype);
cout<<"Garments size"<<gsize;
cout<<"Garmetns fabric";
puts(gfabric);
cout<<"Total Price ="<<tprice;
}
}obs;
void main()
{
garments obs;
obs.input();
obs.display();
getch();
}

I get the error:

Code:

garment.cpp:1: fatal error: iostream.h: No such file or directory
compilation terminated.

I really want to learn all this coding especially via involving the use of command line..

Please shed some light on it LQ members.. And btw, this is Ubuntu 10.10.

Thanks!

paulsm4 04-08-2011 02:28 PM

Hi -

Please try this program:
Code:

#include <iostream>

using namespace std;

int
main (int argc,  char *argv[])
{
  cout << "Hello world" << endl;
  return 0;
}

Quote:

g++ -o tmp -Wall -pedantic tmp.cpp
./tmp
Hello world
If that works, then please consider these revisions:
Code:

#include<iostream>
#include<string>

class garments
{
  string gcode, gtype, gfabric;
  int gsize;
  float gprice;

  float
  assign()
  {
  if (gfabric.compare ("Cotton"))==0)
  {
    if (gtype.compare ("Trouser"))==0)
      gprice=1300;
    else if (gtype.compare ("Shirt"))==0)
      gprice=1100;
    else if(strcmp(gtype,"Trouser")==0)
      gprice=1300-1300*0.1;
    else if(gtype.compare ("Shirt"))==0)
      // Notice that we've already got logic problems...
      // ...Probably time to add more parenthesis!
  }
  return gprice;
  }

public:
  void
  input()
  {
    cout<<"Enter garment's code:";
    cin >> gcode;
    cout<<"Enter garment's type:";
    cin >> gtype;
    cout<<"Enter garment's size:";
    cin>>gsize;
    cout<<"Enter garments fabric:";
    cin>>gfabric;
    // Did you want "int" or "float"?  Do you want to truncate or round?  Etc...
    int tprice=(int)assign();
  }
  ...
}

Note how I substituted C++ "string" class for C-style char arrays; and substituted the C++ string "compare()" for old style "strcmp()".

Note, too, that "conio" is for DOS systems. It's totally obsolete in contemporary systems. If you need terminal control, consider a library like ncurses. If you'd like a GUI, consider a toolkit like GTK+ or Qt. And if you just need quick'n'dirty keyboard input, use "cin" and "cout".

'Hope that helps .. PSM

EzioAuditore 04-08-2011 02:33 PM

Quote:

Originally Posted by paulsm4 (Post 4318341)
Hi -

Please try this program:
Code:

#include <iostream>

using namespace std;

int
main (int argc,  char *argv[])
{
  cout << "Hello world" << endl;
  return 0;
}


Thanks for your reply,, And it worked.

What could be wrong w/ my program? I've already tried removing .h from iostream.h and adding using namespace std; to no avail...

EzioAuditore 04-08-2011 02:52 PM

Thanks again for your help. Its late night here and have school tomorrow.

Will do the revisions as you said and post the outcome..

Quote:

Note how I substituted C++ "string" class for C-style char arrays; and substituted the C++ string "compare()" for old style "strcmp()".
Yeah we weren't taught about these... atleast yet not..

Quote:

Note, too, that "conio" is for DOS systems. It's totally obsolete in contemporary systems. If you need terminal control, consider a library like ncurses. If you'd like a GUI, consider a toolkit like GTK+ or Qt. And if you just need quick'n'dirty keyboard input, use "cin" and "cout".
Yeah,, I too realised it after i posted the code but removed now..

MTK358 04-08-2011 03:39 PM

Quote:

Originally Posted by EzioAuditore (Post 4318349)
What could be wrong w/ my program?

We need the error messages!

EzioAuditore 04-09-2011 10:10 AM

Ok here's the revised code, I ran it in Turbo C++ (Windows), it ran successfully.

But in linux, issuing:
Code:

g++ garment2.cpp -o garment2exe
returns:
Code:

garment2.cpp:53: error: ‘::main’ must return ‘int’
Code:

#include<iostream>
 #include<stdio.h>
 #include<string.h>
using namespace std;
 class garments
 {
 char gcode[20];
 char gtype[20];
 int gsize;
 char gfabric[20];
 float gprice;
 float assign()
 {
 if (strcmp(gfabric,"Cotton")==0)
{
 if (strcmp(gtype,"Trouser")==0)
gprice=1300;
 else
if (strcmp(gtype,"Shirt")==0)
gprice=1100;
}
else if(strcmp(gtype,"Trouser")==0)
gprice=1300-1300*0.1;
else if(strcmp(gtype,"Shirt")==0)
gprice=1100-1100*0.1;

return(gprice);
}
 public:
 void input()
 {
 cout<<"Enter garment;s code:";
 gets(gcode);
 cout<<"Entetr garment's type:";
 gets(gtype);
 cout<<"Entet garments size:";
 cin>>gsize;
 cout<<"Enter garments fabric:";
 gets(gfabric);
 }
 void display()
 {
 cout<<"Garments code";
 puts(gcode);
 cout<<"Garments type";
 puts(gtype);
 cout<<"Garments size"<<gsize;
 cout<<"Garmetns fabric";
 puts(gfabric);
cout<<"Total Price ="<<assign();
 }
 }obs;
 void main()
 {
 garments obs;
 obs.input();
 obs.display();
 return 0;
 }

I thought its talking about void main's return type, so I tried return (int), but no luck..

Aquarius_Girl 04-09-2011 10:14 AM

You have written void main, it means it returns nothing, at the same time you have put, return 0, you need to remove either of the two. i.e.
make it int main or remove return 0.

MTK358 04-09-2011 10:19 AM

Quote:

Originally Posted by Anisha Kaul (Post 4319186)
You have written void main, it means it returns nothing, at the same time you have put, return 0, you need to remove either of the two. i.e.
make it int main or remove return 0.

No, "void main" is just plain wrong. It's just that some compilers decided to allow it.

"int main" is the proper way and adheres to the standard. And it's not like it's much harder to type.

EzioAuditore 04-09-2011 10:21 AM

Hah! What a silly error i made... :P

Anyways, when i tried compiling that w/ void main changed to int main, following error pops-up:

Code:

/tmp/ccpdzlRX.o: In function `garments::input()':
garment2.cpp:(.text._ZN8garments5inputEv[garments::input()]+0x21): warning: the `gets' function is dangerous and should not be used.


MTK358 04-09-2011 10:39 AM

Quote:

Originally Posted by EzioAuditore (Post 4319196)
Hah! What a silly error i made... :P

Anyways, when i tried compiling that w/ void main changed to int main, following error pops-up:

Code:

/tmp/ccpdzlRX.o: In function `garments::input()':
garment2.cpp:(.text._ZN8garments5inputEv[garments::input()]+0x21): warning: the `gets' function is dangerous and should not be used.


That's a warning, not an error. The program should still compile.

It's just reminding you that gets() is dangerous because if the user types in a line longer than the buffer tht you provide it, it could overwrite parts of memory that it shouldn't, and lead to bad things.

EzioAuditore 04-09-2011 11:03 AM

Oh I just ran it and its working nicely w/ the only exception that its not accepting value for Garment's fabric.. It just skips it.. Is it because of gets? But gcode, gtype also uses gets to take input and these are working fine. . .

Thanks anyways.. :)

dwhitney67 04-09-2011 11:23 AM

Quote:

Originally Posted by EzioAuditore (Post 4319227)
Oh I just ran it and its working nicely w/ the only exception that its not accepting value for Garment's fabric.. It just skips it.. Is it because of gets? But gcode, gtype also uses gets to take input and these are working fine. . .

Thanks anyways.. :)

You do not require the use of char arrays for a string; use the C++ STL string object. Read about it here.

Use std::getline() to read in a string.

MTK358 04-09-2011 11:36 AM

Even if you choose to use STL string objects, I just wanted to say this:

There's another alternative to gets(), called fgets(). It's better because it takes an additional parameter that specifies the size of the buffer. If the line is longer than the buffer, then fgets() will return as much as the buffer can hold (note that the rest of the line is not lost forever, subsequent calls to fgets() will keep returning the remaining parts of the long line until you get to the end).

But it also takes a stream parameter which gets() doesn't have. For reading keyboard input, just use "stdin" (it's predefined in cstdio).

EzioAuditore 04-09-2011 12:14 PM

Quote:

Originally Posted by MTK358 (Post 4319255)
Even if you choose to use STL string objects, I just wanted to say this:

There's another alternative to gets(), called fgets(). It's better because it takes an additional parameter that specifies the size of the buffer. If the line is longer than the buffer, then fgets() will return as much as the buffer can hold (note that the rest of the line is not lost forever, subsequent calls to fgets() will keep returning the remaining parts of the long line until you get to the end).

But it also takes a stream parameter which gets() doesn't have. For reading keyboard input, just use "stdin" (it's predefined in cstdio).

I tried stdin and this time it worked nicely... Thanks a lot mate...

However, during compilation, there were some warnings:

Code:

garment2.cpp: In member function ‘void garments::input()’:
garment2.cpp:33: error: ‘stdin’ cannot be used as a function
garment2.cpp:35: error: ‘stdin’ cannot be used as a function
garment2.cpp:39: error: ‘stdin’ cannot be used as a function

I'd like to know your (or whosoever reads it first) thoughts on these warnings.. I'm not using stdin as function anywhere then why the warning??

Here's the final code:

Code:

#include<iostream>
 #include<cstdio>
 #include<string.h>
using namespace std;
 class garments
 {
 char gcode[20];
 char gtype[20];
 int gsize;
 char gfabric[20];
 float gprice;
 float assign()
 {
 if (strcmp(gfabric,"Cotton")==0)
{
 if (strcmp(gtype,"Trouser")==0)
gprice=1300;
 else
if (strcmp(gtype,"Shirt")==0)
gprice=1100;
}
else if(strcmp(gtype,"Trouser")==0)
gprice=1300-1300*0.1;
else if(strcmp(gtype,"Shirt")==0)
gprice=1100-1100*0.1;

return(gprice);
}
 public:
 void input()
 {
 cout<<"Enter garment's code: ";
 stdin(gcode);
 cout<<"Entetr garment's type: ";
 stdin(gtype);
 cout<<"Entet garment's size: ";
 cin>>gsize;
 cout<<"Enter garment's fabric: ";
 stdin(gfabric);
cout<<"\n";
 }
 void display()
 {
 cout<<"Garments code -->";
 puts(gcode);
 cout<<"Garments type -->";
 puts(gtype);
 cout<<"Garments size -->"<<gsize<<"\n";
 cout<<"Garments fabric -->";
 puts(gfabric);
cout<<"Total Price ="<<assign();
 }
 }obs;
 int main()
 {
 garments obs;
 obs.input();
 obs.display();
 return 0;
 }


EzioAuditore 04-09-2011 12:17 PM

Quote:

Originally Posted by dwhitney67 (Post 4319242)
You do not require the use of char arrays for a string; use the C++ STL string object. Read about it here.

Use std::getline() to read in a string.

Thanks for the link mate... Nice read-up there.. :)


All times are GMT -5. The time now is 11:44 AM.