Thread: C++ Codelab problem

  1. #1
    Registered User
    Join Date
    Jan 2017
    Posts
    1

    C++ Codelab problem

    I'm a beginner into C++ and am using CodeLabs (Turing) for homework. I'm working on a problem that states:

    "Write a loop that reads positive integers from standard input and that terminates when it reads an integer that is not positive. After the loop terminates , it prints out the sum of all the even integers read. Declare any variables that are needed."

    Here is my code:

    insert
    Code:
    int n, sum;
    sum = 0;
    {
          cin >> n;
          while (n % 2 == 0)
          {
           sum = n + sum;
           cin >> n;
          }
          if (n % 2 != 0)
          {
           cout << sum;
          }
    }
    I've tested the code using Visual Studio and it works as described, however the results say I have a run-time error and I should be using a > somewhere. I can't figure this out.

    Any help would be greatly appreciated. Also I'm not sure if the spacing is going to be edited correctly in this post (there is spacing).

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    You couldn't post the rest of the program?

    You know, a few extra lines of #includes and the declaration of main?

    It seems to me your code assumes you only input positive even numbers to start with.

    Are you sure it works with
    1 2 3 4 5 6 7 8 9 -2
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Mar 2016
    Posts
    203
    OP: any odd number ends your program (combination of lines 5 and 10) while 0 or negative numbers don't. Have a boolean that turns on when a negative integer or 0 is entered and passes control of the program to code that reports sum of even numbers entered so far:
    Code:
    #include <iostream>
    //using namespace std is not a good idea (google it!)
    int main()
    {
        bool fQuit = false;
        int sum{};
        while(!fQuit)
        {
            std::cout << "Enter number: \n";
            int n{};
    
    
            std::cin >> n;//consider additional input validation here in case of incorrect entry
            if (n <= 0)
            {
                fQuit = true;
                break;//gets you out of the while loop
            }
            else
            {
                if(n % 2 == 0)//only add even numbers
                {
                    sum += n;
                }
            }
        }
        if(fQuit == true)//checks you've broken out of the while loop
        {
            std::cout << sum << '\n';//reports sum of even numbers
        }
    }

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    you should choose one or the other - break to break out of the loop or fQuit.

    Used together - they look ridiculous
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 02-09-2014, 06:46 PM
  2. Replies: 2
    Last Post: 01-06-2013, 07:49 AM
  3. Replies: 1
    Last Post: 12-07-2012, 10:00 AM
  4. Replies: 4
    Last Post: 10-16-2008, 07:30 PM
  5. syntax linked list problem & struct problem
    By beely in forum C Programming
    Replies: 5
    Last Post: 11-11-2002, 09:14 AM

Tags for this Thread