I looked in the contents here but can't find any thing on this particular problem...killingthemonkey's post is close but not quite!!
I want to create a program that asks the user to enter a pre-determined amount of numbers, then they actually enter the numbers. My output should display tbout 3 or four ranges that contain their number and a caveat that identifies any numbers entered outside this range. My attempts so far have been pitiful. . .
#include<iostream>
using namespace std;
int main ()
{
int numbers[100];
int a,b,c;
b=0;
int range[4]; //allowing for 4 ranges within the array
range[0]=0; //initialising the ranges to 0
range[1]=0;
range[2]=0;
range[3]=0;
cout<<"How many numbers will be entered? ";
cin>>numbers[a];
cout<<"Enter the numbers: ";
cin>>range[b];
for (b=0;b<25;b++) // to display numbers within the first range
{
cout<<"The numbers are: "<<range[3];
}
return 0;
}
I've only shown the first range code because I generally try to do the first then duplicate for the rest with suitable changed variables of course.
I am a beginner and yes this is part of a project I have to do but our teacher is one of the superior beings that doesn't understand that beginners really have to work their heads around this material. Also the course that I am doing seems severely condensed and seems to skip some basic stuff. . . .I am reading everything I can find to teach myself but so far no luck. . .hope you guys can help me. ..
Let's start with how cin works. Here's a small example:
1 2 3 4
// declare a variable to hold one int
int number;
// accept a number followed by a newline from the user and put it in number
cin >> number;
Note that if the user would type "45 51 38" and then enter, cin would only grab the first consecutive numeric digits storing only 45 in number.
If you want to accept 10 numbers, you could use cin 10 times. If you want to accept a line of space-delimited numbers all at once, you would have to read a string and then parse out the numbers explicitly afterward (a little more work).
Next, let's look at a simple array:
1 2 3 4 5 6 7 8 9
// declare an array of 5 ints (indices 0-4)
int numbers[5];
// assign values to the individually indexed ints
numbers[0] = 10;
numbers[1] = 11;
// ...snip...
numbers[4] = 14;
// accept a single number from the user and store it in numbers[0]
cin >> numbers[0];
The array of 5 integers, numbers, only holds a single int per index. I'm not sure if this is repetative for you, or not, but I hope something here helps out.
Thanks for the advice. . . but a little clarification request
First accepting space delimited numbers using cin. . .and leaving them on the screen so the user can see what he/she has input. . . yet being able to access the input numbers for the program to use later, how do you do that?
Secondly. . .can index(element) in an array can only fit one int . . .that is
number[0] holds only one int? is there not a way to allow it to hold well any amount ie a range? Perhaps to grab all numbers between 1 and 10 previously input by the user and ignore all others then display it?
#include <iostream>
#include <sstream>
string input;
cin >> input;
ostringstream parser( input );
int number[2];
parser >> number[0]; // first number
parser >> number[1]; // second number
In this case number is a two element array which holds 2 ints: the first number (referenced by number[0]) and the second number (referenced by number[1]). Arrays can hold multiple values, but each element (using the [index]) only refers to one.
int numbers;
cout<<"How many numbers will be entered? ";
cin>>numbers;
int range[numbers];
for( int i = 0; i < numbers; ++i )
{
cout<<"Enter number "<<i+1<<": ";
cin>>range[i];
}
Some compilers such as GCC will allow a variable-sized array. Not sure what compiler the poster is using. (Guessing not msoft which doesn't allow it since not including stdafx.h).