C++/User data entry problem
Expert: Ralph McArdell - 5/19/2006
QuestionFirst of all, thank you very much for your prompt reply. I tried your recommendations, and as you suspected, the results were
0: 9
9
1: 8
2: 7
etc...
I added the loop you wrote in 2/ below and added a little to it just to see if I ever entered the loop ( i.e. cin.good() == false) It looks like this:
while ( i < MAX_NUM_INTEGERS)
{
cout << i << ":";
cin >> integers[i];
cout << "\nYou just entered a number...\n";
if(!cin.good())
{
cout << "\nI'm in the cin test loop...";
if(cin.fail())
{
cout << "cin detected stream formatting eror.)"
<< "\nQuitting";
}
else if(cin.bad())
{
cout << "cin detected fatal stream error.\n"
<< "quitting";
}
else //cin.eof()
{
cout << "cin detected stream file end. Quitting\n";
}
good = false;
}
i++;
if (integers[i - 1] > maximumValue)
{
maximumValue = integers[i-1];
}
}
if(good )
{
cout << "\nThe largest number you have entered is " << maximumValue << "\n\n";
}
As long as I enter a valid int, I never enter the loop.
I downloaded a version of MinGW from
http://nuwen.net/mingw.html, after following the instructions on the page I can compile from the command line and everything works perfectly, but now I have to figure out how to get Dev-C++ to use the new compiler. If you have any information about the configuration, that'd be great, and if not then back to forum prowling :)
Thanks again, the problem is still there with Dev, but at least I can compile from the command line now, and that's a huge improvement over yesterday.
-------------------------
Followup To
Question -
Hello Mr. McArdell, I've been having this problem for a while, on all of my programs. For example, here is code that accepts 10 inputs into an array, then outputs the largest number:
[code]
#include <iostream>
using namespace std;
int main()
{
const int MAX_NUM_INTEGERS = 10;
long int integers[10];
long int i = 0, maximumValue = 0;
cout << "Please enter 10 integers : \n\n";
while ( i < MAX_NUM_INTEGERS)
{
cin >> integers[i];
i++;
if (integers[i - 1] > maximumValue)
{
maximumValue = integers[i-1];
}
}
cout << "\nThe largest number you have entered is " << maximumValue << "\n\n";
system("pause");
return 0;
}
[/code]
When I run this code, the program asks for 10 numbers, but accepts 11. The first digit of the first entry gets appended to the beginning of the second entry, so for example:
entry list:
[code]
9
9
8
45
51
75
21
0
15
74
16
[/code]
outputs that the largest number is 99.
This problem also shows up in programs that execute in a loop. The first time through the loop, the first entry doesn't work properly, but on subsequent runs everything works fine. I believe this is a problem with my computer since my professor has no problems when he compiles my code. I am using Dev-C++ 4.9.9.2, but I have compiled using Ultimate++ and the problem is the same. My computer is running Win XP Pro Sp2.
Answer -
I have run your program on Windows XP x64 and SuSE Linux 10.0 x64, built using the MS Visual C++ 2005 compiler and the GNU g++ 4.0.2 compiler and received no problem as you describe - that is, as for your professor, it worked fine for me.
I cannot give an absolute answer to your problem as I do not have it. I can only make a few points and suggestions.
I do not have the DevC++ (which uses the MinGW GNU C++ compiler port to Windows) or Ultimate++ (which I have not heard of before) so I cannot say for sure what is going on. A quick look on the Internet indicates that Ultimate++ uses existing compilers such as GCC, MinGW and MS Visual C++. So I suspect that either Ultimate++ used the MinGW implementation installed on your system with DevC++ or installed a similar version for its own use. Hence you may not have really tried your code using two different compilers, only two different integrated development environments wrapped around the same compiler!
This leads me to suspect your problem may have to do with your IOStream implementation or build. If your whole computer suffered such a problem at the lowest level then it simply would not work!
What appears to be happening (and this is just a starting off hypothesis) is that the call to the std::cin extraction operation to extract the first integer does not recognise the first newline (Enter key). You might like to add a few things to your code to check on this:
1/ Add a prompt showing for example the item number expected before the cin >> integers[i]; line thus:
while ( i < MAX_NUM_INTEGERS)
{
cout << i << ": ";
cin >> integers[i];
// ...
In the above example the user is prompted thus:
0: 9
1: 9
2: 8
Etc...
For your system we would expect something like:
0: 9
9
1: 8
Etc...
You can change the prompted for value from i to i + 1 if you prefer the range to run from 1 to 10 rather than 0 to 9, or you could just output a static prompt such as a colon or some text maybe ready: or value: or some such.
2/ Check the state of the stream. C++ IOStreams can be in one of several states. For our purposes we wish the stream to be in a good state - not end of file (eof), nor formatting error (fail), nor any fatal errors (bad).
We can do this like so:
bool good(true); // Set good flag to true to start with
while ( i < MAX_NUM_INTEGERS)
{
if ( ! cin.good() )
{
if ( cin.fail() )
{
cout << "cin detected stream formatting error. "
"Quitting.\n";
}
else if ( cin.bad() )
{
cout << "cin detected fatal stream error. "
"Quitting.\n";
}
else // cin.eof()
{
cout << "cin detected stream file end. Quitting.\n";
}
good = false; // not good anymore!
break;
}
// ...
After the loop we check the input was good before displaying the results:
if ( good )
{
cout << "\nThe largest number you have entered is "
<< maximumValue << "\n\n";
}
The above additions will help detect what is going on but not fix or patch around the problem. One thing to do is to check to see if there are any reported problems with the compiler that seem to fit the observed behaviour. In this case, as I stated, you are using the MinGW GNU C++ port supplied with DevC++.
A trip to the MinGW site (
http://www.mingw.org/history.shtml) and a search for cin (you have to enter cin in the search on the left hand side bar, then click search - this takes you to the MinGW Sourceforge site where you have to click on the search button there as well!) produces a list of items, one set of which have the topic MinGW bug:
>
> int main()
> {
> using std::cin;
> using std::cout;
>
>
> cout<<"It should wait only for one ENTER but in MINGW 3.1 (only)\n";
> cout<<"it waits for two. It works as it should in other compilers\n";
>
> // It should work with only one ENTER
> cin.ignore();
> }
>
Thanks for the report. I have a simple fix for that in istream.tcc (using
sbumpc rather than sgetc in the special case when streamsize equals or defaults
to 1). There may be another way using casts.
I"ll clean up and ask for review in libstdc++ list.
Danny
The above shows the bug report and a maintainer's reply. You will note that it pertains _only_ to version 3.1 and seems to be for the ignore function, but it needs 2 Enters key presses not the expected 1 (ignore ignores 1 character by default). This sounds very much like your initial input requiring a second newline to me. The fix may be specific to this case for ignore and so may only paper over the cracks and not fix a root cause. I also found some release notes for the 3.0 version which mentioned that cin in that version was quite broken. These are both _old_ problems from 2002.
So if you are using an old version from 3 or 4 years ago then I suggest you upgrade to the latest version of DevC++. You might like to check with your professor what version he is using. He might also be able to help with any upgrades if the version you are using turns out to be the problem.
If this is not the problem then, well, something is somewhat wrong so I suggest you try uninstalling and re-installing DevC++ and especially the MinGW compiler and its supporting libraries. Again you might like to install a newer version, or ensure you install the same version as your professor's.
AnswerSorry, but as I said in my original answer I do not have Dev-C++ so do not know how they integrate MinGW into the Dev-C++ IDE. However, you might like to check out the Dev-C++ site to see it they have an updated version for download. The site can be found at:
http://www.bloodshed.net/devcpp.html.
A quick peek shows they have version 4 and a beta of a new version 5 available for download...
Also the Dev-C++ resources page at
http://www.bloodshed.net/dev/ has some useful areas. The package repository link takes you to a page that mentions checking for updates using the WebUpdate utility within Dev-C++ (try in Tools, Check for updates/packages). The final line of text links to DevPaks.org at
http://devpaks.org/ from which you can download the latest available DevPaks. One such category of listed DevPaks is for MinGW.
Note: I do not know if the above mention of web updates and DevPak makes any sense to you, I presume it would as a Dev-C++ user. I just repeated what I found on the pages I mentioned. I am no expert in these matters!
I would also suggest you learn how to use one or more search engines such as Google (
http://www.google.com/) to locate such sites, if you do not do so already. I think you may well use search engines already as you mention forum prowling so sorry for repeating stuff you know if that is the case. I am trying to make the point that you should generally try 'looking' for what you want using such sites. I have found that usually if you look you find!
If all else fails you might like to poke around in the DevC++ installation directory.
If this fails to turn up anything useful such as a configuration file then maybe if you are feeling brave you can try looking in the registry. However be very careful and back up the registry and any other important data first. Remember that messing up the Windows registry can seriously mess up your system to the point of making it unusable. Standard registry hacking warning over!
Finally, you might find this site useful as it has links to various free compilers and other resources:
http://www.thefreecountry.com/
The C and C++ compiler page is at:
http://www.thefreecountry.com/compilers/cpp.shtml
Good luck and have fun.