Question

C++ code please: Write a program to count the number of positive and the number of...

C++ code please:

Write a program to count the number of positive and the number of negative inputs values. The program will stop when a 0 is input.

Ex: If the input is

-5.5 568 2.332 0

the output is

2 positive number(s) and 1 negative number(s).

Ex: If the input is

153.0 0.534 2.2 5.6 46.584 0.015 5 0

the output is

7 positive number(s) and 0 negative number(s).

Homework Answers

Answer #1

#include<iostream>
#include<sstream>
#include<string>
using namespace std;
int main(){
double number;int pos=0,neg=0;
string input;
cout<<"Enter the numbers : \n";
getline(cin,input);
stringstream ss(input);
while(!ss.eof()){
ss>>number;
if(number>0) pos++;
else if(number<0) neg++;
else {
cout<<pos<<" Positive number(s) "<<neg<<" Negative number(s)\n\n";
break;
}
}

return 0;
}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
6.31 LAB: Count characters - methods ----- javascript please Write a program whose input is a...
6.31 LAB: Count characters - methods ----- javascript please Write a program whose input is a character and a string, and whose output indicates the number of times the character appears in the string. Ex: If the input is: n Monday the output is: 1 Ex: If the input is: z Today is Monday the output is: 0 Ex: If the input is: n It's a sunny day the output is: 2 Case matters. n is different than N. Ex:...
3.plese to write the correct code: The code below will loop as long as the number...
3.plese to write the correct code: The code below will loop as long as the number that you enter isn’t negative. It will add each non negative number to the current sum. It will print out the sum and average of all of the numbers you have entered. But someone jumbled up the code. Can you reorganize and indent it so it works properly? count = count + 1 message = "Enter an integer or a negative number to stop"...
Please write the code in Python. Write a program/function in any Object-Oriented programming language that will...
Please write the code in Python. Write a program/function in any Object-Oriented programming language that will implement Queue Abstract Data Type with the following functions/methods.  Any build-in/pre-defined Queue function/library (e.g., java.util.Queue in Java) is NOT allowed to use in your code. push(Element):  insert the input Element (e.g., String or Integer in Java) to the end of the queue. pop(): remove the head element of the queue and print the head element on screen. count():  return the total number of elements in the queue...
(C++) Write a program whose input is two characters and a string, and whose output indicates...
(C++) Write a program whose input is two characters and a string, and whose output indicates the number of times each character appears in the string. Ex: If the input is: n M Monday the output is: 1 1 Ex: If the input is: z y Today is Monday the output is: 0 2 Ex: If the input is: n y It's a sunny day the output is: 2 2 Case matters. Ex: If the input is: n N Nobody...
Draw a flowchart for a computer program to receive 40 numbers and output “yes” if number...
Draw a flowchart for a computer program to receive 40 numbers and output “yes” if number of negative values among the first 10 inputs is equal to number of positive values among the last 10 inputs. Note. The 20 numbers in middle are ignored. For instance, if the values are: 1,-1,1,1,-1,1,-2,5,1,1,4,4,5,-6,5,8,-12,0,2,0,1,2,3,7,7,-7,0,1,1,1,3,4,1,-1,-1,-1,-1,-1,-1,-1 the program should output “yes” since the number of negative values in the first 10 numbers is 3, and the number of positive values in the last 10 numbers...
In Java. Write a program that reads-in a times table-number. The program, using this table-number will...
In Java. Write a program that reads-in a times table-number. The program, using this table-number will produce the following report (as shown). The first item in the report is a number with starting value 1. Second column is the word “ X ” (representing the times symbol). Third column is the table-number (itself). Following is an equal sign “ = “ (representing a result). Last column is the result of the operation for that line or row which is the...
Please Write code in visual studio Roman Numeral Converter Write a program that asks the user...
Please Write code in visual studio Roman Numeral Converter Write a program that asks the user to enter a number within the range of 1 through 10. Use a switch statement to display the Roman numeral version of that number. Input Validation: Decide how the program should handle an input that is less then 1 or greater than 10.
Lab 6    -   Program #2   -   Write one number to a text file. Use the write()...
Lab 6    -   Program #2   -   Write one number to a text file. Use the write() and read() functions with binary                                                        data, where the data is not char type.              (Typecasting is required) Fill in the blanks, then enter the code and run the program. Note:   The data is int type, so typecasting is            required in the write() and read() functions. #include <iostream> #include <fstream> using namespace std; int main() {    const int SIZE = 10;   ...
Write a program to count number between 0 and 255 in binary using 8 LEDs connected...
Write a program to count number between 0 and 255 in binary using 8 LEDs connected to the Arduino. This code should start at 0 and loop back to 0 once it counts to 255. Delay 0.25s between counts.
C program- using visual studio code. Write a program that asks for an integer and then...
C program- using visual studio code. Write a program that asks for an integer and then prints all the alternative integers from (and including) that value up to (and including) a value larger by 20. (That is, if the input is 5, the output runs from 5 to 25.) Be sure to separate each output value by a space or tab