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
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Help me!!!

Help me!!!

Scheduled Pinned Locked Moved C / C++ / MFC
c++helplearning
8 Posts 6 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.

    M L M A S 5 Replies 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.

      M Offline
      M Offline
      Manish K Agarwal
      wrote on last edited by
      #2

      simply write - int main() { long a,b; long c = 0; cout << "1st number : "; cin >> a; cout << "2nd number : "; cin >> b; for (int i = a; i <= b; ++i) c += i; cout << c; return 0; } your result() function is wrong which always returns zero. Manish Agarwal manish.k.agarwal @ gmail DOT com

      S 1 Reply Last reply
      0
      • M Manish K Agarwal

        simply write - int main() { long a,b; long c = 0; cout << "1st number : "; cin >> a; cout << "2nd number : "; cin >> b; for (int i = a; i <= b; ++i) c += i; cout << c; return 0; } your result() function is wrong which always returns zero. Manish Agarwal manish.k.agarwal @ gmail DOT com

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

        can you tell me why it is wrong and if i follow my way then what is the solution ? By the way, thank you for your reply. This is my first time, seem that i have a lot of work to do.

        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.

          L Offline
          L Offline
          Le Thanh Cong
          wrote on last edited by
          #4

          long result(long d , long e) { if (d >= 1 , d < e) { return (result(d + 1,0)); } else return (0); } this function is wrong because result(d + 1,0) = 0 bacause d+1 > 0 if you replace e for 0: result(d + 1,0)->result(d + 1,e), this function still wrong because result(d,e)=result(d+1,e)=.....=result(e,e)=0 ----------------- conglt

          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.

            M Offline
            M Offline
            Michael Dunn
            wrote on last edited by
            #5

            if (d >= 1 , d < e) is legal syntax, but doesn't do what you want. Use && for "and" in boolean tests. Look at your exit condition of result() - it only ever returns 0. Recursion is something you should really save for later, when you have more experience with the language.

            --Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | NEW!! PimpFish | CP SearchBar v3.0 | C++ Forum FAQ

            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.

              A Offline
              A Offline
              Amar Sutar
              wrote on last edited by
              #6

              long result(long d , long e) { if (d >= 1 , d <= e) { return d + result(d+1,e); } else return (0); } Instead of calling c = a + result(a , b) + b; only call c = result(a,b); Evaluation of recursive call is - return 2 + result(2+1,5); return 2 + 3 + result(3+1,5); return 2 + 3 + 4 + result(4+1,5); return 2 + 3 + 4 + 5 + result(6,5); After if condition became false evaluation is return 2 + 3 + 4 + 5 + 0; return 2 + 3 + 4 + 5 ; return 2 + 3 + 9; return 2 + 12 ; return 14 ; In your code long result(long d , long e) { if (d >= 1 , d < e)// After first iteration e became 0 and condition false and return 0; { return (result(d + 1,0)); } else return (0); } c = a+ result(a,b) + b = 2 + 0 + 5 Write recursive function carefully. Regards Amar:)

              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
                Surivevoli
                wrote on last edited by
                #7

                are you student from Viet Nam ??? In your program : you see again if statement: if (d >= 1 , d < e)//or (d>=1 && d

                S 1 Reply Last reply
                0
                • S Surivevoli

                  are you student from Viet Nam ??? In your program : you see again if statement: if (d >= 1 , d < e)//or (d>=1 && d

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

                  Thankz for all your reply. I know my problem now.

                  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