Why is my code showing this error?

E

eli m

My compiler is showing this error:

Run-Time Check Failure #3 - The variable 'math' is being used without being initialized.

Here is my code:

// CMD Application
// Created by Eli. M

#include <iostream>
using namespace std;

int main ()
{
char function;
char math = math;
char mathop;
cout << "Type in help for a list of functions";
cout << "Type in a function";
cin >> function;
if (function == math)
{
cout << "What math operation do you want to use?";
cin >> mathop;
}

}

What am i doing wrong?
 
G

Geoff

My compiler is showing this error:

Run-Time Check Failure #3 - The variable 'math' is being used without being initialized.

Here is my code:

// CMD Application
// Created by Eli. M

#include <iostream>
using namespace std;

int main ()
{
char function;
char math = math;

You forgot quotes around the string "math". If you are going to
initialize a string like this, you should specify these variables as
C++ string objects instead of C's char objects.
char mathop;
cout << "Type in help for a list of functions";
cout << "Type in a function";
cin >> function;
if (function == math)

This will fail, you can't compare strings this way.
 
Ö

Öö Tiib

My compiler is showing this error:

Run-Time Check Failure #3 - The variable 'math' is being used without being initialized.

Here is my code:

// CMD Application
// Created by Eli. M

#include <iostream>
using namespace std;
See
http://www.parashift.com/c++-faq/using-namespace-std.html

int main ()
{
char function;

You probably wanted std::string not char here
char math = math;

That is something you probably did not think through clearly what you want.
char mathop;

Again string most likely.
cout << "Type in help for a list of functions";
cout << "Type in a function";
cin >> function;
if (function == math)

Here you likely wanted "math" not math.
{
cout << "What math operation do you want to use?";
cin >> mathop;
}

}

What am i doing wrong?

You are applying woo-doo programming system to learning C++.
You type something in and see what happens. Better pick a book
or tutorial and try lessons or examples from it.
 
G

Geoff

Why does this always creep up into beginners' code? Where do they learn
this stuff?

--- news://freenews.netfront.net/ - complaints: (e-mail address removed) ---

it sows up because lazy programmers coding snippets and samples always
seem to start with it. Cargo cult.
 
J

James Kanze

Why does this always creep up into beginners' code? Where do they learn
this stuff?

The same place they learn `while ( !cin.eof() )`? There are two
or three such things which seem to continually pop up. (And
I too wonder where they are learning it.)
 
B

Bill Gill

it sows up because lazy programmers coding snippets and samples always
seem to start with it. Cargo cult.
It shows up because many C++ books show it that way. Some of them
show it as "using namespace std; and then in the same book show it
as "std::cin;". In fact "Programming Principles and Practice" by
Bjarne Stroustrup uses "using namespace" frequently. I think
that Stroustrup should know something about C++.

So my question: What specifically is wrong with "using namespace"?
Both ways work and in fact it appears that "using namespace" would
be a bit more efficient for the programmer. If you go with "std::cin"
formation you save 5 key strokes each time you use a standard
function.

Now let the flame war begin.

Bill
 
S

Stefan Ram

Bill Gill said:
So my question: What specifically is wrong with "using namespace"?

Nothing when used in small tutorial examples or ad-hoc
code that does not have to be maintained costly.

It is not to be recommended for large library-grade code
that is expected to have to be maintained or for code
that has to have as few potential errors as possible,
because the author might not be aware of all imported
names or the headers might add more name to ::std in
the future.
 
J

Jorgen Grahn

The same place they learn `while ( !cin.eof() )`? There are two
or three such things which seem to continually pop up. (And
I too wonder where they are learning it.)

A lot more than 2--3 if you count bad influences from C and Java.

My theory is that the teachers and books out there are worse than
you'd think, when you know the language already and are past the
beginner's books.

/Jorgen
 
P

Paul N

So my question:  What specifically is wrong with "using namespace"?

There have been occasions where "using namespace std" has brought in a
little-known name which clashes with a name used in the program, with
the result that the cut-down code posted to comp.lang.c++ (which uses
"using namespace std") fails to work for a completely different reason
from why the poster's actual code (which doesn't) fails to work. Fun
all round.
 
V

Victor Bazarov

Sorry guys, i just started learning c++ yesterday and i am coming from python.

<shrug> You sound like you've just started learning French coming from
Portuguese. Get a decent book. You can't learn C++ by "trial and
error" method.

V
 
B

Bill Gill

Do you understand why namespaces exist in the first place? Do you
understand why "using namespace" completely nullifies those benefits?
Well, actually I don't think I do. I'm not sure that it has ever
been explained to me. Mostly it has just been said, "this is how
namespaces work". Admittedly I am not very advanced in C++, maybe
everybody is taking it slow to introduce the basic concepts until
the student has learned the mechanics of the language.

Bill
 
B

Bill Gill

Let me explain it with an actual example that has come up in actual
projects.

Especially in the past many C++ projects had "using namespace std;"
and "using namespace boost;" everywhere as a matter of course. The
projects that used "shared_ptr" broke once compilers started adding
support for C++11 (and before the Boost library was fixed to take
this exact situation into account.)

The namespace "std" is reserved for the standard library, and as long
as you don't start putting your own stuff inside it, you can be sure
that no naming collisions will happen with standard stuff. If you never
use "using namespace std;" of course.

In principle this exact problem could be solved by standardizing certain
prefixes, such as stating that any name starting with "std_" is reserved
for the compiler and shouldn't be used by user code. However, namespaces
offer flexibility that simple name prefixes don't (such as namespace
aliases, and importing namespaces into other namespaces.)

--- news://freenews.netfront.net/ - complaints: (e-mail address removed) ---
But that doesn't address the things you asked me about.

"Do you understand why namespaces exist in the first place? Do you
understand why "using namespace" completely nullifies those benefits?"

I still don't know why they exist and what the benefits are.

Bill
 
V

Victor Bazarov

[..]
I still don't know why they exist and what the benefits are.

At the risk of stating pretty much the obvious, namespaces are designed
to help in developing large systems with substantial number of
third-party components in use, and essentially the sole purpose of the
namespaces is to provide a simpler way to manage possible name conflicts
between different components.

Imagine that you're writing some kind of library (for internal
consumption, say) that, along with uniquely named functions and classes,
contains names like 'min' or 'max'. In order to afford your code to use
the specific 'min' (class or function) in your code it is recommended
that you put *your* 'min' into a namespace of your choice, and then in
the module that is only to use your 'min' you could bring that name (and
all names from your own namespace) into the scope by means of the
'using' directive. Or, alternatively, when you need to use both the
standard 'min' and your own, specify which one the compiler is to
consider with the help of the namespace prefix.

If that's something you already knew, then perhaps I didn't understand
your problem, sorry.

V
 
B

Bill Gill

[..]
I still don't know why they exist and what the benefits are.

At the risk of stating pretty much the obvious, namespaces are designed
to help in developing large systems with substantial number of
third-party components in use, and essentially the sole purpose of the
namespaces is to provide a simpler way to manage possible name conflicts
between different components.

Imagine that you're writing some kind of library (for internal
consumption, say) that, along with uniquely named functions and classes,
contains names like 'min' or 'max'. In order to afford your code to use
the specific 'min' (class or function) in your code it is recommended
that you put *your* 'min' into a namespace of your choice, and then in
the module that is only to use your 'min' you could bring that name (and
all names from your own namespace) into the scope by means of the
'using' directive. Or, alternatively, when you need to use both the
standard 'min' and your own, specify which one the compiler is to
consider with the help of the namespace prefix.

If that's something you already knew, then perhaps I didn't understand
your problem, sorry.

V
Thanks, that was pretty much what I was wondering. It makes sense to
think of it that way. Now all I have to do is to figure out when
to use "using namespace" and when to use "namespace::" which is
basically going to be an individual decision based on the circumstances.

Bill
 
V

Victor Bazarov

[..]
I still don't know why they exist and what the benefits are.

At the risk of stating pretty much the obvious, namespaces are designed
to help in developing large systems with substantial number of
third-party components in use, and essentially the sole purpose of the
namespaces is to provide a simpler way to manage possible name conflicts
between different components.

Imagine that you're writing some kind of library (for internal
consumption, say) that, along with uniquely named functions and classes,
contains names like 'min' or 'max'. In order to afford your code to use
the specific 'min' (class or function) in your code it is recommended
that you put *your* 'min' into a namespace of your choice, and then in
the module that is only to use your 'min' you could bring that name (and
all names from your own namespace) into the scope by means of the
'using' directive. Or, alternatively, when you need to use both the
standard 'min' and your own, specify which one the compiler is to
consider with the help of the namespace prefix.

If that's something you already knew, then perhaps I didn't understand
your problem, sorry.

V
Thanks, that was pretty much what I was wondering. It makes sense to
think of it that way. Now all I have to do is to figure out when
to use "using namespace" and when to use "namespace::" which is
basically going to be an individual decision based on the circumstances.

Right. IME the 'using namespace <myown>;' most often finds its place in
the implementation of myown library functions, with all other namespaces
(including 'std') actually spelled out. Anything without the namespace
is clearly "this one", everything else has a namespace prefix (empty for
the global names, of course).

Another approach I've seen was only to use namespace prefix when a
conflict is discovered. I do however view it as deficient because,
especially in template code, there can be no conflict and still you may
not necessarily be fully aware which namespace is used to resolve a
particular name. Sometimes very weird bugs can be introduced by that.

V
 
B

Bill Gill

On 3/6/2013 9:13 AM, Bill Gill wrote:
[..]
I still don't know why they exist and what the benefits are.

At the risk of stating pretty much the obvious, namespaces are designed
to help in developing large systems with substantial number of
third-party components in use, and essentially the sole purpose of the
namespaces is to provide a simpler way to manage possible name conflicts
between different components.

Imagine that you're writing some kind of library (for internal
consumption, say) that, along with uniquely named functions and classes,
contains names like 'min' or 'max'. In order to afford your code to use
the specific 'min' (class or function) in your code it is recommended
that you put *your* 'min' into a namespace of your choice, and then in
the module that is only to use your 'min' you could bring that name (and
all names from your own namespace) into the scope by means of the
'using' directive. Or, alternatively, when you need to use both the
standard 'min' and your own, specify which one the compiler is to
consider with the help of the namespace prefix.

If that's something you already knew, then perhaps I didn't understand
your problem, sorry.

V
Thanks, that was pretty much what I was wondering. It makes sense to
think of it that way. Now all I have to do is to figure out when
to use "using namespace" and when to use "namespace::" which is
basically going to be an individual decision based on the circumstances.

Right. IME the 'using namespace <myown>;' most often finds its place in
the implementation of myown library functions, with all other namespaces
(including 'std') actually spelled out. Anything without the namespace
is clearly "this one", everything else has a namespace prefix (empty for
the global names, of course).

Another approach I've seen was only to use namespace prefix when a
conflict is discovered. I do however view it as deficient because,
especially in template code, there can be no conflict and still you may
not necessarily be fully aware which namespace is used to resolve a
particular name. Sometimes very weird bugs can be introduced by that.

V
I think I will skip the waiting to find a problem method. That sounds
like a recipe for disaster. What I may start doing is to use
'using namespace std;' for the standard libraries. There shouldn't
be any conflicts there, well there shouldn't be, maybe I will go ahead
and use the <myown>::func(), or whatever for everything that isn't
locally declared. I am not really planning to working on any major
projects with many people involved. But still you never know, and if
I get in the habit I will not accidentally miss it if I do get into
a bigger project. That is one place where habit can work for you.
If you get in the habit of doing things the safest way you won't get
bit later.

Bill
for
 
J

Jorgen Grahn

.
But that doesn't address the things you asked me about.

"Do you understand why namespaces exist in the first place? Do you
understand why "using namespace" completely nullifies those benefits?"

I still don't know why they exist and what the benefits are.

Something like this:

Namespaces replace the C convention of adding prefixes to names to:
- mark them as related (e.g. come from the same library)
- mark them as part of a bigger thing (e.g. the Foo library)
- avoid name clashes ("I cannot use library Foo, because I already
use Bar, and both contain a function barney()")

While they were at it, they added obvious enhancements like:
- you don't have to mention the namespace prefix all the time
/inside/ the namespace itself
- some other "it should be obvious which one I mean" situations
- you can say "okay, within this context, whenever I say
string, I mean std::string"
- "using namespace", for when you just don't care at all

(And then there's the anonymous namespaces, and probably other things
I forgot.)

I notice "using namespace" is hardest to motivate. I suspect the main
reason for adding it was to help people migrate to a standard library
with the std:: namespace.

/Jorgen
 
8

88888 Dihedral

Juha Nieminenæ–¼ 2013å¹´3月5日星期二UTC+8下åˆ3時42分08秒寫é“:
Why does this always creep up into beginners' code? Where do they learn

this stuff?



--- news://freenews.netfront.net/ - complaints: (e-mail address removed) ---

How about using several libraries with many functions ?

Because the way of c++ resolves the function identity,
it is better to write c++ with some assistant tools to look
up function declarations carefully.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,222
Members
46,809
Latest member
moe77

Latest Threads

Top