![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Mar 2006
Posts: 18
Rep Power: 0
![]() |
input data from user into array
How do I ask the user to input some data, and put it into different parts of an array?
Today, I have an assignment that requires me to have the user enter a digit, then ask if they want to do it again, and then find the average of the numbers entered. I have to do this using an array of up to 15 numbers. |
|
|
|
|
|
#2 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
Use a loop?
'Cos it's an assignment, I'm not going to give you the code. I'll explain what I'd do though: loop through fifteen times, asking the user for input. If at any point the user decides he doesn't want to continue, simply break out of the loop. To put data in a specific part of an array: for (int i = 0; i < ARRAY_SIZE; i++)
{
cin >> my_array[i];
} |
|
|
|
|
|
#3 |
|
Newbie
Join Date: Mar 2006
Posts: 18
Rep Power: 0
![]() |
here's what I have thus far. the problem im having now is with the do-while loop:
#include "conio.h"
#include "stdio.h"
#include "iostream.h"
int main()
{
int userinput;
int i;
int number[15];
char choice;
clrscr();
do{
for ( i = 0; i < 15; i++)
{
cout << "Please enter a number:\n";
cin >> userinput;
number[i]=userinput;
}
{
cout << "Do you want to enter another number?";
cin >> choice;
}
while(choice=='y');
getch();
} |
|
|
|
|
|
#4 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5
![]() |
Brace mismatch, helps when you indent like:
#include <iostream>
using namespace std;
int main()
{
int userinput, number[15];
char choice;
do
{
for (int i = 0; i < 15; i++)
{
cout << "Please enter a number:\n";
cin >> userinput;
number[i] = userinput;
}
cout << "Do you want to enter another number?";
cin >> choice;
}
while(choice == 'y');
cin.get();
}
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for." -- Socrates |
|
|
|
|
|
#5 |
|
Newbie
Join Date: Mar 2006
Posts: 18
Rep Power: 0
![]() |
thanks for the help! i got it
|
|
|
|
|
|
#6 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Just some additional blather, along the lines of one of Ruben's discoveries. You should probably, while a novice, line your braces up. It's a stylistic issue, true, but a visual aid helps. You should probably use them even for single-line blocks for a while. You should definitely indent so the nesting is obvious. An editor that can indicate matching braces is nice, also.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|