For loop problem

My homework assignment is to write a program that asks a user to enter two integers, assume the first number will be smaller, and then to calculate and report the sum of all integers including and between both numbers. Example: first number:2 second number:9 sum of all numbers between 2 and 9 is 44.

here is my code
#include

using namespace std;

int main()
{
int f,s,sum;
cout << "Enter the first number: ";
cin >> f;
cout <<"
Enter the second number: ";
cin >> s;


for (int i = f; i <= s; ++i)
{
sum += i;
cout<<sum<<endl;
}

cout <<"The sum of all numbers between "<<f<<" and "<<s<<" is "<<sum<<endl;

system("PAUSE");
return EXIT_SUCCESS;
}
and my out put is:
first number 2 second number 9
4 7 11 16 22 29 37 46

for some reason it adds 2 at the beginning and dose the same with other numbers I have entered. Whats going on?

Comments

  • #include

    using namespace std;

    int main()
    {
    int f,s,sum;
    [color=Red] sum = 0; //give sum a value = 0 [/color]
    cout << "Enter the first number: ";
    cin >> f;
    cout <<"
    Enter the second number: ";
    cin >> s;


    for (int i = f; i <= s; ++i)
    {
    sum += i;
    cout<<sum<<endl;
    }

    cout <<"The sum of all numbers between "<<f<<" and "<<s<<" is "<<sum<<endl;

    system("PAUSE");
    return EXIT_SUCCESS;
    }
  • : #include
    :
    : using namespace std;
    :
    : int main()
    : {
    : int f,s,sum;
    : [color=Red] sum = 0; //give sum a value = 0 [/color]
    : cout << "Enter the first number: ";
    : cin >> f;
    : cout <<"
    Enter the second number: ";
    : cin >> s;
    :
    :
    : for (int i = f; i <= s; ++i)
    : {
    : sum += i;
    : cout<<sum<<endl;
    : }
    :
    : cout <<"The sum of all numbers between "<<f<<" and "<<s<<" is
    : "<<sum<<endl;
    :
    : system("PAUSE");
    : return EXIT_SUCCESS;
    : }
    :
    (slaps himself on the forehead) thank you

  • : : #include
    : :
    : : using namespace std;
    : :
    : : int main()
    : : {
    : : int f,s,sum;
    : : [color=Red] sum = 0; //give sum a value = 0 [/color]
    : : cout << "Enter the first number: ";
    : : cin >> f;
    : : cout <<"
    Enter the second number: ";
    : : cin >> s;
    : :
    : :
    : : for (int i = f; i <= s; ++i)
    : : {
    : : sum += i;
    : : cout<<sum<<endl;
    : : }
    : :
    : : cout <<"The sum of all numbers between "<<f<<" and "<<s<<" is
    : : "<<sum<<endl;
    : :
    : : system("PAUSE");
    : : return EXIT_SUCCESS;
    : : }
    : :
    : (slaps himself on the forehead) thank you
    :
    :

    On my computer Tobuway's code doesn't even compile and I don't know why. Here is my output.
    [code]
    Turbo C++ Version 1.01 Copyright (c) 1990 Borland International
    namespac.c:
    Error namespac.c 1: Unable to open include file 'iostream'
    Error namespac.c 3: Declaration syntax error
    Error namespac.c 8: Undefined symbol 'cout' in function main
    Error namespac.c 9: Undefined symbol 'cin' in function main
    Error namespac.c 10: Illegal use of pointer in function main
    Warning namespac.c 11: Code has no effect in function main
    Error namespac.c 14: Expression syntax in function main
    Error namespac.c 14: Undefined symbol 'i' in function main
    Error namespac.c 14: Lvalue required in function main
    Error namespac.c 14: Statement missing ; in function main
    Error namespac.c 22: Undefined symbol 'EXIT_SUCCESS' in function main
    *** 10 errors in Compile ***


    Available memory 345312
    [/code]

    As you can see the compiler plainly identifies itself as a C++ compiler.

    I think the first code is because the file name needs to end with ".h"

    I have to idea what the second error is.

    I'm guessing that the 4th and 5th errors are because something other than "iostream.h" needs to be included
  • You get those errors because Turbo C++ does [italic]not[/italic] follow the C++ standard, it is an obsolete compiler. It wants the #include syntax that has been obsolete for 13 years. Get a modern C++ compiler and it will work.
  • : You get those errors because Turbo C++ does [italic]not[/italic]
    : follow the C++ standard, it is an obsolete compiler. It wants the
    : #include syntax that has been obsolete for 13 years.
    : Get a modern C++ compiler and it will work.

    The compiler was a freebie, a download from Borland. I doubt a modern compiler is available for the same price. :-)

    I would think it would be possible to make the code work with the "obsolete"compiler. I got it to work in C with just a few changes, primarily changing [b]iostream[/b] to [b]stdio.h[/b] and substituting [b]printf[/b] and [b]scanf[/b] for [b]cin[/b] and [b]cout[/b]. I made a total of 11 changes. Surely fewer than that would be required to compile it as C++ ??

    If the only change I make to the code is to change [b]iostream[/b] to [b]iostream.h[/b] I get a ton of "declaration systax errors" all pointing to [b]iostream.h[/b].
  • Turbo follows the C standard well, since the version of C everyone is using was released 1990, one year before Turbo roughly. The main changes to the C++ standard were made in 1995 however.

    Get a [italic]free[/italic] modern compiler instead.

    Dev C++ is simple and beginner-friendly, based on gcc:
    http://www.bloodshed.net/dev/devcpp.html

    Borland is giving away their compiler with a whole working RAD-tool for free:
    http://cc.codegear.com/Free.aspx?id=24724

    And so is Microsoft doing with Visual Studio:
    http://www.microsoft.com/Express/
  • : Turbo follows the C standard well, since the version of C everyone
    : is using was released 1990, one year before Turbo roughly. The main
    : changes to the C++ standard were made in 1995 however.
    :
    : Get a [italic]free[/italic] modern compiler instead.
    :
    : Dev C++ is simple and beginner-friendly, based on gcc:
    : http://www.bloodshed.net/dev/devcpp.html
    :
    : Borland is giving away their compiler with a whole working RAD-tool
    : for free:
    : http://cc.codegear.com/Free.aspx?id=24724
    :
    : And so is Microsoft doing with Visual Studio:
    : http://www.microsoft.com/Express/

    Thanks for the links. Unfortunately none of these seem to work with OS-X or DOS 6.22, the two operating systems I'm working with.

    That aside, I find that if I change the included file to [b]iostream.h[/b] the compiler generates a ton of errors while compiling the included file. In particular it does not seem to like the word "class". It's curious that the compiler cannot even compile a file that was bundled with it in the download. Is it possible that it's a C compiler and not a C++ compiler, in spite of what it prints out?

  • Strictly speaking, it is not a C++ compiler since it doesn't follow the standard.

    Anyway... did you try to name the file .cpp instead of .c? If that's not the cause, then there are some library settings you need to fix. I'd help you further, but I can't bother to fetch my old 486 from the basement :-)
  • : Strictly speaking, it is not a C++ compiler since it doesn't follow
    : the standard.
    :
    : Anyway... did you try to name the file .cpp instead of .c? If that's
    : not the cause, then there are some library settings you need to fix.
    : I'd help you further, but I can't bother to fetch my old 486 from
    : the basement :-)

    Got it!

    Changing the file extension to .cpp gets me halfway there. The other half is a switch, -P (case sensitive), that tells the compiler the code is C++.

    I have a Vic-20 and an XT in my attic. :-)

    Again, thanks for all the help.



Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories