Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how do you write a code for displaying the number of numbers entered 1

Status
Not open for further replies.

clip123

Technical User
Feb 10, 2002
10
US
program that will accept up to 10 integers depending on user:
1. display number of numbers entered
2. numbers entered
 
You probably would want to use an array of 10 integers to store the values and then use like a count variable to count how many times the user enters a number. Hope that helps. If you need more help let me know.

-CDuddley
 
When you talk about count variable are you talking about using a for loop?


 
Well not the count variable in the for loop setup; although, you could probably do that. I was thinkging like a variable count that you increment every time. Sort of like this.

#include <iostream.h>

int main()
{

int count;
int iarray[10];

// Then put your code for prompting the user for the
// numbers

// Everytime they enter a number
count++;

// Then use the count variable to tell them how many
// numbers they entered

return 0;
}

Hope that helps some.

-CDuddley

 
#include <iostream>
using namespace std;

void main()
{
int n, count;
double sum=0, average;
int x[10];

cout << &quot;How many numbers do you want to enter not exceeding 10? &quot;;
cin >> n;

for (count=0; count<=n-1; count++)
{
cout << &quot;Number that you entered is: &quot; ;
cin >> x[count];
sum += x[count];
}

cout << &quot;\n\nYou entered &quot; << n << &quot; &quot; <<&quot;numbers\n\n&quot;;

cout << &quot;These are the numbers that you entered: &quot; << endl;
for(int counts=0; counts<n; counts++)
{

cout << counts << &quot; &quot;;
}
cout << endl;
cout << &quot;\nThe sum is &quot; << sum << endl;
average = sum / n;
cout << &quot;\nThe average is &quot; << average << endl << endl;
return;
}//end main


This is my code so far but the problem is that when im supposed to display the values that were entered they don't print those out instead they display the numbers 0, 1, 2, 3 if I were to say enter only 4 numbers. Can you show me what I am doing wrong?
 
Change:
cout << counts << &quot; &quot;;
to:
cout << x[counts] << &quot; &quot;;


 
Are you wanting to just count to 10 or actually let them enter some numbers and you repeat them back to them? Cause right now it looks like you just looping from 0 - 9 and displaying the the sum of array garbage. When I say garbage I mean you don't have anything in that array, so it could be anything. Is that what you want to do? Or do you want the to enter like 54 and you tell them that's what they entered, and then tell them they entered 1 number? If you want to do that, put in a loop that prompts them for a number and store them in that x array. For example,

for(count = 0; count < n - 1; count++)
{
cout<< &quot;Enter a number: &quot; << endl;
cin<< x[count]; // that should store them in the array
}

Hope that helps.

-CDudd
 
The program sort of works now but I found that there is still one more problem. The problem is that I would like to restrict them from entering more than 10 numbers. But when i executed the program and typed in 11 numbers to enter it accepted it. I was wondering is there a way to make it so that if they do type in 11 or more that it will prompt the user that they have entered a number more than 10 and to please enter a number 10 or smaller.

Once again thank you for all your help so far.
 
Post the code you have. I'll try and see if I can fix it.

-CDudd
 
#include <iostream>
using namespace std;
void main()
{
int n, count; //declaration
double sum=0, average;
int x[10]; //declare array’s elements as type int

cout << &quot;How many numbers do you want to enter not exceeding 10? &quot;;
cin >> n; //accept integer

for (count=0; count<=n-1; count++)
{
cout << &quot;Enter number: &quot; ; //print out enter number
cin >> x[count]; //accept integer
sum += x[count]; //assign integer to sum

}

cout << &quot;\n\nYou entered &quot; << n << &quot; &quot; <<&quot;numbers\n\n&quot;;

cout << &quot;These are the numbers that you entered: &quot; << endl;
for(int counts=0; counts<=n-1; counts++)
{

cout << x[counts] << &quot; &quot;; //displays integers that were entered
}

cout << endl;
cout << &quot;\nThe sum is &quot; << sum << endl; //displays the sum of integers
average = sum / n; //calculates average of integers
cout << &quot;\nThe average is &quot; << average << endl << endl; //displays average

return;
}//end main
 
#include <iostream>
using namespace std;
void main()
{
int n, count; //declaration
double sum=0, average;
int x[10]; //declare array’s elements as type int

cout << &quot;How many numbers do you want to enter not exceeding 10? &quot;;

while(n > 10) // see if this fixes it
cout << &quot;How many numbers do you want to enter not exceeding 10? &quot;;

cin >> n; //accept integer

for (count=0; count<=n-1; count++)
{
cout << &quot;Enter number: &quot; ; //print out enter number
cin >> x[count]; //accept integer
sum += x[count]; //assign integer to sum

}

cout << &quot;\n\nYou entered &quot; << n << &quot; &quot; <<&quot;numbers\n\n&quot;;

cout << &quot;These are the numbers that you entered: &quot; << endl;
for(int counts=0; counts<=n-1; counts++)
{

cout << x[counts] << &quot; &quot;; //displays integers that were entered
}

cout << endl;
cout << &quot;\nThe sum is &quot; << sum << endl; //displays the sum of integers
average = sum / n; //calculates average of integers
cout << &quot;\nThe average is &quot; << average << endl << endl; //displays average

return;
}//end main
 
the while line didn't work cause when i entered 11 the program still accepted it.
 
#include <iostream.h>

int main()
{
int n, count; //declaration
double sum=0, average;
int x[10]; //declare array’s elements as type int

cout << &quot;How many numbers do you want to enter not exceeding 10?&quot;;
cin >> n; //accept integer

while(n > 10){
cout << &quot;How many numbers do you want to enter not exceeding 10?&quot;;
cin >> n;
}

for (count=0; count<=n-1; count++)
{
cout << &quot;Enter number: &quot; ; //print out enter number
cin >> x[count]; //accept integer
sum += x[count]; //assign integer to sum

}

cout << &quot;\n\nYou entered &quot; << n << &quot; &quot; <<&quot;numbers\n\n&quot;;

cout << &quot;These are the numbers that you entered: &quot; << endl;
for(int counts=0; counts<=n-1; counts++)
{

cout << x[counts] << &quot; &quot;; //displays integers that were entered
}

cout << endl;
cout << &quot;\nThe sum is &quot; << sum << endl; //displays the sum of integers
average = sum / n; //calculates average of integers
cout << &quot;\nThe average is &quot; << average << endl << endl; //displays average

return 0;
}//end main

That should do it for ya. I compiled in VC++, and it seemed to work well. Have fun :).

-CDudd
 
hey thanks CDudd the program works fine now.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top