cout << "Please enter a number: ";
cin >> n;
cout << endl;
for (int i; i < n; i++){
cout << "Please enter each number you want to progress: ";
cin >> x;
}
cout << x;
In this code the compiler will print the last x user had entered how to allow it to print every x user enters so I can compare with each input the user entered?
In this code the compiler will print the last x user had entered
That's because your cout (line 8) is outside your loop. Each time through the loop, you overwrite x with as new value from the user. move it inside your loop.
[How] I can compare with each input the user entered?
#include <iostream>
usingnamespace std;
int main() {
int n;
int x;
int max;
int min;
cout << "Please enter a number: ";
cin >> n;
cout << endl;
for (int i; i < n; i++){
cout << "Please enter each number you want to progress: ";
cin >> x;
max = min = x;
if ( x > max ){
max = x;
}
if ( x < min ){
min = x;
}
}
cout << max << "\n";
cout << min;
cout << endl;
return 0;
}
I'm not sure exactly what you're wanting your code to do. I've found one error already, I dont know what compiler or debugger you are using, but it Visual Studio 2010 didn't allow me to run it without initializing your ' i ' within your for loop.
See here, I've cleaned up your code a bit to make it easier to read.
#include <iostream>
usingnamespace std;
int main()
{
int n=0, x=0, max=0, min=0;
cout << "Please enter a number: "; // Comment what code does
cin >> n;
cout << endl;
for (int i=0; i < n; i++) //(int i; i < n; i++) Error-1
{
cout << "Please enter each number you want to progress: ";
cin >> x;
max = min = x;
if( x > max ) max = x; // Resets Max
if( x < min ) min = x; // Resets Min
}//end for
cout << "Max: " << max;
cout << "\nMin: " << min << endl;
cin.ignore(); // Pause
cin.get();
return 0;
}
What is it that you want your code to do. As of right now, you are iterating through with a for loop, however, your only just getting one value, storing it within x, and printing out the max or min if it is either higher or lower than one or the other. I assume you want to store them within an array or something and have them all printed out, but I'm not sure.
You can initialize min and max. Min would be initialized to a large number, max to a low number. I might use the constants in <climits> http://www.cplusplus.com/reference/climits/
Then in the for loop have them enter x and see if x is less than (or equal to) min or greater than (or equal to) max, updating max and min accordingly if there is a new high or low number so far.
Your max will set correctly. However, your min will not. If you initialize it to zero, your min will always be zero. One way to solve this, is to initialize Max to zero, as it will be the lowest "positive" number that you will have, and set Min to a ridiculously high number, in case someone plans to put ridiculously high numbers within your input.
#include <iostream>
usingnamespace std;
int main()
{
int n=0, x=0;
int max=0, min=999999; // --------------Initializing variables
cout << "Please enter a number: "; // Comment what code does
cin >> n;
cout << endl;
for (int i=0; i < n; i++) // (int i; i < n; i++) Error-1 ---- See formatting
{
cout << "Please enter each number you want to progress: ";
cin >> x;
//max = min = x; ------------- Remove
if( x > max ) max = x; // Resets Max
if( x < min ) min = x; // Resets Min
}//end for
cout << "\nMax: " << max;
cout << "\nMin: " << min << endl;
cin.ignore(); // Pause
cin.get();
return 0;
}
It's not a bother. I assumed that it is for positive numbers only.
Also, make sure that you do proper formatting all of the time, your compiler may allow some things that many compilers won't. Follow ANSI standards and you won't have that problem ever. If you get a job debugging code, you will wish everyone followed the standards lol. Especially engineers and scientists who don't code often and use off the wall compilers for some reason....
If you really don't like it, try notepad++ and use a GNU g++ compiler via the command line. I use the g++ pretty often as well as the VS 2010 for all of my c++ stuff. I use Netbeans for java.