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?
#include <iostream>
#include <string>
#include <sstream>
usingnamespace 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:
#include <iostream>
#include <string>
#include <sstream>
usingnamespace 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
}
#include <iostream>
#include <string>
#include <sstream>
usingnamespace 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
}
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
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);