So something's not working correctly..

Started by
8 comments, last by Gsmtih103 9 years, 3 months ago
So I am learning the basics of c++ and I have a book that had an exercise problem that I tried to do and I thought my code was fine but when I went to run my program this popped up "c++ book.exe has stopped working" and my program doesn't run. I'm not getting any compiler errors either so can you guys help me out? This is what my code looks like :

#include <iostream>
using namespace std;
 
unsigned short int simpleDivision(unsigned short int, unsigned short int);
 
 
int main(){
 
unsigned short int x = 2;
unsigned short int y = 2;
 
simpleDivision(x, y);
 
 
 
 
return 0;
 
}
 
 
 
unsigned short int simpleDivision(unsigned short int x, unsigned short int y){
 
if (unsigned short int y = 0){
 
return -1;
}
 
else{
unsigned short int divide = x / y;
 
return divide;
}
 
 
 
}
 
 
Thanks in advance biggrin.png.
Advertisement

This line:

if (unsigned short int y = 0){

Doesn't do what you think it does. It creates a new, temporary variable called 'y', assigns it the value 0, then tests its value. You probably wanted:

if(y == 0){

(Note the double ==, that's how you test for equality in C++)

But I'm not sure why you are crashing. Your code as is will always return -1 from simpleDivide.

But I'm not sure why you are crashing. Your code as is will always return -1 from simpleDivide.


The assignment "unsigned short int y = 0" evaluates to 0, which does not evaluate to true, so the else branch is taken, at which point the "y" variable is still shadowed by the one declared in the if statement, causing a division by zero.

“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”

But I'm not sure why you are crashing. Your code as is will always return -1 from simpleDivide.


The assignment "unsigned short int y = 0" evaluates to 0, which does not evaluate to true, so the else branch is taken, at which point the "y" variable is still shadowed by the one declared in the if statement, causing a division by zero.

Oh yeah, so it is :)

To clarify: the condition of the "if" statement is declaring a new variable named 'y' that hides the parameter named 'y' in the simpleDivision() function, and initializes it to zero which is an expression equivalent to 'false' so control falls through to the "else" clause. The scope of a variable declared in a control statement is the end of the scope of the control statement, in this case that is the closing brace of the "else" clause associated with the "if" statement. You then divide by zero, and the universe proceeds to implode in an orderly manner.

So the most important lessons learned here is to use '==' and not '=' to compare two values for equality in an expression: if you had done that, the code would not have compiled. Don't worry though, I still sometimes accidentally use '=' instead of '==' after more than 30 years of C programming. Algol-based languages use ':=' for the assignment operator to avoid the problem, Fortran uses '.eq.' for the equality comparator to avoid the problem. Sucks that the language war winner was pretty much the most undeserving candidate.

Stephen M. Webb
Professional Free Software Developer

I was the cause of the release of live software to all our clients that screwed up big time because I put = instead of == in a condition. Took us days to fix the mess it caused with data being synched all over the place.

Explaining the problem to my VB coder boss was also interesting. Much scorn heaped on C++ that day :).

That after 20 years of C and C++. Good to find a compiler setup that will warn you about the cases of this it can. Or code in a way that makes it an error (see Yoda coding).

Shadowing of variables is hard to auto catch since it is often by design but something else to watch, even when it isn't a syntactic misunderstanding like here seems to be.

Thank you guys so much :). I actually did put in == first but it was showing an error. So the first problem was the = sign and the second was I didn't have to add unsigned short int to y in my if statement. So when I am defining a function and pass in two parameters I don't have to declare their type when I use them inside of the function?

Aardvajk that sounds scary lol And im using Visual Studio so it usually will tell me when I make mistakes tongue.png

Thank you guys so much smile.png. I actually did put in == first but it was showing an error. So the first problem was the = sign and the second was I didn't have to add unsigned short int to y in my if statement. So when I am defining a function and pass in two parameters I don't have to declare their type when I use them inside of the function?

You only declare the variable type when you create the variable. When you use the variable, you only use the variable's name.


int myVar = 1; //One variable. It's in the global scope.

int main()
{
    int myVar = 2; //A _different_ variable. It just happens to have the same name.
    
    myFunction(myVar); //Passes 'myVar' into 'myFunction'. In this case, it makes a COPY.
    
    //...myVar is still '2'. It was copied into 'myFunction()', so the original wasn't modified (which is good).
}

void myFunction(int myVar) //A third _different_ variable.
{
    //Only affects the variable inside myFunction(), because each of these three variables are different.
    //They just happen to use the same name.
    myVar = 3;
}

When you want to intentionally modify a variable inside a function, you use a reference or a pointer. By default, C++ passes variables "by value" (by copying).

For a function like 'divide' you actually don't want to modify the originals, so passing by value (copying) is desired.


When you want to intentionally modify a variable inside a function, you use a reference or a pointer. By default, C++ passes variables "by value" (by copying).
For a function like 'divide' you actually don't want to modify the originals, so passing by value (copying) is desired.

Ah ok thanks. I do remember learning a little about passing by reference with the & sign.

This topic is closed to new replies.

Advertisement