I am currently learning to code c++ by using the web page program, where I am doing a course. Now recently I got the following exercise:
Using a while or a do-while loop, make a program that asks the user to enter numbers and keeps adding them together until the user enters the number 0.
I wrote the following code in the hope that it would bring the exercise to conclusion:
#include <iostream>
using namespace std;
int main(void){
int sum = 0;
int number;
do
{
cout <<endl;
cin >> number;
sum += number;
cout << "The total so far is: " << sum << endl;
} while (number != 0);
cout << "The total is: " << sum << endl;
}
Yet when I run the code I get the following feedback from the website (there are two links one on the left and the other on the right):
Instructions of the exercise and
Webpage feedback on the exercise
Can you tell me what am I doing wrong, alternatively can you propose an alternative solution then the code I provided? Thank you for any feedback!
The working code is:
#include <iostream>
using namespace std;
int main(){
int sum = 0, numbers;
do{
cout << "The total so far is: " << sum << endl;
cin >> numbers;
cout<< "Number: "<< numbers;
sum += numbers;
} while (numbers != 0);
cout << "The total is: " << sum << endl;
return 0;
}
You have a mistake in the line cout>>endl;. Also, the output should match the instructions. This is why your answer was "wrong".
I think you should design the exact same output as the instructions.
#include <iostream>
using namespace std;
int main(){
int sum = 0, numbers;
do{
cin >> numbers;
sum += numbers;
cout << "The total so far is: " << sum << endl;
} while (numbers != 0);
cout << "The total is: " << sum << endl;
return 0;
}
As for me then I would write the program the following way
#include <iostream>
int main()
{
int sum = 0;
int number;
std::cout << "Enter a sequence of numbers (0-exit): ";
while ( std::cin >> number && number != 0 ) sum += number;
std::cout << "The total is: " << sum << std::endl;
}
If you need to output partial sums then you can add one more output statement
#include <iostream>
int main()
{
int sum = 0;
int number;
std::cout << "Enter a sequence of numbers (0-exit): ";
while ( std::cin >> number && number != 0 )
{
std::cout << "The total so far is: " << ( sum += number ) << std::endl;
}
std::cout << "\nThe total is: " << sum << std::endl;
}
I am guessing the website is doing a simple comparison check.
Could you remove the first cout << endl;
As that would be closer to the expected output.
As to why you are not getting the "total so far" has me stumped.
Do you see the "total so far" text when ran locally?
You should check whether user inputs a number or a character for making sure the adding operation
Related
This question already has an answer here:
Integer division always zero [duplicate]
(1 answer)
Closed 2 years ago.
it seems that I'm stuck at c++ yet again. woohoo.
#include <iostream>
#include <fstream>
using namespace std;
int main() {
int amount = 0;
int inputNumber = 0;
int temp = 0;
int n = 0;
int lowest;
int highest;
double averages;
cout << "Welcome to simple calculator. Where you can use for average, highest amd lowest value.";
cout << "Please input all the number you plan to use to calculate. ";
cout << "Finish your input by enter any letter and then press on enter. " << endl;
while(cin >> inputNumber)
{
if (inputNumber < lowest) {
int (lowest = inputNumber);
}
else if(inputNumber > highest){
int (highest = inputNumber);
}
amount = amount += inputNumber;
n++;
}
double averages = amount / n;
cout << "Your lowest value is: " << lowest << endl;
cout << "Your highest value is: " << highest << endl;
cout << "Your average value is: " << averages << endl;
cout << "Amount is " << amount << endl;
cout << n << endl;
return 0;
}
but when I put in numbers it doesn't work properly. like
3
5
d
Your lowest value is: 3
Your Highest value is: 5
Your average value is: 4
2
-2
-3
a
Your lowest value is: -3
Your Highest value is: 0
Your average value is: -2
2
Idk why it do this and I'm still new to C++, I also have to code it in Linux and use G++
Thanks in advance if you could help me out.
try average = double(amount)/double(n);
Because average is double and n and amount
There are few errors in your code:
variable averages is defined twice.
Since for calculating amount you are doing int/int. which in result will yield int.
Thus your answer for -3 2 d will come wrong.
#include<iostream>
using namespace std;
int main()
{
int amount = 0;
int inputNumber = 0;
int temp = 0;
int n = 0;
int lowest=INT_MAX;
int highest=INT_MIN;
cout << "Welcome to simple calculator. Where you can use for average, highest amd lowest value.";
cout << "Please input all the number you plan to use to calculate. ";
cout << "Finish your input by enter any letter and then press on enter. " << endl;
while(cin >> inputNumber)
{
if (inputNumber < lowest) {
int (lowest = inputNumber);
}
else if(inputNumber > highest){
int (highest = inputNumber);
}
amount += inputNumber;
n++;
}
double averages = (double)amount / n;
cout << "Your lowest value is: " << lowest << endl;
cout << "Your highest value is: " << highest << endl;
cout << "Your average value is: " << averages << endl;
cout << "Amount is " << amount << endl;
cout << n << endl;
return 0;
}
This above code is correct code
So i am still a beginner at this and still practising. Basically i need to make a program that continues to asks the user to enter any number other than 5 until the user enters the number 5.
I have that done but i could't figure out how to check if the user entered a repeating number.For example:
1
2
3
3 - The program should end
#include <iostream>
#include <conio.h>
#include <iomanip>
using namespace std;
int main() {
cout << setw(15) << setfill('*') << "*" << endl;
cout << "Number 5" << endl;
cout << setw(15) << setfill('*') << "*" << endl;
int num;
cout << "Enter a number: ";
cin >> num;
if (num == 5) {
cout << "\nWhy did you enter 5? :) " << endl;
_getch();
exit(0);
}
for (int i = 1; i < 10;i++) {
cin >> num;
if (num == 5) {
cout << "\nWhy did you enter 5? :) " << endl;
_getch();
exit(0);
}
}
cout << "Wow, you're more patient then I am, you win." << endl;
_getch();
}
The previous answer does not meet the requirement in the linked article, which the querist himself seemed to not grasp:
★★ Modify the program so that it asks the user to enter any number other than the number equal to the number of times they've been asked to enter a number. (i.e on the first iteration "Please enter any number other than 0" and on the second iteration "Please enter any number other than 1"m etc. etc. The program must behave accordingly exiting when the user enters the number they were asked not to.)
This variant complies:
#include <iostream>
using namespace std;
int main()
{
for (int i = 0; i < 10; i++)
{
cout <<"Please enter any number other than " <<i <<": ";
int num;
cin >>num;
if (num == i)
return cout <<"Hey! you weren't supposed to enter " <<i <<"!\n", 0;
}
cout <<"Wow, you're more patient then I am, you win.\n";
}
You could add all inputted numbers to a vector, and whenever you get a new number, check if it's already in the vector. Include these headers:
#include <vector>
#include <algorithm> // for std::find
Make the vector like this
std::vector<int> pastEntries;
Do the check like this:
if (std::find(pastEntries.begin(), pastEntries.end(), num) != pastEntries.end()) {
std::cout << "\nWhy did you enter " << num << "? :) " << endl;
...
And when the number was not found, add it to the vector like this (you can put this after the if block):
pastEntries.push_back(num);
Alternatively, you can use std::set:
std::set<int> pastEntries;
Insert into the set like this:
pastEntries.insert(num);
And find the number in the set like this:
if (pastEntries.find(num) != pastEntries.end()) {
Or insert the number while finding out whether it has already been inserted like this:
if (!pastEntries.insert(num).second) {
Hello everyone I'm trying to find the average of a random amount of numbers that are input into a loop. For sum reason after the loop im able to print the right total, but when i try to find the average i get a weird answer. can anyone help me with this or direct to a thread on here that could help? I wasnt able to find anything on here.
here is my code for the program that isnt working.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
ifstream inData;
string golferFile;
string golfer;
int matches;
int score;
cout << endl;
cout << "Enter the golfer's filename: ";
cin >> golferFile;
inData.open(golferFile.c_str());
getline(inData, golfer);
inData >> matches;
cout << endl;
cout << "The golfer " << golfer << " has " << matches << " matches with scores"
" of" << endl;
cout << endl;
int count;
count = 1;
int matchNumber;
matchNumber = 1;
int sum;
while(count <= matches)
{
inData >> score;
cout << "Match " << matchNumber << ": " << score << endl;
matchNumber++;
count++;
sum = sum + score;
}
}
int mean;
mean = sum / matches;
cout << "The mean score is " << mean << endl;
return 0;
}
the output i receive is this for the mean
The mean score is 1399255
I found several error in your code.
you forget to initialize your sum variable.
in while loop an extra brace found.remove it
you didn't write anything to stop your loop.that why your loop run infinite time.so initialize you loop also.
I'm a beginner at coding and I can't fix this code and I'm going crazy. It keeps telling me certain variables were not declared and i'm not sure how to fix it.
#include <iostream>
using namespace std;
int main()
{
int (a = 0), sum;{
cout << "This program should read all integer numbers ";
cout << "to sum until a negative number is entered\n";
exit(0);
}
while(a < 0) {
cout << "Enter an integer number: ";
cin >> a;
sum += a;
}
cout << "The sum is sum\n";
return 0;
}
You did not initialize sum, so it could start with any value.
You have extra layers of pointless { } around for no reason.
Your final cout statement does not actually print the varaible.
Change it to: cout << "The sum is " << sum << "\n";
#include <iostream>
using namespace std;
int main()
{
int a = 0, sum = 0;
cout << "This program should read all integer numbers ";
cout << "to sum until a negative number is entered\n";
do {
cout << "Enter an integer number: ";
cin >> a;
if (a > 0)
{
sum += a;
cout << "The sum is currently: " << sum << "; but this is not yet the final value.\n";
}
} while(a > 0) ;
cout << "The sum is " << sum << "\n";
return 0;
}
Lots of gotchas with your code.
Issues I found and corrected:
You have extra parentheses and curly brackets.
You didn't initialize the variable called sum
You initialize the variable called a to 0 but your loop will only execute while a is less than 0
I had to #include "stdafx.h" above where you included iostream
The line that says "The sum is sum" - the second sum should be outside the double quotes so that it will be treated as a variable rather than text.
This code works, please compare it to yours:
#include "stdafx.h"
#include <iostream>
using namespace std;
int main()
{
int a = 0, sum = 0;
cout << "This program should read all integer numbers ";
cout << "to sum until a negative number is entered\n";
while (a <= 0) {
cout << "Enter an integer number: ";
cin >> a;
sum += a;
}
cout << "The sum is " << sum << "\n";
return 0;
}
I've been trying to create a simple program that allows me to display a total score after the user has entered the ammount of successful hits, totalHits, by multiplying that input with the constant variable POINTS resulting in another variable; score.
I didn't think creating such a program would be any problem, but as usual, I was wrong.. When I run the program score is always random, even if I enter '1' as totalHits every time. It can differ from 444949349 to -11189181 to name a couple of examples. I have no idea what I've done wrong, so it would be great if someone could give me a clue as to what to do next :)
Here's the code:
#include <iostream>
using namespace std;
int main()
{
const int POINTS = 50;
int totalHits;
int score = totalHits * POINTS;
cout << "Please enter the ammount of successful hits: ";
cin >> totalHits;
cout << "You hit " << totalHits << " targets, and your ";
cout << "score is " << score << " ." << endl;
cin.ignore(cin.rdbuf()->in_avail() + 2);
return 0;
}
Big thanks to KerrekSB and Paddyd for providing me with the correct answer. Here's the finished code with comments:
#include <iostream>
using namespace std;
int main()
{
const int POINTS = 50;
int totalHits;
cout << "Please enter the ammount of successful hits: ";
cin >> totalHits;
cout << "You hit " << totalHits << " targets, and your ";
/*As you can see I moved the line below from the top of the code.
The problem was I had not properly learned how C++ executes the code.
The orignal code was written in a way that calculates `score` before
the user could decide it's value, resulting in a different total score than
it should have been. In the finished code, the user inputs what
value `totalHits` is, THEN score is calculated by using that value. */
int score = totalHits * POINTS;
cout << "score is " << score << " ." << endl;
cin.ignore(cin.rdbuf()->in_avail() + 2);
return 0;
}
int totalHits;
int score = totalHits * POINTS;
You are multiplying by an uninitialized variable (totalHits)! You need to apply a value to totalHits before doing this calculation.
Try using the code like this:
const int POINTS = 50;
int totalHits;
int score;
cout << "Please enter the ammount of successful hits: ";
cin >> totalHits;
cout << "You hit " << totalHits << " targets, and your ";
score = totalHits * POINTS; //totalHits has a value here
cout << "score is " << score << " ." << endl;
cin.ignore(cin.rdbuf()->in_avail() + 2);
return 0;