Welcome
Welcome to the forums of AntiRTFM's Absolute N00b Spoonfeed C++ Tutorials!

You are currently viewing our boards as a guest, which gives you limited access to view most discussions and access our other features. By joining our free community, you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content, and access many other special features. Registration is fast, simple, and absolutely free, so please, <a href="/profile.php?mode=register">join our community today</a>!

Debug Project 1 (Easy)

Think you can spy out that little nasty bug? Try debugging some code in here

Debug Project 1 (Easy)

Postby Marss++ on Mon Jul 21, 2008 10:45 pm

Here is a simple addition program.

The flow of the program is this:

Computer prints out "Hello and Welcome to Mars++'s addition calculator."

The computer asks for first and second number.
If the second Number is 0. Then the computer is to exit the program.
If the second Number is anything else besides 0. Then the program continues on the loop and asks you to input another first number.

Code: Select all

#include "stdafx.h"
#include <iostream>
using namespace std;

int main()
{
   double firstNumber, secondNumber;

   cout << "Welcome to Mars++'s Basic addition Calculator!" << endl;
   for(;;)
   {
      cout << "Enter First Number: ";
      cin >> firstNumber;
      cout << "Enter second Number(0 to Exit): ";
      cin >> secondNumber;
      if (secondNumber = 0)
      {
         cout << "Exiting" << endl;
         break;
      }
      cout << "Those two numbers added up is: " << firstNumber + secondNumber << endl;
      cout << endl;
   }
   return 0;
}




When you figure out what is wrong with the code. Create a new code that works, and tell what you changed to make it work. The program's objective should not change.
I'm a 15 year old kid who has his heart set on Programming.

Came here hoping to help so if you have any questions just ask!
Marss++
 
Posts: 149
Joined: Fri Jul 18, 2008 3:20 pm

Postby asib on Wed Jul 23, 2008 12:26 am

Got it, you need to change :

Code: Select all
if (secondNumber = 0)


to :

Code: Select all
if ( secondNumber == 0 )
{
etc...

This stops the program from reassigning the value, therefore making it unstoppably 0.
Last edited by asib on Wed Jul 23, 2008 12:41 am, edited 1 time in total.
asib
 
Posts: 86
Joined: Tue Jul 22, 2008 9:15 pm

Postby oopalonga on Wed Jul 23, 2008 12:32 am

Hi Marss++,

Here's is the upgraded code to make the program fully functional.

Code: Select all
#include "stdafx.h"
#include <iostream>
using namespace std;

int main()
{
   double firstNumber, secondNumber;

   cout << "Welcome to Mars++'s Basic addition Calculator!" << endl;
   
      
   for(;;)
   {
    
      cout << "Enter First Number: ";
     cin >> firstNumber;
    
    
      cout << "Enter second Number(0 to Exit): ";
     cin >> secondNumber;
     int Answer;
     Answer = firstNumber + secondNumber;
    
      if (secondNumber == 0)
      {
         cout << "Exiting" << endl;
       break;
      
      }
      cout << "Those two numbers added up are: " << Answer << endl;
      cout << endl;
   }
   return 0;
}


I changed
Code: Select all
cout << "Those two numbers added up is: " << firstNumber + secondNumber


to this:
Code: Select all
cout << "Those two numbers added up are: " << Answer << endl;


with "Answer" = firstNumber + secondNumber

Then I added an = sign to this:
Code: Select all

      if (secondNumber == 0)
oopalonga
 
Posts: 17
Joined: Sun Jul 20, 2008 7:53 pm

Postby oopalonga on Wed Jul 23, 2008 11:26 am

Hey Marss++,

I also meant to ask you: why didn't my compiler pick up on the missing '=' sign?
oopalonga
 
Posts: 17
Joined: Sun Jul 20, 2008 7:53 pm

Postby asib on Wed Jul 23, 2008 12:26 pm

That would still be valid, because it would still return a boolean value, as I'm pretty sure positive integers are true and vice versa, so if you assign it a value, it still ends up procuring a boolean value for the if statement to use.
asib
 
Posts: 86
Joined: Tue Jul 22, 2008 9:15 pm

Postby Marss++ on Wed Jul 23, 2008 12:38 pm

GREAT QUESTION


As you saw in antiRTFM's videos you can have the
'=' equals operator
'==' logical comparison operator


Both do seperate things.

Because of the flexibility of an if statement
the compiler does not know whether you are trying to make something equal another thing or if you are trying to evaluate something.

So wichever one you choose it's not going to tell you that you are wrong.

You may get a warning depending on your compiler, but that's not always the case.

So great question, the answer is that the compiler doesn't know whether or not your trying to evaluate or assign.



GREAT QUESTION!!

And good job fixing it.
I'm a 15 year old kid who has his heart set on Programming.

Came here hoping to help so if you have any questions just ask!
Marss++
 
Posts: 149
Joined: Fri Jul 18, 2008 3:20 pm

Postby antiRTFM on Wed Jul 23, 2008 9:33 pm

may i humbly add to Marss++ what asib said;

An 'if' statement needs a boolean value in the parentheses. Whatever the heck you put in those parentheses will be looked at as something boolean, which means that its either 'true' or 'false'.

In c++, the number 0 (zero) will always be considered false. Anywhere you type 'false' in c++ you might as well type 0.
Code: Select all
bool dontLie = 0;
// assigning an int (zero number) to a boolean??? yup! 'false' is 0


Also, any number besides absolute 0 (like 1, 2, 34523, -1, -234, 2.8763, -0.00000000001 etc) evaluates 'true'.

Looking at the expression "secondNumber = 0" where we assign 0 to the variable secondNumber, after the assignment is done (as with any expression) the value of secondNumber is expressed for anyone to catch. The if statement will catch the secondNumber expression and look at it as its required boolean value for the if condition. secondNumber now has 0, so the condition evaluates as false.
User avatar
antiRTFM
Administrator
 
Posts: 470
Joined: Sun Apr 13, 2008 9:10 am

Postby oopalonga on Thu Jul 24, 2008 12:07 pm

Ahh good points, thanks for the feedback.
oopalonga
 
Posts: 17
Joined: Sun Jul 20, 2008 7:53 pm

Postby Marss++ on Thu Jul 24, 2008 12:34 pm

That is exactly correct.

Great job figuring these things out guys. You did a great Job!

Keep up the good work.
I'm a 15 year old kid who has his heart set on Programming.

Came here hoping to help so if you have any questions just ask!
Marss++
 
Posts: 149
Joined: Fri Jul 18, 2008 3:20 pm

Re: Debug Project 1 (Easy)

Postby Argio on Mon Dec 01, 2008 3:09 am

Muahahaha my first debug thing! 'twas very easy as it says!

So here is the code:

Code: Select all
    #include "stdafx.h"
    #include <iostream>
    using namespace std;

    int main()
    {
       double firstNumber, secondNumber;

       cout << "Welcome to Mars++'s Basic addition Calculator!" << endl;
       for(;;)
       {
          cout << "Enter First Number: ";
          cin >> firstNumber;
          cout << "Enter second Number(0 to Exit): ";
          cin >> secondNumber;
          if (secondNumber = 0)
          {
             cout << "Exiting" << endl;
             break;
          }
          cout << "Those two numbers added up is: " << firstNumber + secondNumber << endl;
          cout << endl;
       }
       return 0;
    }



If you your selves could not find the error at first sight let me show you where it is.Well, 'tis on line 16:
Code: Select all
          if (secondNumber = 0)


Oh noes! What is the solution Argio???
Code: Select all
          if (secondNumber == 0)


Argio how the hell does that work?

Simple = is a deceleration operator. Essentially if you want a value to change you tell the computer to change it by using this operator...
== Is a logic operator and in this case you tell the program to check for a value. If is a logic statement which would you rather have:

If the pie equals(==) the cake then
eat the pie

or

if WAIT A SEC COMPUTER! Make PIE equal(=) cake then eat it.

Does choice number 2 make any sense? This is why they made logic(comparison) operators.
I love C++, for reasons I have yet to discover.
All the haters love me so lovers stop hating.
Image
User avatar
Argio
 
Posts: 21
Joined: Fri Jun 20, 2008 5:04 am
Location: Utah. I am jewish.

Re: Debug Project 1 (Easy)

Postby antiRTFM on Wed Dec 03, 2008 11:30 am

lol Argio :D
User avatar
antiRTFM
Administrator
 
Posts: 470
Joined: Sun Apr 13, 2008 9:10 am

Re: Debug Project 1 (Easy)

Postby Xghost27X on Sat Apr 04, 2009 6:34 pm

^^^ lol
User avatar
Xghost27X
 
Posts: 7
Joined: Sat Mar 28, 2009 9:16 pm

Re: Debug Project 1 (Easy)

Postby Symbol on Sun Jun 28, 2009 7:48 am

You just need to change the '=' to "==".


Old
Code: Select all
      if (secondNumber = 0)




New
Code: Select all
      if (secondNumber == 0)









Full Code
Code: Select all
#include "stdafx.h"
#include <iostream>
using namespace std;

int main()
{
   double firstNumber, secondNumber;

   cout << "Welcome to Mars++'s Basic addition Calculator!" << endl;
   for(;;)
   {
      cout << "Enter First Number: ";
      cin >> firstNumber;
      cout << "Enter second Number(0 to Exit): ";
      cin >> secondNumber;
      if (secondNumber == 0)
      {
         cout << "Exiting" << endl;
         break;
      }
      cout << "Those two numbers added up is: " << firstNumber + secondNumber << endl;
      cout << endl;
   }
   return 0;
}
Symbol
 
Posts: 8
Joined: Sun Jun 28, 2009 2:30 am


Return to Debug

Who is online

Users browsing this forum: No registered users and 0 guests