LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-08-2011, 02:17 PM   #1
EzioAuditore
Member
 
Registered: Mar 2011
Posts: 45

Rep: Reputation: 0
Question 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!
 
Old 04-08-2011, 02:28 PM   #2
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
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

Last edited by paulsm4; 04-08-2011 at 02:41 PM.
 
1 members found this post helpful.
Old 04-08-2011, 02:33 PM   #3
EzioAuditore
Member
 
Registered: Mar 2011
Posts: 45

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by paulsm4 View Post
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...
 
Old 04-08-2011, 02:52 PM   #4
EzioAuditore
Member
 
Registered: Mar 2011
Posts: 45

Original Poster
Rep: Reputation: 0
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..
 
Old 04-08-2011, 03:39 PM   #5
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Quote:
Originally Posted by EzioAuditore View Post
What could be wrong w/ my program?
We need the error messages!
 
0 members found this post helpful.
Old 04-09-2011, 10:10 AM   #6
EzioAuditore
Member
 
Registered: Mar 2011
Posts: 45

Original Poster
Rep: Reputation: 0
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..

Last edited by EzioAuditore; 04-09-2011 at 10:15 AM.
 
Old 04-09-2011, 10:14 AM   #7
Aquarius_Girl
Senior Member
 
Registered: Dec 2008
Posts: 4,731
Blog Entries: 29

Rep: Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940
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.
 
Old 04-09-2011, 10:19 AM   #8
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Quote:
Originally Posted by Anisha Kaul View Post
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.
 
Old 04-09-2011, 10:21 AM   #9
EzioAuditore
Member
 
Registered: Mar 2011
Posts: 45

Original Poster
Rep: Reputation: 0
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.
 
Old 04-09-2011, 10:39 AM   #10
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Quote:
Originally Posted by EzioAuditore View Post
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.
 
Old 04-09-2011, 11:03 AM   #11
EzioAuditore
Member
 
Registered: Mar 2011
Posts: 45

Original Poster
Rep: Reputation: 0
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..
 
Old 04-09-2011, 11:23 AM   #12
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Quote:
Originally Posted by EzioAuditore View Post
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.
 
Old 04-09-2011, 11:36 AM   #13
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
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).
 
1 members found this post helpful.
Old 04-09-2011, 12:14 PM   #14
EzioAuditore
Member
 
Registered: Mar 2011
Posts: 45

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by MTK358 View Post
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;
 }

Last edited by EzioAuditore; 04-09-2011 at 12:31 PM.
 
Old 04-09-2011, 12:17 PM   #15
EzioAuditore
Member
 
Registered: Mar 2011
Posts: 45

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by dwhitney67 View Post
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..
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
gcc error - "iostream: No such file or directory" SirTristan Linux - Software 4 06-03-2012 08:59 PM
[SOLVED] ERROR: iostream.h: No such file or directory...‘cin’ was not declared in this scope FelipeGamma Linux - Newbie 2 06-08-2010 08:02 PM
[SOLVED] iostream.h exists, yet facing "iostream.h: No such file or directory" mq15 Linux - Software 13 01-02-2010 09:13 AM
gcc error: iostream: no such file or directory linux 4.1.2 Liah Linux - Newbie 1 03-10-2009 10:53 AM
Linux iostream vs Windows iostream davidguygc Programming 2 05-13-2007 09:13 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:57 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration