Click to See Complete Forum and Search --> : Just started c++ today, weird issue
newbjohn
06-04-2007, 09:50 PM
Hi, just registered. this is day 1 of my c++ career, so this is going to be extremely basic.
My program is supposed to do two things, 1) count and display how many negative numbers the user enters, 2) use a cin at the end to prevent the command prompt window from closing before I can read the amount. The cin the best way I know how to create a pause just before the program ends.
For some reason, it won't pause for input.
I'm using dev-c++ IDE
#include <iostream>
int main()
{
int var = 0, count = 0, thiscoutwilloutput = 0, thiscinwontletuserinput;
while(std::cin >> var){
if (var < 0){
count ++;
}
}
std::cout << count << std::endl;
std::cout << thiscoutwilloutput;
std::cin >> thiscinwontletuserinput;
return 0;
}
thanks in advance.
Danny
06-04-2007, 10:15 PM
The problem is here:
while(std::cin >> var){
This expression doesn't do what you expect it to. It's better to read
cin>>var first and then check its value:
while (var)
{
//..
}
newbjohn
06-04-2007, 10:58 PM
Really?
Tragic news, since one of my books, C++ Primer, will be using this method.
I'm shocked that a technique used as early as page 26 of a very popular book won't be applicable : /
Maybe I'll try a different compiler.
edit by admin: you may not advertise on this forum, thank you
jonnin
06-04-2007, 11:57 PM
its just at typical while loop logic issue.
you cant say
while(whatever)
{
read(whatever)
}
because whatever needs an initial value so you can enter the loop that first time. So you either hard code to enter one time (int whatever = 1) or you read it first (do / while)
Danny
06-05-2007, 12:15 AM
Really?
Tragic news, since one of my books, C++ Primer, will be using this method.
I'm shocked that a technique used as early as page 26 of a very popular book won't be applicable : /
Maybe I'll try a different compiler.
You've solved a mystery for me. I have seen quite a few posts that use this incorrect idiom and wondered where they all came from. Anyway, that book should be fixed or possible replaced with a better C++ book (and there are plenty of those, fortunately).
newbjohn
06-05-2007, 12:16 AM
edit by admin: you may not advertise on this forum, thank you
you're mistaken..
You've solved a mystery for me. I have seen quite a few posts that use this incorrect idiom and wondered where they all came from. Anyway, that book should be fixed or possible replaced with a better C++ book (and there are plenty of those, fortunately).
What a shocker, I found this book to be well praised when I was researching my purchase. Good thing this (http://www.amazon.com/C%2B%2B-Dummies-Stephen-Randy-Davis/dp/0764568523/ref=sr_1_1/102-7721036-0082548?ie=UTF8&s=books&qid=1181017584&sr=8-1) is shipping atm, even if its not as intelligent and thorough I suppose it could be more standard.
This (http://www.parashift.com/c++-faq-lite/input-output.html#faq-15.2)seems to be touching on what I'm experiencing.
JPnyc
06-05-2007, 12:24 AM
That's entirely possible. However, a direct link to a book for sale on amazon will be taken as advertising roughly 100% of the time, whether correct or not.
Danny
06-05-2007, 12:37 AM
Personally, I wouldn't recommend books for dummies, particularly not when C++ is concerned. I can recommend many other C++ books which are known to be accurate, clear and up-to date. C++ Primer by Lippman et al. is very good.
The C++ Programming Language by Stroustrup is still one of the best C++ books around although it's a bit over the top for a beginner.
newbjohn
06-05-2007, 12:51 AM
Odd thing is after reading the edit, I first thought it looked like I was advertising via a smear campaign against the compiler, as if I contrived the question to prompt a "try this compiler" response. I thought this book would be common amongst many crowds, so much so that I linked to it to clarify that I did have the most recent version. The link was just what google returned. I know I don't have to explain myself, I just want to tell the story of what happened.
Anyhow, perhaps I'll only link to products that I do indeed know are popular.. in which case a link to it on amazon wouldn't be taken as an ad.
edit:
sorry for yet another edit, I'm just a bit out of my element right now.
Personally, I wouldn't recommend books for dummies, particularly not when C++ is concerned. I can recommend many other C++ books which are known to be accurate, clear and up-to date. C++ Primer by Lippman et al. is very good.
The C++ Programming Language by Stroustrup is still one of the best C++ books around although it's a bit over the top for a beginner.
Ok thanks. I got the dummies book in case I hid a barrier in the primer. Looking forward to Stroustrup's someday :)
devx.com
Copyright Internet.com Inc. All Rights Reserved