[SOLVED] G++ --> iostream.h: No such file or directory
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
However, during compilation, there were some warnings:
Those are errors, not warnings. There is a very big difference between them that you have to understand:
Errors are fatal, they mean that there's a blatant error that makes the code impossible to compile.
Warnings are about things that do not stop the compiler from working, but indicate bad coding practice or things that might cause subtle errors later on. In many cases they can be ignored, but it's considered best to get rid of them.
They're easy to tell apart: g++, for example, says "error:" or "warning:" in lines that indicate an error or warning, respecively.
Quote:
Originally Posted by EzioAuditore
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
Where did you get the idea that stdin was a function? It's just a varable declared in cstdio.
I said that fgets() is a function, and you should tell it to use the stdin stream. The reason it has a stream parameter is becasue you can open a file and pass that as a stream, so fgets() is not limited to keyboard input.
gets() is hard-coded to use stdin.
For more info, see the manual page:
Code:
$ man 3 gets
Last edited by MTK358; 04-09-2011 at 12:47 PM.
Reason: Added error/warning explanation
All these stdin and fgets... I don't understand why gets() is not taking input (skipping) in case of gfabric but does in other variables..
Did you ignore everything I said about stdin and fgets()? This is how it's done:
Code:
char text[100];
fgets(text, 100, stdin);
For someone else to read it you have to format it much nicer. Use either a tab or 4 spaces for each nesting level, put spaces between operators in expressions, and add empty lines where it makes sense so that it doesn't look like a big, impenetrable block of text. This is much better:
Code:
#include <iostream>
#include <string>
class Garment //Class names are usually capitalized. Also, since this class seems to represent one garment, not many, I removes the "s".
{
public:
void input()
{
std::cout << "Enter garment's code: ";
std::cin >> gcode;
std::cout << "Entetr garment's type: ";
std::cin >> gtype;
std::cout << "Entet garment's size: ";
std::cin >> gsize;
std::cout << "Enter garment's fabric: ";
std::cin >> gfabric;
std::cout << std::endl << std::endl;
}
void display()
{
std::cout << "Garment's code: " << gcode << std::endl <<
"Garment's type: " << gtype << std::endl <<
"Garment's size: " << gsize << std::endl <<
"Garment's fabric: " << gfabric << std::endl <<
"Total Price: " << assign() << std::endl;
}
private:
float assign()
{
if (gfabric == "Cotton")
{
if (gtype == "Trouser")
gprice = 1300;
else
gprice = 1100;
}
else if(gtype == "Trouser")
gprice = 1300 - (1300 * 0.1);
else if(gtype == "Shirt")
gprice = 1100 - (1100 * 0.1);
return gprice;
}
std::string gcode;
std::string gtype;
int gsize;
std::string gfabric;
float gprice;
}; // obs; I've commented this out so it should compile
int main()
{
Garment obs; // ERROR: you already created a variable named "obs" (see the closing brace of your class)
obs.input();
obs.display();
return 0;
}
Just one glitch which I can't figure out: it just skips over the fabric type prompt as if you typed nothing and pressed Enter. Maybe someone else can help?
Just one glitch which I can't figure out: it just skips over the fabric type prompt as if you typed nothing and pressed Enter. Maybe someone else can help?
After the fabric size is inputted (an int), a newline character still remains in the input stream. Thus when it is time to read the response for the fabric type, the cin operator finds a valid input response (a newline character), and hence giving the illusion that the prompt is skipped over.
There are several ways to rectify the problem, but of course understanding the problem is most important! A simple way to fix the problem is using ignore().
Code:
...
std::cout << "Entet garment's size: ";
std::cin >> gsize;
// flush up to 1024 characters in the stream until a newline is found; remove the newline too.
std::cin.ignore(1024, '\n');
std::cout << "Enter garment's fabric: ";
std::cin >> gfabric;
...
A more complex way to verify that the proper input has been provided, is to use a "getter" function that assures that the appropriate data type is returned; for example:
Code:
#include <string>
#include <iostream>
#include <limits>
template <typename T>
T getInput(const char* prompt)
{
using namespace std;
T input;
bool done = false;
while (!done) {
// prompt user and attempt to read input
cout << prompt;
cin >> input;
// check if valid input given; break from loop if so.
if (cin.peek() == '\n' && cin.good())
{
done = true;
}
else
{
// user gave bad input; let them know about it.
cout << "Invalid input! Try again...\n" << endl;
cin.clear();
}
do { cin.ignore(numeric_limits<streamsize>::max(), '\n'); } while (!cin);
}
return input;
}
int main(void)
{
char ch = getInput<char>("Enter a char: ");
std::string str1 = getInput<std::string>("Enter a string: ");
int num = getInput<int>("Enter a number: ");
std::string str2 = getInput<std::string>("Enter another string: ");
std::cout << "Your char is : " << ch << std::endl;
std::cout << "Your string is : " << str1 << std::endl;
std::cout << "Your number is : " << num << std::endl;
std::cout << "Your other string is: " << str2 << std::endl;
}
Last edited by dwhitney67; 04-09-2011 at 05:28 PM.
I suspected that, but why does it only affect the last prompt, and not the two before it?
Simple; in those two cases, a string was being read, not an int, float, or double. Chalk it down to one of the quirks of the C++ language.
Some folks use getline() to get the entire line (up to the newline, which is read/flushed), and then stringstream to obtain the appropriate value based on the desired type. For example:
Code:
std::string input;
int value;
std::cout << "Enter a number: ";
std::getline(std::cin, input);
std::stringstream ss(input);
ss >> value;
if (ss.fail())
{
std::cerr << "Error with input!" << std::endl;
}
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.