Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C++ (http://www.programmingforums.org/forum15.html)
-   -   input data from user into array (http://www.programmingforums.org/showthread.php?t=10003)

lilmul123 May 26th, 2006 9:43 AM

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.

Ooble May 26th, 2006 9:52 AM

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];
}


lilmul123 May 26th, 2006 10:09 AM

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();
}


nnxion May 26th, 2006 10:15 AM

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.

lilmul123 May 26th, 2006 10:24 AM

thanks for the help! i got it

DaWei May 26th, 2006 10:29 AM

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.


All times are GMT -5. The time now is 7:51 AM.

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