Author Topic: Beep goes C++  (Read 5165 times)

reactosguy

  • Member
  • **
  • Posts: 269
  • Kudos: 2
    • Microsoft Sucks !!!
Beep goes C++
« on: 11 September 2010, 02:51 »
So, I'm trying to design a C++ application that tries to find the answer to a certain addition equation. How it does that is that it messes up the answer and while the answer is not correct, the incorrect answer goes down by one until the answer is found.

Here is my entire source code:

Code: [Select]
#include <iostream>
using namespace std;

int main()
{
    int thisisone;
    int thisistwo;
    int thisisthree = thisisone / thisistwo;
    int thisisfour = thisisthree + thisisone + thisistwo;
    int thisisfive = thisisfour * thisisfour;
    int thisissix = thisisfive + thisisone + thisistwo;
    int thisisonetwo = thisisone + thisistwo;

    cout << "Please enter a number to add: ";
    cin >> thisisone;
    cin.ignore();
    cout << "Please enter a second number to add: ";
    cin >> thisistwo;
    cin.ignore();

    while(thisissix != thisisonetwo)
    {
        thisissix--;
        cout << "Solution thrown away: " + thisissix;
    }
    cout << "Found solution: " + thisissix;
    cin.get();
}

I compiled it to .exe with Code::Blocks because MinGW compiler never compiled the result. I tried it and my computer beeps, the command line displays binary junk and then it ends. I never got the outcome because I wanted the beeping to stop. Try out this code and please make some corrections you can post, as the beeping traumatized me.

If you have source code for such a program I described, that would be great as well. Thanks!  S)

EDIT #1: Make sure the C++ code has been tested. If I get one error, I'll try to fix it, if there are too many errors or my computer goes beep again, I'll reject your submission.
« Last Edit: 11 September 2010, 02:54 by reactosguy »

Laukev7

  • VIP
  • Member
  • ***
  • Posts: 2,834
  • Kudos: 495
Re: Beep goes C++
« Reply #1 on: 11 September 2010, 13:09 »
You get an error because you performed calculations on your variables before having initialised them.

For example, in the line number 8, you add up thisisone and thisistwo before having obtained their values from cin. When you declare the variables in lines 6 and 7, they are random integers until you assign a value to them. So adding them, dividing them etc will result in more random numbers. At worst, you might end up with random junk instead of random integers.

Furthermore, in line 21, you use != instead of >. If thisissix starts as a number smaller than thisisonetwo, then your program will never break out of the while loop.