Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. Managed C++/CLI
  4. i have problems

i have problems

Scheduled Pinned Locked Moved Managed C++/CLI
c++learning
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    sieucauthu
    wrote on last edited by
    #1

    This is the first time I write a program after learning C++ for 1 week ago. I use Dev-C++ and I want my program to do function such as: Ask person to type two number : 1st number ; 2nd number. such as: 2 ; 5; and I want my program to get me the sum : 2 + 3 + 4 + 5 = 14 but I try many times and it always gives me : 2 + 5 = 7 This is my code : using namespace std; #include long result(long d , long e) { if (d >= 1 , d < e) { return (result(d + 1,0)); } else return (0); } int main() { long a,b; long c; cout << "1st number : "; cin >> a; cout << "2nd number : "; cin >> b; c = a + result(a , b) + b; cout << c; return 0; } Can someone tell what 's wrong with my code, and give me a hint to do it. Thankz.

    S 1 Reply Last reply
    0
    • S sieucauthu

      This is the first time I write a program after learning C++ for 1 week ago. I use Dev-C++ and I want my program to do function such as: Ask person to type two number : 1st number ; 2nd number. such as: 2 ; 5; and I want my program to get me the sum : 2 + 3 + 4 + 5 = 14 but I try many times and it always gives me : 2 + 5 = 7 This is my code : using namespace std; #include long result(long d , long e) { if (d >= 1 , d < e) { return (result(d + 1,0)); } else return (0); } int main() { long a,b; long c; cout << "1st number : "; cin >> a; cout << "2nd number : "; cin >> b; c = a + result(a , b) + b; cout << c; return 0; } Can someone tell what 's wrong with my code, and give me a hint to do it. Thankz.

      S Offline
      S Offline
      Steve Pullan
      wrote on last edited by
      #2

      The result() function is incorrect. Try this:

      long result(long d , long e)
      {
      long r = 0;
      int i;

      if (d <= e)
      {
          for (i == d, i <= e, i++)
          {
              r += i;
          }
      } 
      return (r);
      

      }

      Also change this line:

      c = a + result(a , b) + b;
      

      to

      c = result(a , b);
      

      ...Steve 1. quod erat demonstrandum 2. "Give a man a fish and you've fed him for a day. Teach him how to fish and you've fed him for life." I read that somewhere once :-)

      S 1 Reply Last reply
      0
      • S Steve Pullan

        The result() function is incorrect. Try this:

        long result(long d , long e)
        {
        long r = 0;
        int i;

        if (d <= e)
        {
            for (i == d, i <= e, i++)
            {
                r += i;
            }
        } 
        return (r);
        

        }

        Also change this line:

        c = a + result(a , b) + b;
        

        to

        c = result(a , b);
        

        ...Steve 1. quod erat demonstrandum 2. "Give a man a fish and you've fed him for a day. Teach him how to fish and you've fed him for life." I read that somewhere once :-)

        S Offline
        S Offline
        sieucauthu
        wrote on last edited by
        #3

        Thankz for your reply. This is the first time so.....I will practice more. By the way thankz for your "fish", hope next time to can show me how to fish. Yeah!!!

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups