| View previous topic :: View next topic |
| Author |
Message |
Chase Payne Grandmaster Cheater
Reputation: 1
Joined: 20 Mar 2008 Posts: 533
|
Posted: Thu Aug 20, 2009 8:07 pm Post subject: Using booleans in C++... |
|
|
Again, I fail at programming... for some reason when I compile this:
| Code: | // Tutop.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
long double X;
long double Y;
int checker();
int chChoice;
char YNyes;
int Universe();
using namespace std;
bool AngelWings;
int Item();
int main()
{
if (AngelWings = true)
cout << "You already completed the game." << endl;
else
cout << "Enter a value: ";
cin >> X;
cout << "Enter a second value: ";
cin >> Y;
checker();
}
int checker()
{
using namespace std;
cout << "\nEnter one of the following: +, -, *, or /";
char chChoice;
cin >> chChoice;
if (chChoice == '+')
{
cout << X << " + " << Y << " is " << (X + Y) << endl;
cout << "Would you like to enter another equation!?\n Enter as Y/N.\n";
char YNyes;
cin >> YNyes;
Universe();
if (YNyes == 'y')
main();
else
cout << "\nOkay, you made it clear you don't\n";
return 0;
}
if (chChoice == '-')
{
cout << X << " - " << Y << " is " << (X - Y) << endl;
cout << "Would you like to enter another equation!?\n Enter as Y/N.\n";
char YNyes;
cin >> YNyes;
if (YNyes == 'y')
main();
else
cout << "\nOkay, you made it clear you don't\n";
return 0;
}
if (chChoice == '*')
{
cout << X << " * " << Y << " is " << (X * Y) << endl;
cout << "Would you like to enter another equation!?\n Enter as Y/N.\n";
char YNyes;
cin >> YNyes;
if (YNyes == 'y')
main();
else
cout << "\nOkay, you made it clear you don't\n";
return 0;
}
if (chChoice == '/')
{
cout << X << " / " << Y << " is " << (X / Y) << endl;
cout << "Would you like to enter another equation!?\n Enter as Y/N.\n";
char YNyes;
cin >> YNyes;
if (YNyes == 'y')
main();
else
cout << "\nOkay, you made it clear you don't\n";
return 0;
return 0;
}
else
{
cout << "Sorry, invalid character.\t\t\t\t";
checker();
return 0;
}
}
int Universe()
{
if (10 == X+Y)
cout << "\nYou recieved an item.\n";
Item();
else
main();
return 0;
}
int Item()
{
bool AngelWings = true;
return 0;
} |
I get compile error:
1>Compiling...
1>Tutop.cpp
1>c:\users\chase\documents\visual studio 2008\projects\tutop\tutop\tutop.cpp(97) : error C2181: illegal else without matching if
1>Build log was saved at "file://c:\Users\Chase\Documents\Visual Studio 2008\Projects\Tutop\Tutop\Debug\BuildLog.htm"
1>Tutop - 1 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
(The else it's complaining at is on the bottom of the script on else jump to main()
What's wrong with the IF?
If I remove the Item(); it works fine?
But then the entire script is pointless....
|
|
| Back to top |
|
 |
Flyte Peanuts!!!!
Reputation: 6
Joined: 19 Apr 2006 Posts: 1887 Location: Canada
|
|
| Back to top |
|
 |
Chase Payne Grandmaster Cheater
Reputation: 1
Joined: 20 Mar 2008 Posts: 533
|
Posted: Thu Aug 20, 2009 8:45 pm Post subject: |
|
|
That was written in 1988, also it's C and not C++ >.<
So lots of functions are missing....
|
|
| Back to top |
|
 |
oib111 I post too much
Reputation: 0
Joined: 02 Apr 2007 Posts: 2947 Location: you wanna know why?
|
Posted: Thu Aug 20, 2009 8:52 pm Post subject: |
|
|
Learn how to use brackets. Change it to:
| Code: |
int Universe()
{
if (10 == X+Y) {
cout << "\nYou recieved an item.\n";
Item();
}
else {
main();
}
return 0;
}
|
When you don't use brackets the only code executed is the line proceeding the if, else if, or else statement. Knowing that I'm sure you can see where you went wrong.
_________________
| 8D wrote: |
cigs dont make people high, which weed does, which causes them to do bad stuff. like killing |
|
|
| Back to top |
|
 |
Chase Payne Grandmaster Cheater
Reputation: 1
Joined: 20 Mar 2008 Posts: 533
|
Posted: Thu Aug 20, 2009 8:58 pm Post subject: |
|
|
| oib111 wrote: | Learn how to use brackets. Change it to:
| Code: |
int Universe()
{
if (10 == X+Y) {
cout << "\nYou recieved an item.\n";
Item();
}
else {
main();
}
return 0;
}
|
When you don't use brackets the only code executed is the line proceeding the if, else if, or else statement. Knowing that I'm sure you can see where you went wrong. |
HOld on... <edit>
|
|
| Back to top |
|
 |
oib111 I post too much
Reputation: 0
Joined: 02 Apr 2007 Posts: 2947 Location: you wanna know why?
|
Posted: Thu Aug 20, 2009 9:18 pm Post subject: |
|
|
What?
_________________
| 8D wrote: |
cigs dont make people high, which weed does, which causes them to do bad stuff. like killing |
|
|
| Back to top |
|
 |
Chase Payne Grandmaster Cheater
Reputation: 1
Joined: 20 Mar 2008 Posts: 533
|
Posted: Thu Aug 20, 2009 9:21 pm Post subject: |
|
|
| Code: | // Tutop.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
long double X;
long double Y;
int checker();
int chChoice;
char YNyes;
void Universe();
using namespace std;
bool AngelWings;
void Item();
int main()
{
if (AngelWings == false)
cout << "You already completed the game." << endl;
else
cout << "Enter a value: ";
cin >> X;
cout << "Enter a second value: ";
cin >> Y;
if (X <= 1)
{
cout << "Invalid Character\n";
return 0;
main();
if (Y <= 1)
cout << "Invalid Character\n";
return 0;
main();
}
else
{
checker();
}
}
int checker()
{
cout << "\nEnter one of the following: +, -, *, or /";
char chChoice;
cin >> chChoice;
if (chChoice == '+')
{
cout << X << " + " << Y << " is " << (X + Y) << endl;
cout << "Would you like to enter another equation!?\n Enter as Y/N.\n";
char YNyes;
cin >> YNyes;
Universe();
if (YNyes == 'y')
main();
else
cout << "\nOkay, you made it clear you don't\n";
return 0;
}
if (chChoice == '-')
{
cout << X << " - " << Y << " is " << (X - Y) << endl;
cout << "Would you like to enter another equation!?\n Enter as Y/N.\n";
char YNyes;
cin >> YNyes;
if (YNyes == 'y')
main();
else
cout << "\nOkay, you made it clear you don't\n";
return 0;
}
if (chChoice == '*')
{
cout << X << " * " << Y << " is " << (X * Y) << endl;
cout << "Would you like to enter another equation!?\n Enter as Y/N.\n";
cin >> YNyes;
if (YNyes == 'y')
main();
else
cout << "\nOkay, you made it clear you don't\n";
return 0;
}
if (chChoice == '/')
{
cout << X << " / " << Y << " is " << (X / Y) << endl;
cout << "Would you like to enter another equation!?\n Enter as Y/N.\n";
cin >> YNyes;
if (YNyes == 'y')
main();
else
cout << "\nOkay, you made it clear you don't\n";
return 0;
}
else
{
cout << "Sorry, invalid character.\t\t\t\t";
checker();
return 0;
}
}
void Universe()
{
if (10 == X+Y) {
cout << "\nYou recieved an item.\n";
Item();
}
else {
main();
}
}
void Item()
{
bool AngelWings = false;
}
|
Okay now this is strictly a boolean issue...
It jumps to item, I executed a message there before but it doesn't set Angel Wings to False... and it ignored the start message.
|
|
| Back to top |
|
 |
hcavolsdsadgadsg I'm a spammer
Reputation: 26
Joined: 11 Jun 2007 Posts: 5801
|
Posted: Thu Aug 20, 2009 9:51 pm Post subject: |
|
|
1. You're declaring another local variable named AngelWings in Item(), not setting your global one.
2. Don't call main(), you'll overflow your stack eventually and your program will crash.
|
|
| Back to top |
|
 |
Chase Payne Grandmaster Cheater
Reputation: 1
Joined: 20 Mar 2008 Posts: 533
|
Posted: Thu Aug 20, 2009 9:52 pm Post subject: |
|
|
| slovach wrote: | 1. You're declaring another local variable named AngelWings in Item(), not setting your global one.
2. Don't call main(), you'll overflow your stack eventually and your program will crash. |
How do I set a "global" one?
Oh, nevermind.
|
|
| Back to top |
|
 |
Flyte Peanuts!!!!
Reputation: 6
Joined: 19 Apr 2006 Posts: 1887 Location: Canada
|
Posted: Fri Aug 21, 2009 4:07 pm Post subject: |
|
|
| Chase Payne wrote: | That was written in 1988, also it's C and not C++ >.<
So lots of functions are missing.... |
It does not matter when it was written. You clearly do not understand even the basics of the language.
| slovach wrote: | | 2. Don't call main(), you'll overflow your stack eventually and your program will crash. |
While it's bad form to call the entry point, there is absolutely nothing wrong with recursive functions.
|
|
| Back to top |
|
 |
Slugsnack Grandmaster Cheater Supreme
Reputation: 71
Joined: 24 Jan 2007 Posts: 1857
|
Posted: Fri Aug 21, 2009 4:46 pm Post subject: |
|
|
building on that, if you think your program is going to be highly recursive you can actually specify the stack to be larger in your compile options
just a little more info ^_^ the system has a guard page at the end of the committed stack and accessing that causes the system to increase the committed size
there is still a limit though which is the total reserved size of the stack which you don't wanna go past ; )
usually MS linker seems to do a default commit size of 4096 bytes and 1MB of reserve. /STACK can be used to change stack allocations :
http://msdn.microsoft.com/en-us/library/8cxs58a6(VS.80).aspx
|
|
| Back to top |
|
 |
Chase Payne Grandmaster Cheater
Reputation: 1
Joined: 20 Mar 2008 Posts: 533
|
Posted: Fri Aug 21, 2009 6:09 pm Post subject: |
|
|
| Athaem wrote: | | Chase Payne wrote: | That was written in 1988, also it's C and not C++ >.<
So lots of functions are missing.... |
It does not matter when it was written. You clearly do not understand even the basics of the language.
| slovach wrote: | | 2. Don't call main(), you'll overflow your stack eventually and your program will crash. |
While it's bad form to call the entry point, there is absolutely nothing wrong with recursive functions. |
Missing a bracket makes me know nothing about the language? MMK.
|
|
| Back to top |
|
 |
oib111 I post too much
Reputation: 0
Joined: 02 Apr 2007 Posts: 2947 Location: you wanna know why?
|
Posted: Fri Aug 21, 2009 8:44 pm Post subject: |
|
|
He's saying based on your post, he noticed you're using bad practices and are showing a large lack of knowledge of how the language works.
_________________
| 8D wrote: |
cigs dont make people high, which weed does, which causes them to do bad stuff. like killing |
|
|
| Back to top |
|
 |
|