Series output.

1
2
3
4
5
6
7
8
        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?

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

Do your comparison inside the loop.
Even if "cout << x" is inside the loop it will print each x but it won't compare each x.

look at the full code.

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
#include <iostream>
using namespace 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.

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
#include <iostream>

using namespace 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.
Last edited on
max = min = x;

max and min are getting set to the value of x, so x is never going to be greater or less than the others.
I can easily code a max or min comparison with arrays, I just want to make it without arrays, and also your code gives the same outputs as mine
I dont know what compiler or debugger you are using

"I am using netbeans"
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.
If you remove this line:
max = min = x;

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.

For example:

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
#include <iostream>

using namespace 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;
}
What if all negative numbers are entered?
worked.. thank you
I will also work on another way..sorry for bothering you
+1 for wildblue
we can initialize max = -9999999 also..
Last edited on
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....
I use Xcode..
School works with netbeans.. D:
thanks for the advice
Try using netbeans, its not bad.

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.
Topic archived. No new replies allowed.