iostream cin problem

A

Active8

Hi:

Hope this isn't an IDE or OS problem. I'm using VC++ v6 and using cin in
a console program. I use cin.getline() and sometimes just cin

Oh... Winders XP Pro Corp Ed. (ooohh... scary, no?) SP 1 on XP and sp4,
IIRC on VS enterprise. Really pushing the envelope here.

This program works fine if I run it from VC++, but even after doing a
release build, if I run the exe, it acts like I didn't enter a thing
when my prompt "enter number" or "enter anything" comes up and I enter a
valid number (or "anything")

It's strange. At first, the function clock() returned zero. Now that
works. I also had array corruption (array initialized in a constructor)
happening and the fix was to declare a dummy variable in the class
constructor. Ok enough horror. Here's a snippet - the problem snip du
jour (sp?), if you will:

Any idea why the cin object isn't doing it's job? Please? I did try
running it in 95 compatibility mode. It's a relatively fresh XP install,
too. And I'm carefull about what SW I install.

Thanks in advance
Mike
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

Hi:

Hope this isn't an IDE or OS problem. I'm using VC++ v6 and using cin in
a console program. I use cin.getline() and sometimes just cin

Oh... Winders XP Pro Corp Ed. (ooohh... scary, no?) SP 1 on XP and sp4,
IIRC on VS enterprise. Really pushing the envelope here.

This program works fine if I run it from VC++, but even after doing a
release build, if I run the exe, it acts like I didn't enter a thing
when my prompt "enter number" or "enter anything" comes up and I enter a
valid number (or "anything")

It's strange. At first, the function clock() returned zero. Now that
works. I also had array corruption (array initialized in a constructor)
happening and the fix was to declare a dummy variable in the class
constructor. Ok enough horror. Here's a snippet - the problem snip du
jour (sp?), if you will:

Any idea why the cin object isn't doing it's job? Please? I did try
running it in 95 compatibility mode. It's a relatively fresh XP install,
too. And I'm carefull about what SW I install.

First off, you forgot to post the snippet, and, while it probably has
nothing to do with your problem, you really should install SP2 for XP.
 
A

Active8

First off, you forgot to post the snippet, and, while it probably has
nothing to do with your problem, you really should install SP2 for XP.


Sorry <blush> and sorry for posting via google, but mozilla is sucking
ram. I thoght I saw the prob, but it should f up either wa I run the
program.

cout << "Hexagram number now enter: ";
cin >> hexnum;

Yeah. It's an I Ching program.

I don't know if something is in the buffer like a white space or what.
I *seem to have better luck with cin.getline, but as I said, this crap
app does run out of VC++

Thanks for the speedy response.

If you can, could you tell me the best way to do SP2 without upgrading
all that stuff I might not need. Seems like the online upgrade
"wizard" want's to give me everything under the sun. I especially want
to keep messenger services, MSN Messenger, and Alexa far away. I also
don't want any new services set to run at startup.

Thanks again.
Mike
 
J

Jim Langston

First off, you forgot to post the snippet, and, while it probably has
nothing to do with your problem, you really should install SP2 for XP.


Sorry <blush> and sorry for posting via google, but mozilla is sucking
ram. I thoght I saw the prob, but it should f up either wa I run the
program.

cout << "Hexagram number now enter: ";
cin >> hexnum;

Yeah. It's an I Ching program.

I don't know if something is in the buffer like a white space or what.
I *seem to have better luck with cin.getline, but as I said, this crap
app does run out of VC++

Thanks for the speedy response.

If you can, could you tell me the best way to do SP2 without upgrading
all that stuff I might not need. Seems like the online upgrade
"wizard" want's to give me everything under the sun. I especially want
to keep messenger services, MSN Messenger, and Alexa far away. I also
don't want any new services set to run at startup.

=====

Well, if you go to windowsupdate.microsoft.com after authenticating your
windows XP license, it will search your machine and build a list of things
it wants to instal (critical updates). You can then, if you desire, go
through that list and uncheck anything you don't want to install. However, I
would really suggest you get all the critical updates. They close a number
of security holes in XP that can leave your computer vulnerable.
 
B

BobR

Active8 said:
cout << "Hexagram number now enter: ";
cin >> hexnum;

'cout' is not defined. 'cin' is not defined.
'operator<<' is not defined.
'hexnum' is not defined.
In other words, you have not posted enough code to determine your problem.

I'll give you an example of what the people in this NG want to see:

#include <iostream>

int main(){
int hexnum(0);
std::cout << "Hexagram number now enter: ";
std::cin >> hexnum;
std::cout << "\nthe number entered: "<<hexnum<<std::endl;
// - may need if more input. (ignores next char in buffer) -
// std::cin.ignore(); // .ignore(2); skips 2 chars, etc.
// std::cin >> hexnum;
return 0;
} // main()
[ note: when posting, please use spaces, not tabs, when indenting. ]

FAQ http://www.parashift.com/c++-faq-lite
 
A

Active8

Active8 said:
cout << "Hexagram number now enter: ";
cin >> hexnum;

'cout' is not defined. 'cin' is not defined.
'operator<<' is not defined.
'hexnum' is not defined.
In other words, you have not posted enough code to determine your problem.

I'll give you an example of what the people in this NG want to see:

#include <iostream>

int main(){
int hexnum(0);
std::cout << "Hexagram number now enter: ";
std::cin >> hexnum;
std::cout << "\nthe number entered: "<<hexnum<<std::endl;
// - may need if more input. (ignores next char in buffer) -
// std::cin.ignore(); // .ignore(2); skips 2 chars, etc.
// std::cin >> hexnum;
return 0;
} // main()
[ note: when posting, please use spaces, not tabs, when indenting. ]

FAQ http://www.parashift.com/c++-faq-lite


The above example you provided is indicative of the relevant code in
my prog except I might need an ignore() (ok that didn't help.) If
something in the code after the cin is doing it, I wouldn't know where
and to post the whole prog is a lot of tabs to change. cin *does work
elsewhere. Can't run debug 'cause it only fouls up when I'm not in vc+
+ IDE. Remember the really odd thing is that it only screws up when I
run the prog "normally" i.e. not clicking "run" button in VC++? The
program flow is the same either way except the flow stops in the case
of the former 8)

Is it possible that VC++ is preventing a condition (in the stream
buffer?) from occuring?

When I enter a valid number and hit return, the cursor just goes to a
new line, nothing else happens, I can't enter anything after that ,
and have to ctrl-c kill the prog?

I'm including iostream via fstream.h and

oh what joy, not. I tried std::cin >> hexnum;

error C2653: 'std' : is not a class or namespace name

Been so long since I saw that I don't remember the fix.

Using cin as opposed to std::cin couldn't have adverse effects, could
it?

Thanks for the link. Great FAQ

Hope we're not chasing a non c++ related rabbit.

Thanks
Mike
 
B

BobR

Active8 said:
When I enter a valid number and hit return, the cursor just goes to a
new line, nothing else happens, I can't enter anything after that ,
and have to ctrl-c kill the prog?

I'm including iostream via fstream.h and

That's a problem. Always specifically include the headers you need. Do NOT
depend on one header includeing another.
'fstream.h' is a 'C' header. For 'C++', do it this way:

#include <iostream>
#include <fstream> // NO '.h'

If you use the 'C' headers, use the ones provided for 'C++'.

// #include <time.h>
#include <ctime>
// #include <stdlib.h>
oh what joy, not. I tried std::cin >> hexnum;

error C2653: 'std' : is not a class or namespace name
Been so long since I saw that I don't remember the fix.

#include said:
Using cin as opposed to std::cin couldn't have adverse effects, could
it?

It should not compile. Turn up the warning level of your compiler.

Try those 'fixes', and post back here.
 
A

Active8

It should not compile. Turn up the warning level of your compiler.

warnings do not a failed compile make IMO, but I get you.

cin (no wonder it's used alone in the first c++ book I read. The
author must have learned c first and isn't half the coder you are?
wish I had my c/c++ ref. handy)

cin did compile and still does after the header changes unless I've
another include that's now free of the inclusion guards. I'll check.

But std::cin does work now.
Try those 'fixes', and post back here.

Lots of warnings about stuff not in the brainbench certification

char mybits;
char operator ^ (char rhs) {return myBits ^ rhs;} // was warning prior
to the include file changes. The xor forces an implicit cast of myBits
to int, IIR[or assume]C ? Wonder why I no longer need that protection
from myself. Is the c++ include checking to make sure there's no
possibility of data loss?

I like this one from <XMEMORY>:

.... INCLUDE\xmemory(39) : warning C4100: '_P' : unreferenced formal
parameter

microsoft's own code creates warnings from an include of an include of
who knows how many included cludes (or is it "kludge"?) nah... it must
be me.

still digging but still the same prob. I'm at warn level 4, BTW was at
3. I'll conceed it's good to see the warnings up to a point maybe
turning off specific ones one is aware of and can live with.

