Problem with program output

This is a discussion on Problem with program output within the C++ Programming forums, part of the General Programming Boards category; Hi guys. I am having a problem with a project for my Computer Science class. The basic program works but ...

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    2

    Problem with program output

    Hi guys. I am having a problem with a project for my Computer Science class. The basic program works but I am having trouble getting the output the professor asked for. Here is my source code:

    Code:
    #include<iostream>
    #include<iomanip>
    using namespace std;
    
    #define clear() cout << (char)(27) << '[' << '?' << '3' << 'l';
    #define cur_yx(y,x) cout << (char)(27) << '[' << y << ';' << x << 'H';
    main()
    {
      int start, end, sum, square, sum_cubes, sum_terms, cubes_terms;
      cout<<"Input Series Start Here: ";
      cin>>start;  
    cout<<"Input Series End Here: ";
      cin>>end;
        sum_terms = start, start++;
    
        cubes_terms = start*start*start;
        cubes_terms++;
      sum = (((0.5)*(end*end)) + (0.5*end));
      square = sum * sum;
      sum_cubes = (end*(end + 1) / 2) * (end*(end + 1) / 2);
      cout<<"Sum = "<<start<<" + "<<sum_terms++<<" = "<<sum<<endl;
      cout<<"Square of Sum = "<<square<<endl<<endl;
      cout<<"Sum of Series Terms Cubes = "<<cubes_terms<<" = "<<sum_cubes<<endl<<en\
    dl;
    
    }
    As I said, the main program works. It calculates the sum, square and sum of the cubes. However, the professor has asked for a specific output. He wants the output to say the following:

    For example, if the two integers were 1 and 3, the output should
    be:

    Sum = 1+ 2 + 3 = 6
    Square of Sum = 36

    Square of Cubes = 1 + 8 + 27 = 36

    What I am having trouble with is how to get the program to output 1+2+3 or 1+8+27 along with the answer, which in this is 36. Any help would be appreciated. Thanks.

  2. #2
    Registered User
    Join Date
    Mar 2005
    Posts
    140
    Your program does not work correctly if the start number is not 1.

    You are almost certainly going to need a loop to print the output in that format.

    Code:
        cout << "Sum: ";
        for(i = start; i <= end; i++) {
            cout << i;
            if(i < end) cout << " + ";
        }
        cout << " = " << sum << endl;

  3. #3
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    Quote Originally Posted by progrookie
    Code:
    #define clear() cout << (char)(27) << '[' << '?' << '3' << 'l';
    #define cur_yx(y,x) cout << (char)(27) << '[' << y << ';' << x << 'H';

    ARRGGHH, my eyes, my eyes! Macros are EVIL!! somewhere between telemarketers and hitler on a scale of 1 to evil.


    as spydoor said you'll need a loop to output your values, and probably a loop to calculate them too. what happens if someone inputs 1 and 9999?
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  4. #4
    Registered User
    Join Date
    Mar 2006
    Posts
    2
    Thanks spydoor and chaosengine. I got the desired output. Chaos, the macros were not my idea. The professor instructed us to place that in every program.

    Thanks again guys.

  5. #5
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    Quote Originally Posted by progrookie
    Chaos, the macros were not my idea. The professor instructed us to place that in every program.
    I'd look into having him fired.
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  6. #6
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,157
    There's worse things to fire a guy for than macros, Chaos.
    Code:
    main()
    and no
    Code:
    return x;
    for example....
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  7. #7
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    Quote Originally Posted by WaltP
    There's worse things to fire a guy for than macros, Chaos.
    Code:
    main()
    and no
    Code:
    return x;
    for example....
    that's pretty bad alright, but those could be the op's fault.
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  8. #8
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,069
    Quote Originally Posted by ChaosEngine
    that's pretty bad alright, but those could be the op's fault.
    Plus I heard he seduces his students. Just what I heard... s'all I'm sayin'.
    Sent from my iPadŽ

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  2. Problem with .CPP program output HELP please!!!
    By shoeb_hi in forum C++ Programming
    Replies: 4
    Last Post: 03-24-2006, 10:01 AM
  3. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 11:39 AM
  4. Program abort due to memory problem
    By cazil in forum C++ Programming
    Replies: 5
    Last Post: 01-21-2002, 11:55 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21