-
Newbie Mistake?
Seems that I can not get this code to work correctly. I've looked over it many times but forever whatever reason. I'm still unable to get this code to run all the way though and output the last part of the code. It seems to happen to everyone of my simple little programs I make. Can someone please assist me, and tell me what I am doing wrong?
Code:
// Simple program to figure out the size of a volume of a box...
#include <vcl.h>
#include <iostream>
#pragma hdrstop
#pragma argsused
using namespace std;
int main() {
double side1, side2, square;
cout << "Enter the frist side of the square: ";
cin >> side1;
cout << "Now enter side 2: ";
cin >> side2;
square = (side1 * side2);
cout << "Square total is: " << square;
return 0;
}
-
It looks ok to me. Are you sure its not just exiting before you get to see the output? Maybe try putting: cin.ignore(); before your return 0.
-
How does it not work? Are you running it from the command line? You might want to remove the #include <vcl.h> as you do not need it, and the pragmas probably are not necessary too.
By the way, it looks like you want to calculate the area of a rectangle, since the length of the sides of a square are the same ;)
-
Well, it seems that I am unable to get that to work either, tried putting in cin.ignore(); ... However that still didnt work, it seems that the Prompt is just closing out befor i see what the output is, the answer to your question.
*Cries*
-
Okay, well maybe you could replace cin.ignore() with cin >> side1; and see if that works?
-
Ok, so every time you do "cin >> something;" you need to follow it by "cin.ignore();", and then finish your application with "cin.get();" to make it wait for a key-press.
Or just run your application in a command prompt in the first place.
--
Mats
-
might the #pragma hdrstop be keeping the changes from being saved?
-
Looks like adding the cin.get(); cin.ignore(); took care of the issue all together... Unsure what the "#pragma hdrstop" is really for to be honest. I tried taking it outta the code and the C++ Builder I am using just gives me an error so I am not sure if that is the reason i get an error or what.
Ahh who knows, i am just happy now that I am finally able to see the end result of my program.
Thanks everyone!
-
#pragma hdrstop is for precompiled headers. You really don't need that except to speed up the compile time of very large projects.
#pragma argsused probably affects the warnings BC++ emits.
-
u need to put cin.ignore(2); and then you will be able to see the results before the complier shuts it down. put it before the return 0; and it should work. and if you know you wont be using decimals then use int instead.
-
>u need to put cin.ignore(2); and then you will be able to see the results before the complier shuts it down.
Unless there are 3 extra characters. Or 4. Or 5. Where do you want to stop? It's better to avoid this problem entirely by using std::getline and std::string for all of your input needs and then converting using something like std::stringstream or boost::lexical_cast<>. However, if you hit this problem, the conventional solution is this:
Code:
#include <iostream>
#include <ios> // For streamsize
#include <limits> // For numeric_limits<>
int main()
{
// ...
std::cin.clear();
std::cin.ignore ( std::numeric_limits<std::streamsize>::max(), '\n' );
std::cin.get();
}
These three lines clear the stream state so that you can read from the stream (if the stream is in a failure state, any input requests will fail), then remove all of the characters in the stream up to a newline, and finally make a blocking read. If there are extraneous characters in the stream, this will pause the program until you type a newline.
If the stream is empty, you'll end up having to type a newline twice because both cin.ignore and cin.get are blocking reads. You can fix that (if it's really an issue) with a little stream buffer magic trick:
Code:
#include <iostream>
#include <ios> // For streamsize
#include <istream> // For basic_istream<>
#include <limits> // For numeric_limits<>
namespace jsw {
template <typename CharT, typename Traits>
std::basic_istream<CharT, Traits>& ignore (
std::basic_istream<CharT, Traits>& in, CharT delim )
{
if ( in.rdbuf()->sungetc() != Traits::eof() && in.get() != delim )
in.ignore ( std::numeric_limits<std::streamsize>::max(), delim );
return in;
}
}
int main()
{
// ...
jsw::ignore ( std::cin, '\n' );
std::cin.clear();
std::cin.get();
}
How it works really isn't important at this point because if you're having trouble with basic I/O, throwing you head first into stream buffers probably isn't a good idea. ;)
-
all you need to declare in this program is <iostream> and using namespace std;
and to stop the program from exiting before your ready use cin.ignore(2);
like this
Code:
cin.ignore(2);
return 0;
} //end of int main or w/e
-
>and to stop the program from exiting before your ready use cin.ignore(2);
Did you even bother to read my post?