Thread: Adding extra lines of code doesn't work

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    3

    Question Adding extra lines of code doesn't work

    I have started learning C++ recently and am currently working my way through various tutorials and books.

    Most recently the object of interest was a very basic calculating program, running from the win prompt.
    I understand the code and the program runs perfectly fine. However, I had the idea to try and add a little gimmick to the program. As you might already guess, that didn't work the way I expected. Actually, it didn't work at all.
    The basic idea of the original program is, asking the user to input two numbers and then asking again to choose between adding, subtracting, multiplying and dividing the numbers, then displaying the result.
    With the intent to increase my understanding of C++ and explore the code system a bit on my own I was wondering if it was possible to get the program to react to the result it puts out in the end.
    My idea was to add a literature reference at the end if the output result equals 42, i.e. Don't Panic! or The answer to the great question or something like that. Would that be possible? And if so, what would I need to do?

    The original code for the program is as follows:

    #include <iostream>
    int main()
    {
    using namespace std;
    float num1;
    float num2;
    char op;
    float ans;
    cout << "Please enter a number: ";
    cin >> num1;
    cout << "Please enter another number: ";
    cin >> num2;
    cout << "Press A to add the two numbers." << endl;
    cout << "Press S to subtract the two numbers." << endl;
    cout << "Press M to multiply the two numbers." << endl;
    cout << "Press D to divide the two numbers." << endl;
    cin >> op;
    if (op == 65)
    ans = num1 + num2;
    if (op == 83)
    ans = num1 - num2;
    if (op == 77)
    ans = num1 * num2;
    if (op == 68)
    ans = num1 / num2;
    cout << "The answer is " << ans << endl;
    cin.clear();
    cin.ignore(255, '\n');
    cin.get();
    return 0;
    }


    Like I said, this runs just like it should.
    I tried to add my "Easter egg" by inserting an integer 'x' and declaring it being '42'. I then followed to insert the code that if 'ans' equals 'x' 'cout' should display my extra statement 'Don't Panic!'.
    Like already mentioned, this didn't work. The compiler seems to have processed everything accordingly, no errors were reported, but the extra simply got left out.
    Can somebody help me understand why that happens and help me fix it?
    I'm hoping that I will be able to understand the code and programming in C++ a little better when venturing off the path given by the books and trying some new/other things that are not set in the curriculum.

    Appreciate your help and would like to thank you in advance.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Was the answer, in fact, 42?

    Otherwise, be more specific about what you did and where.

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    3
    More details? Sure.
    The answer to the calculation I did was 42, yes. I asked the program to multiply 6 with 7 and it put out the answer, 42.

    I think nothing will be more specific than the actual code I wrote, so here goes:

    #include <iostream>
    int main()
    {
    using namespace std;
    int x;
    (x == 42);
    float num1;
    float num2;
    char op;
    float ans;
    cout << "Please enter a number: ";
    cin >> num1;
    cout << "Please enter another number: ";
    cin >> num2;
    cout << "Press A to add the two numbers." << endl;
    cout << "Press S to subtract the two numbers." << endl;
    cout << "Press M to multiply the two numbers." << endl;
    cout << "Press D to divide the two numbers." << endl;
    cin >> op;
    if (op == 65)
    ans = num1 + num2;
    if (op == 83)
    ans = num1 - num2;
    if (op == 77)
    ans = num1 * num2;
    if (op == 68)
    ans = num1 / num2;
    cout << "The answer is " << ans << endl;
    if (ans == x)
    {
    cout << "Don't Panic!" << endl;
    }
    cin.clear();
    cin.ignore(255, '\n');
    cin.get();
    return 0;
    }

    I have highlighted the changes I made to the original code in red.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Try highlighting the rest of the code with code tags, like you're supposed to have done.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    In your code, x does not have the value of 42. You compare it to 42, but you don't ever put the value 42 in the variable.

  6. #6
    Registered User
    Join Date
    Mar 2010
    Posts
    3
    Oh, you mean I didn't assign a value to 'x' but compared it with 42?
    I didn't realise...
    That is seriously embarassing that I mixed those up. I double checked and fixed it. It works now.
    Thanks very much for your help.

  7. #7
    Registered User
    Join Date
    Dec 2009
    Posts
    16
    By the way, you don't have to declare a new variable. You can just do if (ans==42).

    If you want to get more complicated, you could use two arrays to store a bunch of easter eggs and call them with a lot less code.

    Example with two easter eggs, "Nothing!" for ans==0 and "Don't Panic!" for ans==42:

    put this code with your variable declarations:
    Code:
    const int NUM_OF_EASTER_EGGS = 2; //declares a constant variable with the number of easter eggs in the program
    int easterEggs[NUM_OF_EASTER_EGGS] = {0, 42}; //one element for each number that has an easter egg
    string easterEggMessage[NUM_OF_EASTER_EGGS] = {"Nothing!", "Don't Panic!"}; //the message the corresponds with the easter egg
    Then put this in place of "if (ans == x)" and everything in it (the cout)
    Code:
    for(int n = 0;n < NUM_OF_EASTER_EGGS;n++) //creates a loop that will run once for each easter egg
    {
        if (ans == easterEggs[n]) //checks if the answer is equal to whichever easter egg is being checked
        {
            cout << easterEggMessage[n]; //displays the appropriate message if it's true
        }
    }
    All you have to do is change the variables at the start of the file to add or remove easter eggs.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. switch memory locs with 2 lines of code
    By saltzmanjoelh in forum C++ Programming
    Replies: 12
    Last Post: 09-20-2008, 07:55 PM
  2. Checking lines of code
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 04-10-2008, 02:05 AM
  3. How to ignore lines of code.
    By watsonsword in forum C++ Programming
    Replies: 6
    Last Post: 10-23-2005, 03:06 PM
  4. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 04:17 PM
  5. count only lines of code...
    By flightsimdude in forum C Programming
    Replies: 13
    Last Post: 09-23-2003, 07:08 PM