Hi....I got a simple question here which i dont know how to solve...

Question
Type casting is common in writing programs. It converts from one data type to another, for example from integer to double or float. Write a complete C++ program which uses casting, which will ask the quantity of integers the program will receive from the user. The program will then ask the end user to enter series integers. The program will add up all of the integers, find its average and the multiplication of 2 of its average. Below is the sample output of the program. Please ensure your program is error proof (eg. you cannot enter -3 integers).

How many integers do you want to enter? 3
Please enter 3 integers separated by a space: 5 4 9
The sum of the numbers is 18
The average of the numbers is 6.00
The multiplication of 2 of the average is 12.00

How could i make the program that could limit the input integer the user try to enter,and how to make the program calculate the sum and all of that??I've tried to write the code,but i think there must be something that i need to know to write this which i dont know yet..could you tell me what it is??

Thank you...

## All 8 Replies

Have you covered while loops? Can you make a program that takes in 5 integers? It will be very similar. There's only one cast (AFAIK) that you need to make. Just take it a step at a time and code something...

How could i make the program that could limit the input integer the user try to enter

``````int x=0;

//Force user compliance..!!!
do{
cout << "Enter a number between 1 and 20: ";
cin >> x;

if(x<1 || x>20)
{
cout "\aNumber out of range, try again!";
}
}while(x<1 || x>20);``````

and how to make the program calculate the sum and all of that??

``````cout << "How many numbers do ye' wish to enter? ";
cin >> amount;

// "Dynamic Array", You can create an array of whatever size you want
int* integers = new int[amount];

//After your array has been populated, you can perform a summation:
for(int i=0; i<amount; i++)
{
//perform accumulation
average += integers[i];
}

//calculate average
average /= amount;

//display useful information (with a cast as per your assignment requirement)
cout << "The average of entered integers is: " << static_cast<double>(average);``````

CP what if average is 29 and amount is 30 (I know in this example it wouldn't be)? Casting it still won't cure the roundoff. I know he knows how to do this, that's why I chose not to give him any starter code.

jonsca...
ya i know how to do if just wanna ask the user to enter the 5 integer..
but in this case,the question seems to give the user instruction to enter how many integer that he/she would like to enter...
So how i suppose to do the average and all others if i dont know to which variable the input that user entered go into???

ops...CP..
I knew that it could be coding using the array...
and it would be simple if using that..
but seems my lecturer did not teach the array topic yet,the way how this question should be answered i think surely not allow me to use the array...

So, in this case...I just have no idea what else i suppose to use to replace the array???
I just cant figure it out..

Help me please,thank you...

Get the number of entries first. Use a while loop to get that many inputs. Keep a running sum of all the numbers (nothing needs to be stored). At this point you have an integer total and an integer count. Give you a clue, if you have (1+0+1+2)/5 = 0. Do a quick net search on casting.

I'm so sorry..
but it looks like i still cant get the idea that you told me....

I've tried to figure it out,but still i cant....
I realized now that my knowledge about the programming is still weak since i not even can answer this simple question...

you said that i need to use the while loop to get many inputs right??
how could i really do this??what should i write in the while statement if it is like this :

``````int main()
{
cout<<"Enter series of integers :";
int integer;
cin>>integer;

while (.........) {

...................................
}``````

I'd try a for loop。

``````int main()
{
cout<<"Enter series of integers :";
int integer;
cin>>integer;
。。。。。。。``````

This is definitly a step in the right direction， you just need to use integer to know how many times to go through your loop that asks for input。

I'm so sorry..

I realized now that my knowledge about the programming is still weak since i not even can answer this simple question...

This is not true at all. You have a lot of knowledge you need, and you need to be able to incorporate it all in one place. Bamcclur is correct you could preferably use a for loop (I kinda had my mind stuck on you needing an unknown number of entries hence the while). So in your case, take the users input, called numberofentries or something, use it as part of the condition of the for loop `for (int i = 0;i<numberofentries;i++)` . Within the for loop keep a running total (write it out on a piece of paper if it helps you -- draw your loop and run through it line by line keeping track of all the variables, then go through it x number of times).
I think your psyching yourself out here, it's not that involved.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.