C++? Programming Software Question

  • Two questions: I have a $50 copy of Borland C/C++ ver 4.52 copyright 1995. I can’t figure out how to get float input with a “cin >>” statement. It inputs the others fine and outputs everything using “cout <<”. The MS Visual Studio at school has no problem with this as long as the var is the correct type. Is this beyond my version’s capabilities or is this a MS variation on C++?
  • Next question; is the Turbo C++ the next (latest) version? It’s the only “general” version I can seem to find on Borland’s site; everything else is ‘optimized for this or that’ and I dunno what -n- th’ hell is on it, or if I need it. - MC

Not sure on the first, I haven’t used Borland’s C/C++ in a long long time or even a cin in a long time.

However, I can answer the second, the latest version of their C++ compiler is Borland C++ Builder V3. Or at least it was last time I checked (it is also the version we use here to build various support tools for our games, but we use MS Visual C++ for actual development simply because the code it generates is faster and because 3rd party developers use it).


“Glitch … Window, large icons.” - Bob the Guardian

Well, I can’t tell ya what the problem is, but it ain’t a special MS feature. In C++, cin should work on all basic data types (chars, ints, longs, floats, doubles)

You could check your header file (iostream.h) to see what operations are defined for operator>>. There should be something like:

istream& operator>>(int&)
istream& operator>>(short&)

If one of these is “istream& operator>>(float&)” then the function is defined, and ought to work.

If you can’t get it to work, you may want to try reading the number in as a string, and then using atof or strtod to convert it to a float.

MC Just to add to what Hunsecker said:
What happens when you try? Do you get a compile error? If so then the problem is probably what Hunsecker said. If the statement compiles OK, are you getting garbage for the value of your variable or a run time error? If you are getting an unexpected value for your variable or if the variable is unchanged, you might check the state of cin before and after your with an if(cin).


Virtually yours,

DrMatrix

      • If I declare a float variable and try to get keyboard input with a “cin >>” statement, I get a “floating point: overflow” error message. Other var types work like they’re supposed to.
      • I can’t find anything like what Hunseker describes in iostream.h. There is also a float.h that looks suspicious, but including it don’t help. - MC?

You could try the following:
[ul][li]make your variable a double instead of a float[/li][li]Make sure your input stream is not f’d up. I know with cout there are tons of formatting options you can do like[/li]


cout.setf(flags);
cout << setiosflags(flags) << "foo" << 1.0f;

[/ul]
It’s been 3 years since I’ve done C++ - I’m doing java now - and I rarely used cin/cout so I couldn’t tell you much more than that. Good luck!

Clarification re: cout << setXXX(); - I don’t know what there is to do for cin to configure it, maybe nothing. Consult your C++ text…

Some older compilers require special instructions (command-line options, #pragmas…) to include floating-point support for scanf (in C) and << (in C++). It’s just a question of not wanting to burden every EXE file with floating-point stuff it doesn’t need. (Most modern compilers don’t do it because the difference isn’t worth fussing about any more.)


John W. Kennedy
“Compact is becoming contract; man only earns and pays.”
– Charles Williams

MC
I don’t have a C/C++ compiler on my PC right now. I use an IBM mainframe at work. The header IOSTREAM (which, through the arcane mainframe mapping, maps to iostream.h or iostream.hpp) has the declarations for
ostream& operator<<(long);
ostream& operator<<(double);
But the fact that you are not getting a compiler error tells me that the declarations are visible to the compiler in some header, so I don’t think your problem is in your header (I could be wrong here, though.)

Just to check for the obvious (which should have been the first thing to do), What exactly are you inputting?

If you output a float (via cout<<) to a file, you could try redirecting cin to that file.


Virtually yours,

DrMatrix

      • This is the very beginning of my first C++ class, so it’s not real complicatd stuff, and the instructor wants it done this way:
        (paraphrased)

float a;
cout << “Enter sum :”;
cin >> a;
cout << "You entered " << a;

  • I completely reinstalled the compiler just a couple days ago, after reformatting a few weeks back. There’s nobody else with any knowledge of programing around to have screwed it up as a joke. It seems so simple, and yet. . . it works in MS Vis Studio at school, but not on Borland at home.
  • And in class we seem to have moved on to printf statements, so it might be a moot point, but it does make me wonder. - MC