I am trying to create a loop that allows the user to enter as many elements to the array as they would like then sum up those elements. I need the loop to terminate when the user enters a negative number. How would I go about terminating this?
double sum = 0;
double group[] = { 0 };
for (int i = 0; i >= 0; i++) {
cout << "Please enter employee salary. Enter negative number to end." << endl;
cout << "Employee " << i + 1 << ": $";
cin >> group[i];
if (i < 0) {
break;
}
sum += group[i];
}
cout << "The total salary ouput for Ernest Inc is: $" << fixed << showpoint << setprecision(2) << sum << endl;
I need the loop to terminate when the user enters a negative number.
For that a while loop would be better than for. You should also use vector which allows arbitrary number of items.
Something like this:
vector<double> group;
double salary;
while (true)
{
cout << "Please enter employee salary. Enter negative number to end." << endl;
cout << "Employee " << i + 1 << ": $";
cin >> salary;
if (salary<0)
{
break;
}
group.push_back(salary);
sum += salary;
}
Related
Hello im stuck at SUBTRACTION AND DIVITION AND I CANT FIGURE OUT WHAT CODE to use because when I try to subtract 10 i inputed it then it will loop since the while condition is not meet which it needs to be negative to terminate the loop and i inputed 2 for the second number then loop again then i putted -number which lead to terminate loop and subtract all the number but the result is -12 its always wrong in every number cant figure out why Please help
Also with divition, only my addition is working havent started the divition cuz i cant figure out how
#include <iostream>
using namespace std;
int amt2, total;
double subNumbers();
double amt=1;
double number=0;
int main() {
int chc=0;
int amt = 0;
int amt2 = 1;
cout << "Welcome again User!\n";
cout << "______________________________________________________________\n" << endl;
cout << "Mathematical Operations(Improved):\n\n";
cout << "\t[1]-Addition" << endl;
cout << "\t[2]-Subtraction" << endl;
cout << "\t[3]-Multiplication" << endl;
cout << "\t[4]-Division\n" << endl;
cout << "______________________________________________________________\n" << endl;
cout << "Type the number corresponding to your chosen operation: ";
cin >> chc;
```
switch (chc) {
case 1:
```
system ("cls");
cout << "\n\n\tOperation chosen: Addition";
cout << "\n______________________________________________________________" << endl;
cout << "\n\nInput positive numbers to use the operation and input a negative number to end the operation.\n\n";
cout << "Enter your number: ";`
cin >> number;
while (number >= 0) {
// add all positive numbers
amt += number;
// take input again if the number is positive
cout << "Enter another number: ";
cin >> number;
}
// display the sum
cout << "\nThe sum of all the numbers is: " << amt << endl;
break;
```
case 2:
system ("cls");
cout << "\n\n\tOperation chosen: Subtraction";
cout << "\n______________________________________________________________" << endl;
cout << "\n\nInput positive numbers to use the operation and input a negative number to end the operation.\n\n";
do{
cout << "Enter your number: ";
cin >> number;
amt=number-number ;
}while (number >= 0);// subtract all positive numbers
// display the difference
cout << "\nThe difference of all the numbers is: "<<amt;
return 0;
}}
```
enter code here
You are subtracting number from number:
amt = number - number; // Which is always 0
So that's why amt == 0 always.
So just change your loop to this:
while (true) {
cout << "Enter your number: ";
cin >> number;
if (number < 0) break;
if (amt == 0) amt = number;
else if (number >= 0) amt -= number;
}
What this does is that if amt == 0, then set amt to number. I have done this because as the default value of amt is 0 (due to int amt = 0;), when amt == 0, then we can assume that the user has entered the first number, and thus we can set amt to number. And then we can use -= operator, which basically means:
amt = amt - number;
But before all this, using if (number < 0) break; we can check if the user has entered a negative number, and if the user has entered a negative number, then the break keyword will break out of the while loop.
I need help adding up several user inputed values using C++
double total; // Accumulates total
double price; // Gets next price from user
int numItems; // Number of items
int counter = 1; // Loop control counter
cout << "How many items do you have? ";
cin >> numItems;
cout << endl;
while (counter <= numItems) {
total = 0;
cout << "Enter the price of item " << counter << ": ";
cin >> price;
cout << endl;
total += price;
counter++;
}
cout << "The total price is: " << total << endl;
When I run my code, I end up getting the sum of only one value from the user
Yes, just as Chetan Ranpariya said:
total = 0; should be before while loop.
My problem is that when i compile and run the program, the hours doesnt have "hours" listing 1,2,3 as the loop continues and also the loop calculation is the same for each line.
this is what the program looks like
http://postimg.org/image/htk194eah/
the calculation is wrong and the hours is suppose to say 1,2...5
I would like it to look something like this
http://postimg.org/image/pnkvab1j1/
this is what i have so far:
int main()
{
// Variables
int speed;
int time;
int distance;
// Obtain the speed
cout << "Please input the speed of the vehicle " ;
cin >> speed;
while(speed < 0) // while statement
{
cout << "Please refrain from using a negative number ";
cin >> speed;
}
cout << "Please enter the time, represented in hours, travelled" <<endl;
cin >> time;
// Obtain the time
while(speed < 1)
{
cout<< "Please use a number greater than 1 " <<endl;
cin >> time;
}
// Calculation
distance = speed * time;
cout << endl;
cout << "Hour(s) " << "\t" << "Distance Travelled" << endl;
cout << "____________________________________________" << endl;
// "for" Loop statement
for(int count =1; count <= time; count++)
{
cout << " " << "\t\t" << speed*time << endl;
}
system ("PAUSE");
return 0;
}
When you print in your for loop, speed = 20 and time = 5 always, so it always prints 100 (in the example you give).
You want to be printing speed*count (in this case).
Again you print "" for the hours, which is an empty string, you want to be printing count.
cout << count << "\t\t" << speed*count << endl;
Here is the correct program. Your program was very good. But you have a very small mistake.
#include<iostream.h>
main()
{
// Variables
int speed;
int time;
int distance;
// Obtain the speed
cout << "Please input the speed of the vehicle " ;
cin >> speed;
while(speed < 0) // while statement
{
cout << "Please refrain from using a negative number ";
cin >> speed;
}
cout << "Please enter the time, represented in hours, travelled ";
cin >> time;
// Obtain the time
while(speed < 1)
{
cout<< "Please use a number greater than 1 ";
cin >> time;
}
// Calculation
cout << endl;
cout << "Hour(s) " << "\t" << "Distance Travelled" << endl;
cout << "____________________________________________" << endl;
// "for" Loop statement
for(int count =1; count <= time; count++)
{
cout << count << "\t\t" << speed * count << endl;
}
system ("PAUSE");
return 0;
}
Homework problem I am helping one of my mentees out with (check my history, I have previously asked help with Java in more advanced programs. This is something simple I can't help her figure out). We need to use a while loop to read in numbers, keep track of the count, and keep summing up the numbers entered. We keep getting an error in line 24. Even when I comment it out and run it, the programs doesn't do what it is supposed to do. Been forever since I've done a program in C++ and I need the help of you guys!
#include <iostream>
using namespace std;
int main()
{
int num;
int sum = 0;
int count = 0;
float avg;
cout << "Enter numbers, 999 to quit" << endl;
cin >> num; //
while (num != 999)
{
cout << "Number entered is" << num << endl;
cout << "Enter numbers, 999 to quit" << endl;
cin >> num;
sum = sum + num;
count++;
}
cout << "Total numbers entered: " + count << endl;
cout << "Sum of numbers entered is " + sum << endl;
avg = sum/count;
cout << "Average of numbers entered:" + avg << endl;
return 0;
}
cout << "Total numbers entered: " + count << endl;
cout << "Sum of numbers entered is " + sum << endl;
avg = sum/count;
cout << "Average of numbers entered:" + avg << endl;
Change those +'s to <<'s.
cout << "Total numbers entered: " << count << endl;
cout << "Sum of numbers entered is " << sum << endl;
avg = sum/count;
cout << "Average of numbers entered:" << avg << endl;
#include<iostream>
using namespace std;
int main()
{
int num,count;
float sum,average;
cout << "Enter numbers, 999 to quit" << endl;
cin>>num;
count=0;
sum=0;
while (num!=999)
{
cout<<"Number entered is"<<num<<endl;
++count;
sum+=num;
cout << "Enter numbers, 999 to quit" << endl;
cin>>num;
}
if (count==0) {
count=1;
}// if the first number you enter is 999 count should be 1
// , otherwise avg will be (sum/0 ),which doesn't make sense.
cout << "Total numbers entered: " <<count << endl;
cout << "Sum of numbers entered is " <<sum << endl;
average = sum/count;
cout << "Average of numbers entered:"<<average << endl;
// use << not + because "Total..." is string type and count is int type
system("pause");
return 0;
}
You should pay attention to the type of variable when you do something,which often can cause small errors.
Not a great title I know but I'm not sure how to word it. Anyway, in brief I am trying to calculate the number of scores, the total of scores and the average and grade of the scores. I am using for... loops to complete this task. So...
my function prototypes:
int validateNumber(int);
void summary(int,int,float,char);
char getGrade(float);
float getAvg(int,int);
probably only the validateNumber(int) is relevant here but just in case.
The main()
int num, total, scores;
cout << over4 << "How many scores do you want to average? " << endl;
cout << over4 << "Enter a value from 1 to 4: ";
cin >> num;
And the calls(?):
total = validateNumber(num);
summary(num,total,average,grade);
And then the definitions:
int validateNumber(int num)
{
int total = 0, score;
while (num < 1 || num > 4)
{
cout << over3 << num << " is not between 1 and 4! Try again: ";
cin >> num;
}
system("CLS");
for (int i = 1; i <= num; i++)
{
cout << over3 << "Enter score " << i << ": " << endl;
cout << over3 << "Enter a value from 0 to 100: ";
cin >> score;
while (score < 0 || score > 100)
{
cout << over3 << score << " is not between 0 and 100! Renter the score: "
<< i << ": ";
cin >> score;
}
total += score;
}
return total;
}
and:
void summary(int num,int total,float average,char grade)
{
cout << over4 << "Number of scores : " << num << endl;
cout << over4 << "Scores total : " << total << endl;
cout << over4 << "Average : " << average << "%" << endl;
cout << over4 << "Grade : " << grade << endl;
cout << down11;
}
When the user enters a num value between 1 and 4, there is no problem, the program works as it should. However when the user enters a value for num not in that range, the function works as it should BUT the summary will tell me that the number of scores was that first erroneous value and as a result mess up my average/grade.
in your function you are passing by value not by reference, so the change which is done in your function int validateNumber(int); is only in that function's scope, outside num's value is same as it was first. you should send value by reference. in this way :
int validateNumber(int &);
What happens is that you pass num to the validateNumber function by value. The local num in the validateNumber function copies the global num's value and get's processed accordingly. However, the global num variable stays as it were. In order to change the num itself you will have to pass it by reference. Change the parameters on your function definition to: int validateNumber(int &num) { ... }