This forum is in archive mode. You will not be able to post new content.

Author Topic: [C++]loop "for" help!(solved)  (Read 2067 times)

0 Members and 1 Guest are viewing this topic.

Offline gh0st

  • Sir
  • ***
  • Posts: 575
  • Cookies: 8
  • #DEDSec
    • View Profile
[C++]loop "for" help!(solved)
« on: April 13, 2011, 04:56:59 AM »
well guys Im trying to make a problem from my book but this time has happened something really weird just compile it ah and the problem is this btw:

1. Write a program that requests the user to enter two integers. The program should then
calculate and report the sum of all the integers between and including the two integers.
At this point, assume that the smaller integer is entered first. For example, if the user
enters 2 and 9, the program should report that the sum of all the integers from 2 through
9 is 44.


so I did:
Code: [Select]
#include <iostream>
using namespace std;
int main()
{
    int x;
    int z;
    int w;
    int y;
    cout << "Enter the first integrer: ";
    cin >> x;
    cout << endl;
    cout << "Enter the second integrer: ";
    cin >> z;
    cout << endl;
    for (y = x; x > z; y++)
    cin >> y;
    for (w = z; z > x; w++)
    cin >> w;
   
    if (x > z)
    {
          cout << y;
    }
    if (z > x)
    {
          cout << w;
    }
    system("pause");
    return 0;
   
}

well this time I did it fast but I think that it has something wrong but I hope that the answer is close to this source or I will cry  :'(
can somebody help me plz?
award: +1 karma  ;)
« Last Edit: April 13, 2011, 05:50:32 AM by gh0st »

Offline Satan911

  • VIP
  • Knight
  • *
  • Posts: 289
  • Cookies: 25
  • Retired god/admin
    • View Profile
Re: loop "for" help!
« Reply #1 on: April 13, 2011, 05:21:07 AM »
No offense but I have no idea what you were trying to do there. Here's my solution:

Code: [Select]
#include <iostream>

using namespace std;

int main()
{
    int x, y ,sum=0;

    cout << "Enter the first integrer: ";
    cin >> x;
    cout << endl << "Enter the second integrer: ";
    cin >> y;

for(int i=x; i<=y; i++)
sum+=i;

cout << endl << "The sum is " << sum << endl;

return 0;
}

Try a bit more and make sure you understand the question before posting next time ;)
Satan911
Evilzone Network Administrator

Offline gh0st

  • Sir
  • ***
  • Posts: 575
  • Cookies: 8
  • #DEDSec
    • View Profile
Re: loop "for" help!
« Reply #2 on: April 13, 2011, 05:34:15 AM »
Satan911 you are the best man thank you +1 of karma

 



Want to be here? Contact Ande, Factionwars or Kulverstukas on the forum or at IRC.