I'll keep at it checking for the naughty includes. Thanks for
elucidating me on something I rarely touch. Just trying to get the
classes finished and have a quick tool that I can let friends try. As
soon as I get the warnings cleaned up, if the prob still exists, I'll
just use getline() and validate the input since getline() has been
nicer to me. I wasn't going to guard against all the stoopid user
tricks - let 'em crash it if they want to waste time entering
meaningless values. It'll be a windows app when it grows up. OTHOH
fixing it right saves me the effort of warning them against easter egg
hunting. They're adults but we like to play, too.

Tnx
Mike
 
B

BobR

Active8 said:
<snip>

I'll keep at it checking for the naughty includes. Thanks for
elucidating me on something I rarely touch. Just trying to get the
classes finished and have a quick tool that I can let friends try. As
soon as I get the warnings cleaned up, if the prob still exists, I'll
just use getline() and validate the input since getline() has been
nicer to me.

If 'getline' works (show me/us which one you are using, and how), then
std::cin should too. You most likely have something left in the buffer. Try
this after the first input:

// .....
std::cin>>MyVar;
if( std::cin.peek() == '\n' ){
std::cin.ignore(1); // clear a 'return' left in buffer.
std::cout<<"removed a return."<<std::endl;
}
if( not std::cin ){
std::cout<<"exiting."<<std::endl;
// std::terminate(); // pick one
// exit(1);
}
std::cin>>MyVar2;
// check again
// .....

An 'std::getline' removes the '\n', std::cin' leaves it in buffer.

Don't be afraid to put plenty of 'couts' in your code to test things, they
are easily removed once everything is working.
If you have lots of input, you could put the 'check' in a function:
[ just for a loose idea. ]

bool CheckCin( std::istream &in ){
if( not in ){
return false;
}
if( in.peek() == '\n' ){
in.ignore(1);
std::cout<<"removed a return."<<std::endl;
}
return true;
} // CheckCin(istream&)

// main()
std::cin>>MyVar;
if( not CheckCin( std::cin ) ){ exit(1); }
std::cin>>MyVar2;
if( not CheckCin( std::cin ) ){ exit(1); }

I wasn't going to guard against all the stoopid user
tricks - let 'em crash it if they want to waste time entering
meaningless values. It'll be a windows app when it grows up. OTHOH
fixing it right saves me the effort of warning them against easter egg
hunting. They're adults but we like to play, too.
Tnx
Mike

Well, when you get to 'windows', std::cin, std::cout, etc. won't work! <G>
 
B

Bo Persson

Active8 wrote:
:::
:::: When I enter a valid number and hit return, the cursor just goes
:::: to a new line, nothing else happens, I can't enter anything
:::: after that , and have to ctrl-c kill the prog?
:::
:: <snip>
:::
::
:::: Using cin as opposed to std::cin couldn't have adverse effects,
:::: could it?
:::
::: It should not compile. Turn up the warning level of your compiler.
::
:: warnings do not a failed compile make IMO, but I get you.
::
:: cin (no wonder it's used alone in the first c++ book I read. The
:: author must have learned c first and isn't half the coder you are?
:: wish I had my c/c++ ref. handy)
::
:: cin did compile and still does after the header changes unless I've
:: another include that's now free of the inclusion guards. I'll
:: check.
::
:: But std::cin does work now.
:::
::: Try those 'fixes', and post back here.
::
:: Lots of warnings about stuff not in the brainbench certification
::
:: char mybits;
:: char operator ^ (char rhs) {return myBits ^ rhs;} // was warning

You cannot define operators for built-in types like char. They are
predefined.


:: prior to the include file changes. The xor forces an implicit cast
:: of myBits to int, IIR[or assume]C ? Wonder why I no longer need
:: that protection from myself. Is the c++ include checking to make
:: sure there's no possibility of data loss?
::
:: I like this one from <XMEMORY>:
::
:: ... INCLUDE\xmemory(39) : warning C4100: '_P' : unreferenced formal
:: parameter
::
:: microsoft's own code creates warnings from an include of an
:: include of who knows how many included cludes (or is it "kludge"?)
:: nah... it must be me.

Yes, it's you - for using an ancient compiler. :)

The warnings in standard headers were fixed in VS2002, that's over 5
years ago.

After you security update your OS, you might consider getting a recent
C++ compiler. Visual C++ Express, for example, has an exceptional
price/performance ratio.


Bo Persson
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,772
Messages
2,569,593
Members
45,111
Latest member
KetoBurn
Top