[SOLVED]

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

User avatar
jtst1
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 80
Joined: Tue Sep 30, 2008 4:32 pm
Location: Atlanta Georgia

[SOLVED]

Post by jtst1 »

It seems that Microsoft hates me lol. Basically it's giving me an error saying "cout and cin are undeclared" when i put #include <iostream> and using namespace std;. here's my code:

Code: Select all

#include <iostream>
using namespace std
int Area(int length, int width);
int main()
{
int lengthOfYard;
int widthOfYard;
int areaOfYard;
cout << "\nHow wide is your yard?";
cin >> widthOfYard;
cout << "\nHow long is your yard?";
cin >> lengthOfYard;
areaOfYard = Area(lengthOfYard, widthOfYard);
cout << "\nYour yard is "; << areaOfYard << "in square feet\n\n";
	return 0;
}

int Area(int yardLength, int yardWidth)
{
	return yardLength * yardWidth;
}
one more thing, why is it that vc++ tells me to put #include <stdafx.h> but that doesnt resolve it
Last edited by jtst1 on Tue Aug 04, 2009 5:40 pm, edited 2 times in total.
When One learns to Love, One must bear the risk of Hatred.
andrew
Chaos Rift Regular
Chaos Rift Regular
Posts: 121
Joined: Mon Dec 08, 2008 2:12 pm

Re: Visual c++ 2008 version

Post by andrew »

Code: Select all

using namespace std
should be:

Code: Select all

using namespace std;
If you make your code neater it will be much easier to spot things like this. ;)

EDIT:

Code: Select all

#include <iostream>

using namespace std;

int Area(int, int);

int main()
{
    int lengthOfYard;
    int widthOfYard;
    int areaOfYard;

    cout << "\nHow wide is your yard?";
    cin >> widthOfYard;

    cout << "\nHow long is your yard?";
    cin >> lengthOfYard;

    areaOfYard = Area(lengthOfYard, widthOfYard);
    cout << "\nYour yard is "; << areaOfYard << "in square feet\n\n";

    return 0;
}

int Area(int yardLength, int yardWidth)
{
   return yardLength * yardWidth;
}
There is still an error, see if you can spot it.
User avatar
hurstshifter
ES Beta Backer
ES Beta Backer
Posts: 713
Joined: Mon Jun 08, 2009 8:33 pm
Favorite Gaming Platforms: SNES
Programming Language of Choice: C/++
Location: Boston, MA
Contact:

Re: Visual c++ 2008 version

Post by hurstshifter »

I see the errrrrrooorrrr...... ;)
"Time is an illusion. Lunchtime, doubly so."
http://www.thenerdnight.com
User avatar
jtst1
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 80
Joined: Tue Sep 30, 2008 4:32 pm
Location: Atlanta Georgia

Re: Visual c++ 2008 version

Post by jtst1 »

andrew wrote:

Code: Select all

using namespace std
should be:

Code: Select all

using namespace std;
If you make your code neater it will be much easier to spot things like this. ;)

EDIT:

Code: Select all

#include <iostream>

using namespace std;

int Area(int, int);

int main()
{
    int lengthOfYard;
    int widthOfYard;
    int areaOfYard;

    cout << "\nHow wide is your yard?";
    cin >> widthOfYard;

    cout << "\nHow long is your yard?";
    cin >> lengthOfYard;

    areaOfYard = Area(lengthOfYard, widthOfYard);
    cout << "\nYour yard is "; << areaOfYard << "in square feet\n\n";

    return 0;
}

int Area(int yardLength, int yardWidth)
{
   return yardLength * yardWidth;
}
There is still an error, see if you can spot it.
Was the error the extra ";"

Code: Select all

   cout << "\nYour yard is "; << areaOfYard << "in square feet\n\n";
Edit-------
well i read the note about stdafx.h, and i just went into the header and added #include <iostream> and using namespace std; Worked perfectly. I have to put any other header requirements into stdafx...weird eh?
When One learns to Love, One must bear the risk of Hatred.
andrew
Chaos Rift Regular
Chaos Rift Regular
Posts: 121
Joined: Mon Dec 08, 2008 2:12 pm

Re: Visual c++ 2008 version

Post by andrew »

jtst1 wrote:Was the error the extra ";"
That's it. :)
Edit-------
well i read the note about stdafx.h, and i just went into the header and added #include <iostream> and using namespace std; Worked perfectly. I have to put any other header requirements into stdafx...weird eh?
You shouldn't need stdafx.h for this code.
User avatar
jtst1
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 80
Joined: Tue Sep 30, 2008 4:32 pm
Location: Atlanta Georgia

Re: Visual c++ 2008 version

Post by jtst1 »

