School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!
Welcome to Dream.In.Code
Become an Expert!

Join 340,144 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 3,814 people online right now. Registration is fast and FREE... Join Now!



Error: 'num' was not declared in this scope

Error: 'num' was not declared in this scope book exercise flawed? Rate Topic: -----

#1 bytescribe  Icon User is offline

  • New D.I.C Head
  • Pip
  • Group: Members
  • Posts: 19
  • Joined: 17-January 09


Dream Kudos: 0

Post icon  Posted 17 January 2009 - 08:46 PM

Hi, all.

Both my bf and I are settling down to teach ourselves C++, so he bought a book that looked really helpful.

I'm a few exercises into the second chapter, sitting currently on one that teaches about setting precedence.

I typed in the code precisely the way it is shown in the book, like so:

#include <iostream>
using namespace std;

int main()
{
	num = 1 + 4 * 3;
	cout << endl << "Default order:" << num << endl;
	
	num = ( 1 + 4 ) * 3;
	cout << "Forced order:" << num << endl << endl;
	
	num = 7 - 4 + 2;
	cout << "Default direction:" << num << endl;
	
	num = 7 - ( 4 + 2 );
	cout << "Forced direction:" << num << endl;
		
	return 0;



}



All well and good, right? Well, not exactly. When I went to compile the code (using the GNU compiler in Ubuntu LInux), I got the following error in my terminal screen:

"7: error: ‘num’ was not declared in this scope" (the error is on line 7)

There is a warning note in the margin of my book that says not to rely on default precedence as it may vary between compilers, and to always use parentheses to clarify expressions.

So I tried doing just that...putting parentheses on the calculations that didn't originally have them, where I know the parentheses might go (multiplication before addition, subtraction, etc)...and recompiled, but I still got the same error.

I checked all my braces, brackets, semicolons, everything. I even removed the parentheses and recompiled again. Same error result.

I've sifted through at LEAST five pages of Google results, and nothing even matches my issue. So, what gives? Perhaps the book exercise is flawed...or there's something my compiler doesn't like about this specific exercise.

BB,
Kat ^.^
Was This Post Helpful? 0
  • +
  • -


#2 OliveOyl3471  Icon User is offline

  • Everybody's crazy but me!
  • Icon
  • Group: Moderators
  • Posts: 6,529
  • Joined: 11-July 07


Dream Kudos: 775

Expert In: Basketball

Posted 17 January 2009 - 08:54 PM

Declare your variable with the word 'int' for integer and it will work. You could also use 'float' or 'double' and it would work. :)

#include <iostream>
using namespace std;

int main()
{
	int num = 1 + 4 * 3;
	cout << endl << "Default order:" << num << endl;
	
	num = ( 1 + 4 ) * 3;
	cout << "Forced order:" << num << endl << endl;
	
	num = 7 - 4 + 2;
	cout << "Default direction:" << num << endl;
	
	num = 7 - ( 4 + 2 );
	cout << "Forced direction:" << num << endl;
		
	return 0;



}


Was This Post Helpful? 1
  • +
  • -

#3 no2pencil  Icon User is offline

  • PHoToN PoWeR PaCK : not included
  • Icon
  • View blog
  • Group: Moderators
  • Posts: 15,043
  • Joined: 10-May 07


Dream Kudos: 2875

Expert In: Goofing Off

Posted 17 January 2009 - 08:55 PM

Quote

int main()
{
	num = 1 + 4 * 3;



You never declared it, you just started using it.

C/C++ doesn't assume well, so it needs to know what you are using this variable for. I'm going to guess it's an int, but your compiler will need to know for sure.
Was This Post Helpful? 1
  • +
  • -

#4 bytescribe  Icon User is offline

  • New D.I.C Head
  • Pip
  • Group: Members
  • Posts: 19
  • Joined: 17-January 09


Dream Kudos: 0

Posted 17 January 2009 - 09:53 PM

Problem solved.

I had a funny feeling that was my problem all along...and I went back to look at the directions for the exercise...and there it was, telling me to declare the integer variable. :rolleyes: How silly of me to assume they'd go ahead and print the 'int' next to the 'num' just in case some impatient goofball of a newbie doesn't completely read the directions. :rolleyes:

I feel sorta stupid now. But oh, well...I ended up going on a research journey and found you guys....and I ended up learning something..."declare all variables because C++ can't read minds." :P

Thanks for the help, anyway. :D

BB,
Kat ^.^
Was This Post Helpful? 0
  • +
  • -

#5 no2pencil  Icon User is offline

  • PHoToN PoWeR PaCK : not included
  • Icon
  • View blog
  • Group: Moderators
  • Posts: 15,043
  • Joined: 10-May 07


Dream Kudos: 2875

Expert In: Goofing Off

Posted 17 January 2009 - 09:56 PM

View Postbytescribe, on 17 Jan, 2009 - 11:53 PM, said:

I ended up learning something..."declare all variables because C++ can't read minds." :P

heh, I only put it that way b/c in VB you can get away with "Garbage Code" where you don't have to declare a variable as the type you use it. I guess it can do type conversion on the fly.

I'm not sure if that makes the environment more advanced, or makes for more garbage programmers.

Either way, glad to see you got it working!
Was This Post Helpful? 0
  • +
  • -

#6 OliveOyl3471  Icon User is offline

  • Everybody's crazy but me!
  • Icon
  • Group: Moderators
  • Posts: 6,529
  • Joined: 11-July 07


Dream Kudos: 775

Expert In: Basketball

Posted 17 January 2009 - 10:00 PM

View Postbytescribe, on 17 Jan, 2009 - 11:53 PM, said:

I ended up going on a research journey and found you guys.

Then it was definitely worth it, because this site is the coolest ever! And it's the best programming help site I have ever seen. Stick around and you'll see what I mean.

Welcome to dream.in.code! :)
Was This Post Helpful? 0
  • +
  • -

#7 Pwn  Icon User is offline

  • D.I.C Regular
  • PipPipPip
  • Group: Members
  • Posts: 380
  • Joined: 25-November 07


Dream Kudos: 0

Posted 18 January 2009 - 04:44 AM

View Postbytescribe, on 17 Jan, 2009 - 11:53 PM, said:

I ended up going on a research journey and found you guys.


...and you should check out the tutorials section, they have plenty.
Was This Post Helpful? 0
  • +
  • -



Fast Reply

  

3 User(s) are reading this topic
0 members, 3 guests, 0 anonymous users



Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month