Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old May 26th, 2006, 8:43 AM   #1
lilmul123
Newbie
 
Join Date: Mar 2006
Posts: 18
Rep Power: 0 lilmul123 is on a distinguished road
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.
lilmul123 is offline   Reply With Quote
Old May 26th, 2006, 8:52 AM   #2
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
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];
}
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old May 26th, 2006, 9:09 AM   #3
lilmul123
Newbie
 
Join Date: Mar 2006
Posts: 18
Rep Power: 0 lilmul123 is on a distinguished road
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();
}
lilmul123 is offline   Reply With Quote
Old May 26th, 2006, 9:15 AM   #4
nnxion
Programming Guru
 
nnxion's Avatar
 
Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5 nnxion is on a distinguished road
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();
}
I also removed the deprecated headers.
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for."
-- Socrates
nnxion is offline   Reply With Quote
Old May 26th, 2006, 9:24 AM   #5
lilmul123
Newbie
 
Join Date: Mar 2006
Posts: 18
Rep Power: 0 lilmul123 is on a distinguished road
thanks for the help! i got it
lilmul123 is offline   Reply With Quote
Old May 26th, 2006, 9:29 AM   #6
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 2:46 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC