Reputation:
I'm new to C++ programming and just programming in general, and I was going through Stroustrup's Programming Principles and Practice Using C++ 2nd Edition when I got stuck on Chapter 4 Drill 6.
The question reads: Write a program that consists of a while-loop that reads in two ints and then prints them. Change the program to write out the smaller value, and the larger value. etc. etc. Now change the body of the loop so that it reads just one double each time around. Define two variables to keep track of which is the smallest and which is the largest value you have seen so far. Eacht itme through the loop write out the vlue entered If it's the smallest so far, write the smallest so far after the number. If it is the largest so far, write the largest so far after the number.
Basically, I am having trouble to keep the smallest and largest values to run through all of the loops. Because, at the moment, all that's happening is that each time I enter two numbers, it just displays which one is the largest/smallest which I had already achieved earlier in the program. It's not maintaining the largest/smallest values until there is a smaller/larger value in one of the loops as it should.
I hope I was able to explain the situation clearly. I'm sure its just a very simple fix, I'm new to programming. This is my code so far:
#include "../../../std_lib_facilities.h";
int main()
{
double val1;
double val2;
double largest =0 ;
double smallest= 0 ;
double input = 0;
char exit = ' ';
while (exit != 'x') {
cout << "Please enter two numbers.\n";
cin >> val1 >> val2;
cout << "The two values you have entered are: " << val1 << " and " << val2 << "\n";
if (val1 < val2) {
cout << "The smaller value is: " << val1 << ". The larger value is: " << val2 << ".\n";
if (val2 - val1 <= .5)
cout << "The numbers are almost equal.\n";
}
else if (val2 < val1) {
cout << "The smaller value is: " << val2 << ". The larger value is: " << val1 << ".\n";
if (val1 - val2 <= .5)
cout << " The numbers are almost equal.\n";
}
else
cout << "The two numbers " << val1 << " are equal!\n"; \
if (val1 < smallest && val2>val1) {
cout << val1 << " is the smallest so far.\n";
smallest = val1;
}
else if (val2<smallest && val1>val2) {
cout << val2 << " is the smallest so far.n";
smallest = val2;
}
else {}
if (val1 > largest && val2 < val1) {
cout << val1 << " is the largest so far.\n";
val1 = largest;
}
else if (val2 > largest && val1 < val2) {
cout << val2 << " is the largest so far.\n";
val2 = largest;
}
else {
}
cout << "If you would like to exit this program, please enter x. Otherwise, enter any other key to continue. \n";
cin >> exit;
}
}
Upvotes: 1
Views: 515
Reputation: 1
This implementation seems to be much clearer. I was seeking for an answer for a long time, this guy deserves respect, sorry for responding to another answer. https://stackoverflow.com/a/49228379/13644998
#include < iostream>
#include < cstdlib>
int main() {
double num_1 = 0;
double num_2 = 0;
double largest = 0;
double smallest = 0;
bool condition1 = true;
while (true) {
std::cin >> num_1;
if (num_1 > largest) {
largest = num_1;
}
else if (num_1 < smallest) {
smallest = num_1;
}
std::cout << "The largest so far: " << largest << std::endl;
std::cin >> num_2;
if (condition1) {
smallest = largest;
condition1 = false;
}
if (num_2 < smallest) {
smallest = num_2;
}
else if (num_2 > largest) {
largest = num_2;
}
std::cout << "The smallest so far: " << smallest << std::endl;
}
system("pause");
return 0;
}
Upvotes: 0
Reputation: 206627
You are not keeping track of the largest value correctly.
cout << val1 << " is the largest so far.\n";
val1 = largest; // Wrong
needs to be
cout << val1 << " is the largest so far.\n";
largest = val1;
and
cout << val2 << " is the largest so far.\n";
val2 = largest; // Wrong
needs to be
cout << val2 << " is the largest so far.\n";
largest = val2;
Upvotes: 4