Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Why is my code showing this error?

Reply
Thread Tools

Why is my code showing this error?

 
 
eli m
Guest
Posts: n/a
 
      03-05-2013
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?
 
Reply With Quote
 
 
 
 
Geoff
Guest
Posts: n/a
 
      03-05-2013
On Mon, 4 Mar 2013 21:03:06 -0800 (PST), eli m <(E-Mail Removed)>
wrote:

>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.

> {
> cout << "What math operation do you want to use?";
> cin >> mathop;
> }
>
>}
>
>What am i doing wrong?

 
Reply With Quote
 
 
 
 
Tiib
Guest
Posts: n/a
 
      03-05-2013
On Tuesday, 5 March 2013 07:03:06 UTC+2, eli m wrote:
> 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/usi...space-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.
 
Reply With Quote
 
Geoff
Guest
Posts: n/a
 
      03-05-2013
On Tue, 5 Mar 2013 07:42:08 +0000 (UTC), Juha Nieminen
<(E-Mail Removed)> wrote:

>eli m <(E-Mail Removed)> wrote:
>> using namespace std;

>
>Why does this always creep up into beginners' code? Where do they learn
>this stuff?
>
>--- news://freenews.netfront.net/ - complaints: http://www.velocityreviews.com/forums/(E-Mail Removed) ---


it sows up because lazy programmers coding snippets and samples always
seem to start with it. Cargo cult.
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      03-05-2013
On Tuesday, 5 March 2013 07:42:08 UTC, Juha Nieminen wrote:
> eli m <(E-Mail Removed)> wrote:


> > using namespace std;


> 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.)

--
James
 
Reply With Quote
 
Bill Gill
Guest
Posts: n/a
 
      03-05-2013
On 3/5/2013 7:49 AM, Geoff wrote:
> On Tue, 5 Mar 2013 07:42:08 +0000 (UTC), Juha Nieminen
> <(E-Mail Removed)> wrote:
>
>> eli m <(E-Mail Removed)> wrote:
>>> using namespace std;

>>
>> Why does this always creep up into beginners' code? Where do they learn
>> this stuff?
>>
>> --- news://freenews.netfront.net/ - complaints: (E-Mail Removed) ---

>
> 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
 
Reply With Quote
 
Stefan Ram
Guest
Posts: n/a
 
      03-05-2013
Bill Gill <(E-Mail Removed)> writes:
>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.

 
Reply With Quote
 
Jorgen Grahn
Guest
Posts: n/a
 
      03-05-2013
On Tue, 2013-03-05, James Kanze wrote:
> On Tuesday, 5 March 2013 07:42:08 UTC, Juha Nieminen wrote:
>> eli m <(E-Mail Removed)> wrote:

>
>> > using namespace std;

>
>> 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.)


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

--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .
 
Reply With Quote
 
Paul N
Guest
Posts: n/a
 
      03-05-2013
On Mar 5, 6:40*pm, Bill Gill <(E-Mail Removed)> wrote:

> 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.
 
Reply With Quote
 
eli m
Guest
Posts: n/a
 
      03-05-2013
Sorry guys, i just started learning c++ yesterday and i am coming from python.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Sh Crypto isakmp sa is showing Active but Tunnel is showing line protocol down. urvin Cisco 0 04-15-2008 08:17 AM
why why why why why Mr. SweatyFinger ASP .Net 4 12-21-2006 01:15 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
ASP 1.1: DataGrid - Showing/Not Showing Buttons Ray Booysen ASP .Net 2 03-28-2006 02:49 PM
IE downloading, not showing. Why? Samul van Laere HTML 12 01-05-2005 08:47 PM



Advertisments