andrew wrote:
jtst1 wrote:Was the error the extra ";"
That's it. :)
Edit-------
well i read the note about stdafx.h, and i just went into the header and added #include <iostream> and using namespace std; Worked perfectly. I have to put any other header requirements into stdafx...weird eh?
You shouldn't need stdafx.h for this code.
I know, it's weird but everytime i take it out and put #include <iostream> and using namespace std; it gives me an error.
O well atleast I can get back to learning!! :P
When One learns to Love, One must bear the risk of Hatred.
User avatar
JaxDragon
Chaos Rift Junior
Chaos Rift Junior
Posts: 395
Joined: Mon Aug 04, 2008 2:03 pm
Current Project: Kanoba Engine
Favorite Gaming Platforms: PS3, PC
Programming Language of Choice: C++
Location: Northeast NC

Re: Visual c++ 2008 version

Post by JaxDragon »

A c++ book I have says that you NEED stdafx.h in every visual studio application. I never had to, but I guess it's true for some people.
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Re: Visual c++ 2008 version

Post by MarauderIIC »

If you turn off precompiled headers in your compilation options, you don't need it.
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
User avatar
jtst1
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 80
Joined: Tue Sep 30, 2008 4:32 pm
Location: Atlanta Georgia

Re: Visual c++ 2008 version

Post by jtst1 »

MarauderIIC wrote:If you turn off precompiled headers in your compilation options, you don't need it.
Ahh thank you Marauder.
I figured I might as well post this in this post as not to make another.
In my book it was talking about Recursion, and using the Fibonacci series, now i get the whole 1 1 2 3 5 8 13 21 thing, but I don't understand the code, and the book did a shitty job or explaining it. here's the code if anyone wants to look at it.

Code: Select all

#include "stdafx.h"

int fib (int n);

	int main()
	{
		int n, answer;

		cout << "Enter number to find: ";
		cin >> n;

		cout << "\n\n";
		answer = fib(n);

		cout << answer << " Is the " << n << "th Fibonacci number\n";
		Sleep(50000); // I added the sleep
		return 0;
	}

			int fib (int n)
		{
			cout << "Processing fib)" << n << ")...";
			if (n < 3)
			{
				cout << "Return 1!\n";
				return (1);
			}
			else
			{
				cout << "Call fib(" << n-2 << ") and fib(" << n-1 << ").\n";
				return( fib(n-2) + fib(n-1));
			}
		}
When One learns to Love, One must bear the risk of Hatred.
User avatar
short
ES Beta Backer
ES Beta Backer
Posts: 548
Joined: Thu Apr 30, 2009 2:22 am
Current Project: c++, c
Favorite Gaming Platforms: SNES, PS2, SNES, SNES, PC NES
Programming Language of Choice: c, c++
Location: Oregon, US

Re: Visual c++ 2008 version

Post by short »

What exactly would you liked explained about the series?
My github repository contains the project I am currently working on,
link: https://github.com/bjadamson
User avatar
jtst1
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 80
Joined: Tue Sep 30, 2008 4:32 pm
Location: Atlanta Georgia

Re: Visual c++ 2008 version

Post by jtst1 »

I would like to understand what the code does, like the n-2+n-1 I don't understand what n is, and F
When One learns to Love, One must bear the risk of Hatred.
User avatar
short
ES Beta Backer
ES Beta Backer
Posts: 548
Joined: Thu Apr 30, 2009 2:22 am
Current Project: c++, c
Favorite Gaming Platforms: SNES, PS2, SNES, SNES, PC NES
Programming Language of Choice: c, c++
Location: Oregon, US

Re: Visual c++ 2008 version

Post by short »

First off, I made a few code changes so that it would compile with my compiler, as well as I commented on a few coding changes I made.

Code: Select all

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

int fib (int n);

int main()
{
	int n, answer;

	cout << "Enter number to find: ";
	cin >> n;

	cout << "\n\n";
	answer = fib(n);

	cout << answer << " Is the " << n << "th Fibonacci number\n";
	//Sleep(50000); // I added the sleep
	cin >> answer; // I changed this because calling sleep is an awful habit to get into. It is very cpu intensive,
	// calling cin just waits for an input thats not null then exits in this case.
	return 0;
   }

int fib(int n)
{
	cout << "Processing fib)" << n << ")...";
	if (n < 3)
	{
		cout << "Return 1!\n";
		return(1);
	}
	else
	{
		cout << "Call fib(" << n-2 << ") and fib(" << n-1 << ").\n";
		return( fib(n-2) + fib(n-1));
	}
}
Note: I'm awful at explaining things.

The fib(int n) function calls itself in the return statement You can see that

Code: Select all

 return (fib(n-2) + fib(n-1))
The Fibonacci series is found using recursion.
Mathematically it can be shown as Image. You can see that the series is defined using Fib(n-1) + Fib(n-2), the same as your return statement.

