Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 132,668 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,166 people online right now. Registration is fast and FREE... Join Now!




how to write a pgm where no. of values r provided by the user?

 
Reply to this topicStart new topic

how to write a pgm where no. of values r provided by the user?

dan_ram
post 30 Aug, 2007 - 06:07 AM
Post #1


D.I.C Head

**
Joined: 15 Aug, 2007
Posts: 56


My Contributions


what will be the program where the user gives the number of times values have to be taken in??the values must be stored in various locations(variables) and should be used elsewhere in the program...pls use only iostream.h
User is offlineProfile CardPM

Go to the top of the page

Louisda16th
post 30 Aug, 2007 - 06:15 AM
Post #2


 

Group Icon
Joined: 3 Aug, 2006
Posts: 1,790



Thanked 1 times

Dream Kudos: 755
My Contributions


Please show us your code. We will not do your homework for you.
User is offlineProfile CardPM

Go to the top of the page

dan_ram
post 30 Aug, 2007 - 06:21 AM
Post #3


D.I.C Head

**
Joined: 15 Aug, 2007
Posts: 56


My Contributions


CODE

int numItems;
int i;

cout << 'How many numbers will you enter?';
cin >> numItems;
int myArray[numItems];

for(i = 0; i < numItems; i++) {
cout << 'Please enter a number:';
cin >> myArray[i];
}

in this only single digit nos. can be stored..array is a character array rite?


This post has been edited by dan_ram: 30 Aug, 2007 - 06:25 AM
User is offlineProfile CardPM

Go to the top of the page

MorphiusFaydal
post 30 Aug, 2007 - 06:39 AM
Post #4


D.I.C Lover

Group Icon
Joined: 12 May, 2005
Posts: 1,086



Thanked 8 times

Expert In: Hardware, Networking

My Contributions


QUOTE(dan_ram @ 30 Aug, 2007 - 09:21 AM) *

CODE

int numItems;
int i;

cout << 'How many numbers will you enter?';
cin >> numItems;
int myArray[numItems];

for(i = 0; i < numItems; i++) {
cout << 'Please enter a number:';
cin >> myArray[i];
}

in this only single digit nos. can be stored..array is a character array rite?


The declaration for the array you used is int myArray[numItems].. That's an integer array, storing integers. If you wanted a character array, you'd switch the int for char.
User is offlineProfile CardPM

Go to the top of the page

Bench
post 30 Aug, 2007 - 07:43 AM
Post #5


D.I.C Addict

Group Icon
Joined: 20 Aug, 2007
Posts: 602



Thanked 10 times

Dream Kudos: 150

Expert In: C/C++

My Contributions


You can't use a non-const variable to create a C-style array like that. If the size of an array is not known at compile time, you need to use a container that automatically resizes itself, such as a vector (a vector is a 'C++ array') or allocate the array onto the heap with the new operator

CODE
#include <iostream>
#include <vector>

int main()
{
    using namespace std;

    //The vector resizes itself automatically,
    // depending how many elements you add.
    vector<int> my_vec;
    int num_items;

    cout << "How many items of data? ";
    cin >> num_items;

    for(int i(0); i < num_items; ++i)
    {
        int temp;
        cout << "Enter a number: ";
        cin >> temp;

        //push_back adds 'temp' to the end, increasing the
        //  size of the vector by 1.
        my_vec.push_back(temp);
    }
}


This post has been edited by Bench: 30 Aug, 2007 - 07:44 AM
User is offlineProfile CardPM

Go to the top of the page

MorphiusFaydal
post 30 Aug, 2007 - 10:24 AM
Post #6


D.I.C Lover

Group Icon
Joined: 12 May, 2005
Posts: 1,086



Thanked 8 times

Expert In: Hardware, Networking

My Contributions


QUOTE(Bench @ 30 Aug, 2007 - 10:43 AM) *

You can't use a non-const variable to create a C-style array like that. If the size of an array is not known at compile time, you need to use a container that automatically resizes itself, such as a vector (a vector is a 'C++ array') or allocate the array onto the heap with the new operator


D'oh! How could I have forgotten about that? confused.gif
User is offlineProfile CardPM

Go to the top of the page

dan_ram
post 1 Sep, 2007 - 07:44 AM
Post #7


D.I.C Head

**
Joined: 15 Aug, 2007
Posts: 56


My Contributions


thnks evry1 for the info i got...as we hvnt been taught all these, i myself found out the answer which apparently turned out to be a while loop where the the no. of times can be taken into an integer(variable), say 'a' and the loop can be run till another integer, say 'i' which is 0 at the beginning and keeps increasing with every loop equals 'a'.
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/23/08 05:57AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month