3

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!

4
  • 3
    I guess it isn't expecting the extra newline you are introducing with cout <<endl;? Commented Nov 23, 2015 at 9:09
  • also you should print '.' after every number. Commented Nov 23, 2015 at 9:16
  • This works fine on visual studio 2013 Commented Nov 23, 2015 at 9:46
  • Yes indeed when I ran it there it was also ok for me it has to be a problem with the web page that it doesn't recognize the strings. Commented Nov 25, 2015 at 8:19

5 Answers 5

2

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".

Sign up to request clarification or add additional context in comments.

2 Comments

i.sstatic.net/xJoN7.png i.sstatic.net/kOgw3.png When I run this code I get the correct outcome in the console of the webpage, as well as in visual studio, but for some reason when I submit the code for testing it doesn't print the strings thus resulting in the exercise being incorrect. My assumption is that the error lies with the web page.
How about not printing numbers at all? The expectation might be that the program stops for input after cout << "Number: "; and you add cin >> numbers after that. With the current answer you get input prompt before "Number: " is printed and you get the input twice on the console: once as the actual input and once as a result of cout << "Number: " << numbers; Also the exercise description and the expected results in the checker have a conflict: description lists the original sum of 0 but that is not included in the expected output column.
1

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;
}

Comments

1

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;
}

Comments

0

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?

Comments

0

You should check whether user inputs a number or a character for making sure the adding operation

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.