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-09-2011, 01:38 PM   #16
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721

Quote:
Originally Posted by EzioAuditore View Post
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 View Post
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 01:47 PM. Reason: Added error/warning explanation
 
Old 04-09-2011, 02:05 PM   #17
EzioAuditore
Member
 
Registered: Mar 2011
Posts: 45

Original Poster
Rep: Reputation: 0
I'm a little bit confused... Can u pls post a corrected version of my code?
 
Old 04-09-2011, 02:15 PM   #18
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721
Quote:
Originally Posted by EzioAuditore View Post
I'm a little bit confused...
What exactly are you confused about?
 
Old 04-09-2011, 02:46 PM   #19
EzioAuditore
Member
 
Registered: Mar 2011
Posts: 45

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

Rep: Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721
Quote:
Originally Posted by EzioAuditore View Post
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?

Last edited by MTK358; 04-09-2011 at 04:49 PM.
 
Old 04-09-2011, 06:26 PM   #21
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,533

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Quote:
Originally Posted by MTK358 View Post
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 06:28 PM.
 
Old 04-09-2011, 06:59 PM   #22
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721
Quote:
Originally Posted by dwhitney67 View Post
After the fabric size is inputted (an int), a newline character still remains in the input stream.
I suspected that, but why does it only affect the last prompt, and not the two before it?
 
Old 04-09-2011, 07:08 PM   #23
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,533

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Quote:
Originally Posted by MTK358 View Post
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;
}
 
Old 04-10-2011, 02:16 PM   #24
EzioAuditore
Member
 
Registered: Mar 2011
Posts: 45

Original Poster
Rep: Reputation: 0
Well thank you guys.... Its all clear now.. Very much appreciate the help..
 
  


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 09: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 09:02 PM
[SOLVED] iostream.h exists, yet facing "iostream.h: No such file or directory" mq15 Linux - Software 13 01-02-2010 10:13 AM
gcc error: iostream: no such file or directory linux 4.1.2 Liah Linux - Newbie 1 03-10-2009 11:53 AM
Linux iostream vs Windows iostream davidguygc Programming 2 05-13-2007 10:13 PM

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

All times are GMT -5. The time now is 02:38 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration