Need a bit of help with string and ENTER as an input please?

So I got this project due for class, I've got the majority of it done, but I'm stuck on this last bit.

I'm suppose to make a program where you type in two integers, and then it will tell you which is larger. So far I've done this. What my problem is, say if the user puts in one number, but doesn't put in a second number and just presses enter, I need it to just print out the first number as theoretically it is the larger number. Any help?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
#include <string>
#include <sstream>
using namespace std;

int main () {

      string n;
  int first=0;
  int second=0;

  cout << "Enter two natural numbers to get the largest number." << endl;
  cout << "First number: ";
   getline (cin,n);
   stringstream(n) >> first;

      if (first <=0){
                     cout << " That is not a natural number, please enter a natural number." << endl;
                     getline (cin,n);
                     stringstream(n) >> first;
    } while (first <=0);

  cout << "Second number: ";
  getline (cin,n);
  stringstream(n) >> second;

      if (second <=0){
                     cout << " That is not a natural number, please enter a natural number." << endl;
                     getline (cin,n);
                     stringstream(n) >> second;
    } while (second <=0);

  if (first > second)  cout << "The larger number is: " << first << endl;
  if (first < second)  cout << "The larger number is: " << second << endl;
  if (first == second)  cout << first-second << endl;

     }
I'm suppose to make a program where you type in two integers,

But this program is only checking for natural numbers...

Line 21 is an infinite while loop whenever the first number is invalid.
Line 31 too..

It is unclear what u'r asking. It looks like the program is supposed to continue prompting for user input until it receives it. If the 2nd number is not necessary then this will do the trick:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>
#include <string>
#include <sstream>
using namespace std;

int main () {

      string n;
  int first=0;
  int second=0;

  cout << "Enter two natural numbers to get the largest number." << endl;
  cout << "First number: ";
   getline (cin,n);
   stringstream(n) >> first;

while (first <=0)
{
     cout << " That is not a natural number, please enter a natural number." << endl;
     getline (cin,n);
     stringstream(n) >> first;
} 
  cout << "Second number: ";
  getline (cin,n);
  stringstream(n) >> second;

if (second <=0)
{
   cout << " That is not a natural number." << endl;
   cout << "The larger number is: " << first << endl;
   system("PAUSE"); //for quick but dirty hangtime to see the result
   return 1; //exit the program here when 2nd input is invalid
}

  if (first > second)  cout << "The larger number is: " << first << endl;
  if (first < second)  cout << "The larger number is: " << second << endl;
  if (first == second)  cout << first-second << endl;

 system("PAUSE"); //for quick but dirty hangtime to see the result
 return 0; //always return something
 }

Last edited on
I need it to accept ENTER as an input is pretty much what I'm getting at.
'cin' already does this.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>
#include <string>
#include <sstream>
using namespace std;

int main () {

      string n;
  int first=0;
  int second=0;

  cout << "Enter two natural numbers to get the largest number." << endl;
  cout << "First number: ";
   getline (cin,n);
   stringstream(n) >> first;

while (first <=0)
{
     cout << " That is not a natural number, please enter a natural number." << endl;
     getline (cin,n);
     stringstream(n) >> first;
} 
  cout << "Second number: ";
  getline (cin,n);
  stringstream(n) >> second;

while(second <=0)
{
   cout << " That is not a natural number, please enter a natural number." << endl;
   getline (cin,n);
   stringstream(n) >> second ;
} 

  if (first > second)  cout << "The larger number is: " << first << endl;
  if (first < second)  cout << "The larger number is: " << second << endl;
  if (first == second)  cout << first-second << endl;

 system("PAUSE"); //for quick but dirty hangtime to see the result
 return 0; //always return something
 }
Last edited on
Hmmmm..... its not working that way, if I press enter it just goes down one line and waits for an integer to be put in.
Ok, its not just enter...

Here's a script.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
Script started on Tue 25 Sep 2012 12:53:43 PM PDT
^[[?1034hbash-4.1$ exit^H^H^H^H^[[3@./a.out
Enter two natural numbers to get the largest number.
First number: 8
 That is not a natural number, please enter a natural number.
First number:
Second number: 8
 That is not a natural number, please enter a natural number.
Second number: 9
The larger number is: 9
bash-4.1$ ./a.out
Enter two natural numbers to get the largest number.
First number:
 That is not a natural number, please enter a natural number.
First number:
 That is not a natural number, please enter a natural number.
First number:
 That is not a natural number, please enter a natural number.
First number: 8
Second number: 8
 That is not a natural number, please enter a natural number.
Second number: 8
0
bash-4.1$ exit
exit

Script done on Tue 25 Sep 2012 12:54:33 PM PDT
This?
http://www.cplusplus.com/forum/beginner/2624/

line 36
1
2
first-second will always equal 0...not sure what u'r expecting to see here...
if (first == second)  cout << first-second << endl; 
Part of the assignment is if they are equal, then the output is 0.

Here is a link to the assignment, maybe this will explain better then what I am explaining it.

http://www2.cs.uidaho.edu/~rinker/cs120/lab5fall12.pdf
My earlier post on Sep 25, 2012 at 4:45pm does what you require for the assignment. The only thing missing is separating the code that's there into 3 functions.

I'll give u the prototypes but u should implement them:
1
2
3
int GetUserInput();
int Largest(int, int);
void PrintRestult(int);
That's totally fine, just needed a push in the right direction is all. Thank you!
Topic archived. No new replies allowed.