how can i compute a code if i enter numbers then if i press any key its stops and computes the inputs of all no.

this is my code:

#include <iostream>
using namespace std;
int main()
{
int y,s[100];



cout << "Enter a Number: ";cin >> s[y];
cout<< y;
}

i dont know how to solve my problems

What you might want to do is use two loops. One loop for storing each number, and another for printing each number.

#include <iostream>
using namespace std;
int main()
{
	int y, s[100];

	for ( y = 0 ; y < sizeof(s); y++ ) {
		cout << "Enter a Number: ";
                cin >> s[y];
	}

	for ( y = 0; y < sizeof(s); y++ ) {
		cout << s[y];
	}
}

But this still leaves you with a few more problems to solve.
1) What if the user only wants to input 3 numbers? What if they want to input 9 numbers? How are you going to break the loop?

2) How are you going to print out numbers if the user only input 3 numbers? 20 numbers? You'll only want to output the numbers the user actually entered.

I would do something different and use a sentinel value to terminate input. Loop until users stops the loop.

int value, count=0, sum=0, total;
    
    for(;;) // out forever loop
    {
        for(;;) // 
        {
            cout << "Enter value " << count+1 << ": ";
            value = -9999;
            cin >> value;
  
        
          
            cin.clear();
            cin.sync();

            if ( value == -9999 ) { cout << "Bad data entry ... "; continue; }
            if ( value ==  9999 ) break; // break out of inner loop
            
++count;

total= sum += value;
average= total/count;         
                       
                      
    
           
        } // end of inner forever loop ...
        
        if (value == 9999 ) break; // break out of outer loop
      
   
    
    } // end of outer forever loop ...

that's dumb.

what if he wants to add 9999 as one of his numbers

Cool thanks for coming in offering no valuable input of your own just destructive criticism. If choosing 9999 is a problem he could use a different sent value it's just a suggestion.

Moron.

that's dumb.

what if he wants to add 9999 as one of his numbers

That's a common way to terminate user input, another is to type Ctrl+Z. If he needs to let the user enter 9999 then his program should choose a different number to terminate input.

okay. sorry. his implementation of the basic sentinel concept is just fine.

but a calculator that spontaneously shuts down when you try to add 9999... well, it just isn't going to sell really well.

i'm just saying

what if the user try to enter lots of no. and then press a key maybe "b" to stop and it generate the sum of all the users input thats anybody know simply codes :) thanks im still dumb in c++

what if the user try to enter lots of no. and then press a key maybe "b" to stop and it generate the sum of all the users input

i think that's a good idea.

use a non-numeric character (like 'b' or whatever) as the sentinel to stop taking input

and if you want to get fancy, you could use the '+', '-', '=' characters as inputs to perform the functions you want to do.

i mean, isnt that what you're trying to do here? program a basic adding machine?


.

You could use any non-numeric character as the terminator.

int main()
{
    long n;
    long total = 0;
    while(cin.good())
    {
        cout << "Enter a number or 'n' to stop\n";
        cin >> n;
        total += n;
    }
    cout << "total = " << total << "\n";
}

so do i use if else statement or do while in hitting a button to stop and show the result of inputed no.

thats nice ancient dragon how about the total is divided by the times you input no.


long n is divided by how many times you input a no. :)

Do you see an if statement in the code I posted? If you don't understand how it works then compile and run it yourself. When you are done typing just enter any non-numeric character, doesn't matter what you type as long as its not a digit.

thats nice ancient dragon how about the total is divided by the times you input no.


long n is divided by how many times you input a no. :)

I'll leave that up for you to do. I don't want to spoil all your fun;)

yes thats right! dont just ask and ask, just try it first to do by yourself. try and try until you cry. ;)

im still a newbie im just asking :(

i already complie and it run well ancient dragon i just want to ask last how if i want to divide the inputed no. by the times i input it :)

add another int variable and increment it inside that loop -- that will count the number of iterations that loop made. Then do the math after that loop terminates.

You could use any non-numeric character as the terminator.

int main()
{
    long n;
    long total = 0;
    while(cin.good())
    {
        cout << "Enter a number or 'n' to stop\n";
        cin >> n;
        total += n;
    }
    cout << "total = " << total << "\n";
}

theres an error while im doing this the last value is added the computation isnt exact :)

just after cin make a test for cin.good(). The program is adding n to total whether cin.goo() is true or not.

Or you could reset n to 0 before the cin line.

This question has already been answered. Start a new discussion instead.