954,535 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Problem with user input

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??



#include <iostream>

using namespace std;

int main()
{
int num1 , num2 , sum , lowBound, upBound;
char repeat = 'y';

while (repeat == 'y')
{
num1 = 0;
num2 = 0;
sum = 0;

cout << "Enter the first number: ";
cin >> num1;
cout << "\nEnter the second number: ";
cin >> num2;

if (num1 >= num2)
{
upBound = num1;
lowBound = num2;
}
else
{
upBound = num2;
lowBound = num1;
}

for( int i = lowBound; i <= upBound; i++ )
{
sum += i;
}

cout << "\nThe sum of all numbers, inclusive, between " << lowBound << " and " << upBound
<< " is " << sum << ". \n";

cout << "Would you like to play again?: ";

cin >> repeat;
}

system("Pause");
return 0;
}




edit * Thanks for pointing out my stupidity Narue, hope that's a little better :)

Aenima
Newbie Poster
10 posts since May 2006
Reputation Points: 10
Solved Threads: 0
 

>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.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

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

#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;
}



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 :

9
9
8
45
51
75
21
0
15
74
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

Aenima
Newbie Poster
10 posts since May 2006
Reputation Points: 10
Solved Threads: 0
 

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 ...

#include <iostream>
#include <cstdlib>

using namespace std;

int main()
{
    
    const int MAX_NUM_INTEGERS = 10;
    long int integers[10];
    long int i = 0, maximumValue = 0;
    
    int k;

    cout << "Please enter 10 integers : \n\n";

    while ( i < MAX_NUM_INTEGERS)
    {
        cout << i << "  ";  // for testing
        cin >> integers[i];
        i++;
    
        if (integers[i - 1] > maximumValue)
        {
            maximumValue = integers[i-1];
        }
    }
    
    // test cout of the array
    for (k = 0; k < sizeof(integers)/sizeof(int); k++)
        cout << integers[k] << endl;

    cout << "\nThe largest number you have entered is " << maximumValue << "\n\n";

    system("pause");
    return 0;
}
vegaseat
DaniWeb's Hypocrite
Moderator
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
 

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.

Aenima
Newbie Poster
10 posts since May 2006
Reputation Points: 10
Solved Threads: 0
 

Should I post this in another forum, since it seems to be more of a computer/compiler problem than a C++ problem?

Aenima
Newbie Poster
10 posts since May 2006
Reputation Points: 10
Solved Threads: 0
 

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.

Aenima
Newbie Poster
10 posts since May 2006
Reputation Points: 10
Solved Threads: 0
 

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
Posting Pro
599 posts since Aug 2005
Reputation Points: 70
Solved Threads: 32
 

There was a problem with my MinGW installation. Once I fixed it there were no more problems :)

Aenima
Newbie Poster
10 posts since May 2006
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You