Writing a program with specific numbers

Hey guys,

I'm trying to write a program that reads in an integer that is greater than 1,000 and less than 1,000,000. And then to display the number with a comma inserted where it belongs. I know how to prompt for the number but how do I only read in an integer from a specific range??


cout << "please enter a number";
cin >> num1

and how do I specifically insert the commas in the right place??

I would appreciate any sort of help or hints!
thanks in advance!
I would use a conditional, something like (assuming num1 is an int)

if (num1 >= 1000000 || num1 <= 1000){
    cout << "out of bounds, reenter";
    cin >> num1;

I would probably convert that to a string next. Once you convert the input to a string, I would probably write an output function with a counter and output character by character, using a counter which triggers a ',' output every time counter%3 == 0;
Hey fmdub I appreciate the help,
this is what I got

#include <iostream>
#include <cstdlib>
#include <cmath>
#include <string>

using namespace std;

int main ()

int num1; //user's input number

cout << "Please insert a number:" <<endl;
cin >> num1;

if (num1 >= 1000000 || num1 <= 1000){
cout << "The number you have entered is out of bounds, please reenter a"
<< " different number: ";
cin >> num1;

cout << num1 <<endl;

// End of program statements
cout << "Please press enter once or twice to continue...";
cin.ignore().get(); // hold console window open
return 0; // successful termination
// end main() *************************************************************

here's the problem :P
1) I still dont really understand when you say using the "counter"
2) I understand how it denies any thing outside the desired range, but when another number is inserted for the 2nd time, regardless of if it's inside or outside the range it goes through. How could I make it so, even if its the 2nd or 3rd time, if the user enters a number outside the wanted range it denies it and asks for a different number?

sorry for the questions! I'm learning alot though :D

thanks in advance!
Topic archived. No new replies allowed.