When your return statement is called, it calls fib using n-1, without actually ever finishing the return statement. Instead it goes on to call fib(n-1), creating a sort of back order or stack of functions that have been called. These are stored on the computers stack, which you will learn about later on. It keeps on doing this until eventually the base case is reached (n < 3). Every time it calls fib(n-1) it creates a stack of function calls. You know each function is never finished until the base case is reached. One the base case is reached, fib finally returns a value of 1. Once this happens, it has a "stack" of fib calls to return through, finally able to close all those open function calls until it gets back to the very first fib(n) call where it returns a value to answer which is then printed to the screen. As you can see on every return call it calls fib twice. I don't exactly remember how it handles both calls, of whether or not it handles the fib(n-2) after all the fib(f-1) have returned or not.

If someone knows feel free to remind me.

Also, I realize my explanation may be confusing, It's a hard concept to grasp, but even harder to explain (for me personally). I suggest you do some looking up on recursion, and if you really want an understanding take a look at induction. It's a way to prove things mathematically. Hopefully someone will come along and can explain it a little bit better then I can. Let me know if anything doesn't make sense and I will try.
My github repository contains the project I am currently working on,
link: https://github.com/bjadamson
User avatar
jtst1
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 80
Joined: Tue Sep 30, 2008 4:32 pm
Location: Atlanta Georgia

Re: Visual c++ 2008 version

Post by jtst1 »

short0014 wrote:First off, I made a few code changes so that it would compile with my compiler, as well as I commented on a few coding changes I made.

Code: Select all

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

int fib (int n);

int main()
{
	int n, answer;

	cout << "Enter number to find: ";
	cin >> n;

	cout << "\n\n";
	answer = fib(n);

	cout << answer << " Is the " << n << "th Fibonacci number\n";
	//Sleep(50000); // I added the sleep
	cin >> answer; // I changed this because calling sleep is an awful habit to get into. It is very cpu intensive,
	// calling cin just waits for an input thats not null then exits in this case.
	return 0;
   }

int fib(int n)
{
	cout << "Processing fib)" << n << ")...";
	if (n < 3)
	{
		cout << "Return 1!\n";
		return(1);
	}
	else
	{
		cout << "Call fib(" << n-2 << ") and fib(" << n-1 << ").\n";
		return( fib(n-2) + fib(n-1));
	}
}
Note: I'm awful at explaining things.

The fib(int n) function calls itself in the return statement You can see that

Code: Select all

 return (fib(n-2) + fib(n-1))
The Fibonacci series is found using recursion.
Mathematically it can be shown as Image. You can see that the series is defined using Fib(n-1) + Fib(n-2), the same as your return statement.

When your return statement is called, it calls fib using n-1, without actually ever finishing the return statement. Instead it goes on to call fib(n-1), creating a sort of back order or stack of functions that have been called. These are stored on the computers stack, which you will learn about later on. It keeps on doing this until eventually the base case is reached (n < 3). Every time it calls fib(n-1) it creates a stack of function calls. You know each function is never finished until the base case is reached. One the base case is reached, fib finally returns a value of 1. Once this happens, it has a "stack" of fib calls to return through, finally able to close all those open function calls until it gets back to the very first fib(n) call where it returns a value to answer which is then printed to the screen. As you can see on every return call it calls fib twice. I don't exactly remember how it handles both calls, of whether or not it handles the fib(n-2) after all the fib(f-1) have returned or not.

If someone knows feel free to remind me.

Also, I realize my explanation may be confusing, It's a hard concept to grasp, but even harder to explain (for me personally). I suggest you do some looking up on recursion, and if you really want an understanding take a look at induction. It's a way to prove things mathematically. Hopefully someone will come along and can explain it a little bit better then I can. Let me know if anything doesn't make sense and I will try.
I sort of understand what you are saying, and as you said it's a hard thing to grasp. Honestly i'm not understanding how it gets that "blank" is the n'th number in the fib series. But you did help me sort of understand it, so Thank you. I'll go look up recursion and induction.
When One learns to Love, One must bear the risk of Hatred.
User avatar
short
ES Beta Backer
ES Beta Backer
Posts: 548
Joined: Thu Apr 30, 2009 2:22 am
Current Project: c++, c
Favorite Gaming Platforms: SNES, PS2, SNES, SNES, PC NES
Programming Language of Choice: c, c++
Location: Oregon, US

Re: Visual c++ 2008 version

Post by short »

blank?
My github repository contains the project I am currently working on,
link: https://github.com/bjadamson
User avatar
jtst1
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 80
Joined: Tue Sep 30, 2008 4:32 pm
Location: Atlanta Georgia

Re: Visual c++ 2008 version

Post by jtst1 »

The blank was for a number any number. How does it know that 8 is the 6th number in the fib series? or 10 is some other number in the series.
When One learns to Love, One must bear the risk of Hatred.
Post Reply