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 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."
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
//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;
}
}
//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:
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)
//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
#include <iostream>
#include <vector>
#include <algorithm>
usingnamespace 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.