Help with input 10 # output in numerical order

Create a program that will accept 10 numbers between 1 and 20 as input. If the user enters an invalid number re-prompt with an error message for the number. Output the list of numbers in numerical order.

#include <iostream>
#include <vector>

using namespace std;

int main()

{
vector <int> number;
int x;

for ( int n=0; n<10; n++)
{
cout <<"Enter a number between 1-20 :";
cin >> x;
number.push_back(x);
}

sort (number.begin(),number.end()); // sorting

cout <<"\n Print integers in ascending order :"<< endl;

for ( int i=0; i<number.size(); i++)
{
cout <<"\n"<<number.at(i) << endl;

}
system("PAUSE");
return 0;

}

i figured out how to enter 10 numbers but the numerical order is throwing me off and the invalid numbers outside of 1-20. please help! im very new to programming and confused
To use std::sort, you must #include <algorithm> .

To handle invalid input, you need to put some sort of check on your input before you add it to your vector. So in place of your for loop that gets user input, do something along these lines:
while (number vector has less that 10 items in it)
    output: "Enter a number between 1-20: "
    read an input into x
    if ( x >= 1 AND x <= 20 )
        add x to the number vector
    else
        output: "Bad input: Try again."
Last edited on
Thank You booradley60 i will try that

I tried to insert that into my code and i got errors i think i botched the job im trying different ways of entering it to see my errors i will post code soon i hope
Last edited on
i think i am going off track but this is what i have come up with

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main()

{
vector <int> number;
int x;

while (vector <10> number)
cout<< "Enter a number between 1-20: ";
vector <int> number;
int x;
if ( x >= 1 AND x <= 20 )

else
cout<< "Bad input: Try again.";

{
cout <<"Enter a number between 1-20 :";
cin >> x;
number.push_back(x);
}

sort (number.begin(),number.end()); // sorting

cout <<"\n Print integers in ascending order :"<< endl;

for ( int i=0; i<number.size(); i++)
{
cout <<"\n"<<number.at(i) << endl;

}
system("PAUSE");
return 0;

}
Okay i started from scratch this is my new code i still dont know how to keep the numbers between 1-20

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main(void){
vector<int> vec;
int x;

for(int i=0;i<10;i++){
cout << "Please enter your number: ";
cin >> x;
vec.push_back(x);
}
sort(vec.begin(), vec.end());
for(int i=0;i<10;i++)
cout << vec[i]<<" ";


system("PAUSE");
return 0;
}
Last edited on
I'm going to translate the pseudocode I gave you into real code line by line. This code is intended to replace your for loop that reads in numbers:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
    //while (number vector has less that 10 items in it)
    while(vec.size() < 10)
    {

        //output: "Enter a number between 1-20: "
        cout << "Enter a number between 1-20: ";

        //read an input into x
        cin >> x;

        //if ( x >= 1 AND x <= 20 )
        if( x >= 1 && x <= 20)
        {
            //add x to the number vector
            vec.push_back(x);
        }
        //else
        else
        {
            //output: "Bad input: Try again."
            cout << "Bad input: Try again." << endl;
        }
    }
#include <iostream>
#include <vector>
#include <algorithm>


using namespace std;

int main()

{

vector <int> number;
int x;

while(vec.size() < 10)
{

//output: "Enter a number between 1-20: "
cout << "Enter a number between 1-20: ";

//read an input into x
cin >> x;

//if ( x >= 1 AND x <= 20 )
if( x >= 1 && x <= 20)
{
//add x to the number vector
vec.push_back(x);
}
//else
else
{
//output: "Bad input: Try again."
cout << "Bad input: Try again." << endl;
}

sort (number.begin(),number.end()); // sorting

cout <<"\n Print integers in ascending order :"<< endl;

for ( int i=0; i<number.size(); i++)
{
cout <<"\n"<<number.at(i) << endl;

}
system("PAUSE");
return 0;

}
This is the code i have assembled and it gives me the errors:

in function int main()
Vec undeclared
Well, you've once again changed how you've declared your vector.
Now it is declared vector <int> number;
In the post before mine, you declared it vector<int> vec;
Pick one and stick with it. It looks like you want to call it number now, so change each occurrence of vec to number, i.e. instead of saying
while(vec.size() < 10) you will now say
while(number.size() < 10)

And use code tags, please. http://www.cplusplus.com/articles/jEywvCM9/
Last edited on
new to this forum business thank you for your guidance and knowledge.
#include <iostream>
#include <vector>
#include <algorithm>


using namespace std;

int main()

{

vector <int> number;
int x;

while(number.size() < 10)
{

//output: "Enter a number between 1-20: "
cout << "Enter a number between 1-20: ";

//read an input into x
cin >> x;
}
//if ( x >= 1 AND x <= 20 )
if( x >= 1 && x <= 20)
{
//add x to the number vector
number.push_back(x);
}
//else
else
{
//output: "Bad input: Try again."
cout << "Bad input: Try again." << endl;
}

sort (number.begin(),number.end()); // sorting

cout <<"\n Print integers in ascending order :"<< endl;

for ( int i=0; i<number.size(); i++)
{
cout <<"\n"<<number.at(i) << endl;

}
system("PAUSE");
return 0;

}


This Code will compile but it runs the same loop over and over an it allows you to enter any number
Use code tags.
Indent your code sensibly.
Here's your code with indentation and line numbers. (Much 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
33
34
35
36
37
38
39
40
41
42
43
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
    vector <int> number;
    int x;

    while(number.size() < 10)
    {
        //output: "Enter a number between 1-20: "
        cout << "Enter a number between 1-20: ";

        //read an input into x
        cin >> x;
    }
    //if ( x >= 1 AND x <= 20 )
    if( x >= 1 && x <= 20)
    {
        //add x to the number vector
        number.push_back(x);
    }
    //else
    else
    {
        //output: "Bad input: Try again."
        cout << "Bad input: Try again." << endl;
    }

    sort (number.begin(),number.end()); // sorting

    cout <<"\n Print integers in ascending order :"<< endl;
    for ( int i=0; i<number.size(); i++)
    {
        cout <<"\n"<<number.at(i) << endl;

    }
    system("PAUSE");
    return 0;
}


The bracket on line 19 should be on line 32. Can you explain why? Mentally step through your code and understand why the placement of the closing bracket matters.
Last edited on
Topic archived. No new replies allowed.