944,017 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 2983
  • C++ RSS
May 14th, 2006
0

Problem with user input

Expand Post »
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??

C++ Syntax (Toggle Plain Text)
  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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Aenima is offline Offline
10 posts
since May 2006
May 14th, 2006
0

Re: Problem with user input

>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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
May 15th, 2006
0

Re: Problem with user input

Here's another example of the problem I'm having. When I run this program:

C++ Syntax (Toggle Plain Text)
  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 :
C++ Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Aenima is offline Offline
10 posts
since May 2006
May 15th, 2006
0

Re: Problem with user input

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 ...
C++ Syntax (Toggle Plain Text)
  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. }
Moderator
Reputation Points: 1333
Solved Threads: 1403
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
May 15th, 2006
0

Re: Problem with user input

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Aenima is offline Offline
10 posts
since May 2006
May 16th, 2006
0

Re: Problem with user input

Should I post this in another forum, since it seems to be more of a computer/compiler problem than a C++ problem?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Aenima is offline Offline
10 posts
since May 2006
May 17th, 2006
0

Re: Problem with user input

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Aenima is offline Offline
10 posts
since May 2006
May 20th, 2006
0

Re: Problem with user input

I tried your code with DEV 4.9.9.2. It's working fine on my end. It even works fine on microsoft compiler.
SpS
Reputation Points: 70
Solved Threads: 32
Posting Pro
SpS is offline Offline
598 posts
since Aug 2005
May 21st, 2006
0

Re: Problem with user input

There was a problem with my MinGW installation. Once I fixed it there were no more problems
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Aenima is offline Offline
10 posts
since May 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Saving Entered Data
Next Thread in C++ Forum Timeline: Select a development environment





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC