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.
ongpong 0 Newbie Poster
Recommended Answers
Jump to Postthat'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.
Jump to PostYou 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"; }
Jump to PostDo 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.
Jump to Postthats 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;)
Jump to Postyes thats right! dont just ask and ask, just try it first to do by yourself. try and try until you cry. ;)
All 19 Replies
ongpong 0 Newbie Poster
nexocentric 18 Junior Poster in Training
Undermine 31 Light Poster
jephthah 1,888 Posting Maven
Undermine 31 Light Poster
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster
jephthah 1,888 Posting Maven
ongpong 0 Newbie Poster
jephthah 1,888 Posting Maven
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster
ongpong 0 Newbie Poster
ongpong 0 Newbie Poster
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster
valtikz 36 Junior Poster in Training
Ancient Dragon commented: Exactly :) +36
ongpong 0 Newbie Poster
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster
ongpong 0 Newbie Poster
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.