Problem with user input

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: May 2006
Posts: 10
Reputation: Aenima is an unknown quantity at this point 
Solved Threads: 0
Aenima Aenima is offline Offline
Newbie Poster

Problem with user input

 
0
  #1
May 14th, 2006
First of all, hello everyone. My problem is this. In writing a program which accepts multiple user inputs of type int, during the first run through the program, after the first cout << statement prints, I have to press enter twice in order for the second cout statement to display. The first cin only stores the first digit of the input, which messes up the calculation. On all subsequent runs through the program, everything works fine. I've had this problem on a few programs now. When my professor compiles the programs himself, using Visual C++, everything works the way it's supposed to, but at home I use Dev C++. I've also built the program using Ultimate++ and I get the same problem. Is there something wrong with my computer setup??

  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7. int num1 , num2 , sum , lowBound, upBound;
  8. char repeat = 'y';
  9.  
  10. while (repeat == 'y')
  11. {
  12. num1 = 0;
  13. num2 = 0;
  14. sum = 0;
  15.  
  16. cout << "Enter the first number: ";
  17. cin >> num1;
  18. cout << "\nEnter the second number: ";
  19. cin >> num2;
  20.  
  21. if (num1 >= num2)
  22. {
  23. upBound = num1;
  24. lowBound = num2;
  25. }
  26. else
  27. {
  28. upBound = num2;
  29. lowBound = num1;
  30. }
  31.  
  32. for( int i = lowBound; i <= upBound; i++ )
  33. {
  34. sum += i;
  35. }
  36.  
  37. cout << "\nThe sum of all numbers, inclusive, between " << lowBound << " and " << upBound
  38. << " is " << sum << ". \n";
  39.  
  40. cout << "Would you like to play again?: ";
  41.  
  42. cin >> repeat;
  43. }
  44.  
  45. system("Pause");
  46. return 0;
  47. }


edit * Thanks for pointing out my stupidity Narue, hope that's a little better
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,625
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 715
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Problem with user input

 
0
  #2
May 14th, 2006
>yech, that looks sloppy, the indenting looks better in the IDE.
The closing tag for code uses a forward slash rather than a backslash. There's an obvious watermark in the post editor window that shows you exactly how to do it, so you really have no excuses.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 10
Reputation: Aenima is an unknown quantity at this point 
Solved Threads: 0
Aenima Aenima is offline Offline
Newbie Poster

Re: Problem with user input

 
0
  #3
May 15th, 2006
Here's another example of the problem I'm having. When I run this program:

  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7.  
  8. const int MAX_NUM_INTEGERS = 10;
  9. long int integers[10];
  10. long int i = 0, maximumValue = 0;
  11.  
  12. cout << "Please enter 10 integers : \n\n";
  13.  
  14. while ( i < MAX_NUM_INTEGERS)
  15. {
  16. cin >> integers[i];
  17. i++;
  18.  
  19. if (integers[i - 1] > maximumValue)
  20. {
  21. maximumValue = integers[i-1];
  22. }
  23. }
  24.  
  25. cout << "\nThe largest number you have entered is " << maximumValue << "\n\n";
  26.  
  27. system("pause");
  28. return 0;
  29. }
It compiles with no errors, when I run the program it takes
11 entries, then it seems to concatenate the first digit of
the first entry with the second entry, so for an entry list like
this :
  1. 9
  2. 9
  3. 8
  4. 45
  5. 51
  6. 75
  7. 21
  8. 0
  9. 15
  10. 74
  11. 16
The output will say that the largest number is 99. Needless
to say, this is very annoying and makes testing programs
much more of a hassle. If anyone has any idea why this is
happening, please let me know. I'm using dev-c++ 4.9.9.2
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,009
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 928
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: Problem with user input

 
0
  #4
May 15th, 2006
I am using dev-c++ 4.9.9.1 and your last code works just fine (Windows XP). I think the version should not make any difference, that is only an update of the IDE. Are you using g++ as the compiler?

I added a few test lines to your code ...
  1. #include <iostream>
  2. #include <cstdlib>
  3.  
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8.  
  9. const int MAX_NUM_INTEGERS = 10;
  10. long int integers[10];
  11. long int i = 0, maximumValue = 0;
  12.  
  13. int k;
  14.  
  15. cout << "Please enter 10 integers : \n\n";
  16.  
  17. while ( i < MAX_NUM_INTEGERS)
  18. {
  19. cout << i << " "; // for testing
  20. cin >> integers[i];
  21. i++;
  22.  
  23. if (integers[i - 1] > maximumValue)
  24. {
  25. maximumValue = integers[i-1];
  26. }
  27. }
  28.  
  29. // test cout of the array
  30. for (k = 0; k < sizeof(integers)/sizeof(int); k++)
  31. cout << integers[k] << endl;
  32.  
  33. cout << "\nThe largest number you have entered is " << maximumValue << "\n\n";
  34.  
  35. system("pause");
  36. return 0;
  37. }
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 10
Reputation: Aenima is an unknown quantity at this point 
Solved Threads: 0
Aenima Aenima is offline Offline
Newbie Poster

Re: Problem with user input

 
0
  #5
May 15th, 2006
In the compile log it shows "Executing g++.exe" Under the compiler options in Dev-C++ it shows "Default Compiler", and in the program tab it lists
gcc.exe
g++.exe
make.exe
gdb.exe
windres.exe
dllwrap.exe
gprof.exe

These files are all in the c:\Dev-C++\bin directory. I have the same problem when I compile with Ultimate++. I uninstalled, redownloaded, and reinstalled Dev, with mingw-3.4.2, but that didn't fix anything.

My programs work fine on my professor's computer, too.

Thanks for your help.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 10
Reputation: Aenima is an unknown quantity at this point 
Solved Threads: 0
Aenima Aenima is offline Offline
Newbie Poster

Re: Problem with user input

 
0
  #6
May 16th, 2006
Should I post this in another forum, since it seems to be more of a computer/compiler problem than a C++ problem?
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 10
Reputation: Aenima is an unknown quantity at this point 
Solved Threads: 0
Aenima Aenima is offline Offline
Newbie Poster

Re: Problem with user input

 
0
  #7
May 17th, 2006
I have reinstalled Dev and uninstalled Visual C# Express, since I first noticed the problem around the time I installed VC#, but it hasn't changed anything. I've searched every C++ forum I could find and looked all over bloodshed.net for anything that might help, but I haven't had any luck. Please help me out if you have any ideas.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 598
Reputation: SpS is on a distinguished road 
Solved Threads: 32
SpS's Avatar
SpS SpS is offline Offline
Posting Pro

Re: Problem with user input

 
0
  #8
May 20th, 2006
I tried your code with DEV 4.9.9.2. It's working fine on my end. It even works fine on microsoft compiler.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 10
Reputation: Aenima is an unknown quantity at this point 
Solved Threads: 0
Aenima Aenima is offline Offline
Newbie Poster

Re: Problem with user input

 
0
  #9
May 21st, 2006
There was a problem with my MinGW installation. Once I fixed it there were no more problems
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC