Welcome to Dream.In.Code
Become a C++ Expert!

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




negative numbers

 
Reply to this topicStart new topic

negative numbers, program should accept positive and negative numbers

celeste
13 Jan, 2008 - 01:08 AM
Post #1

New D.I.C Head
*

Joined: 13 Jan, 2008
Posts: 1

hi,i need help for make program prompt user to enter numbers and never stops till user enter negative numeber then get average from all numbers user enters????[size=3]


CODE
#include <iostream>
using namespace std;

int main()
{
    int num;
    while (num!= -)
    {
        cout<<" Enter numbers "<<endl;'
        cin>>num;
    
    }
    int avrg= num +num/num
        cout<<sum;
  return 0;
}



*mod edit (jjhaag) - please post your code using the code tags code.gif
User is offlineProfile CardPM
+Quote Post

PennyBoki
RE: Negative Numbers
13 Jan, 2008 - 01:22 AM
Post #2

system("revolution");
Group Icon

Joined: 11 Dec, 2006
Posts: 2,009



Thanked: 5 times
Dream Kudos: 500
Expert In: Java,C++,C

My Contributions
Hi celeste, welcome.

I suggest you use an array of integers to store the values that would be entered by the user.
Therefore use a for loop instead of a while it is the same thing but I think it would be more neat with for loop.

something like

for(int i=0; num[i]>=0; i++)
{

}

But set all the values of the elements of the array to 0.

Then use another for loop to calculate the average. Hope this helps.

And yes next time please code.gif smile.gif

This post has been edited by PennyBoki: 13 Jan, 2008 - 01:23 AM
User is offlineProfile CardPM
+Quote Post

RodgerB
RE: Negative Numbers
13 Jan, 2008 - 01:37 AM
Post #3

D.I.C Lover
Group Icon

Joined: 21 Sep, 2007
Posts: 2,132



Thanked: 17 times
Dream Kudos: 2200
Expert In: Dot Net Technologies

My Contributions
Here is an example of how you can do this. Notice the changes on the while loop, the array, calculating the average and other self-explainatory changes. smile.gif

CODE

#include <iostream>
// We use this to determine the amount of elements in our array.
#define MAX_NUMBERS 1024
using namespace std;

int main()
{
    // Our array of numbers.
    int num[MAX_NUMBERS];
    // The amount of numbers counted.
    int num_count = 0;
    
    // It was annoying me, so I made it display once. :P
    cout << " Enter numbers " << endl;

    // Loop until the user enters a negative number.
    while (true)
    {
        if(!(num[num_count - 1] < 0))            
            cin >> num[num_count];
        else break;
        num_count++;
    }

    // Use a double instead of a float due to it's limitations.
    double avrg = 0.0;

    // Add all the numbers together.
    for(int i = 0; i < num_count - 1; i++)
        avrg += num[i];

    // Divide all the added numbers by the amount of numbers we have used.
    avrg /= num_count - 1;

    cout << avrg << endl;

    return 0;
}


Hope that helps. smile.gif

EDIT: Removed system("PAUSE"); as I know Amadeus would kill me for it. wink2.gif

This post has been edited by RodgerB: 13 Jan, 2008 - 02:40 AM
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/5/08 04:46AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month