| View previous topic :: View next topic |
| Author |
Message |
The Fish Expert Cheater
Reputation: 3
Joined: 11 Aug 2008 Posts: 114 Location: Ohio bitch
|
Posted: Tue Mar 01, 2011 11:21 am Post subject: Error with this code |
|
|
| Code: | // Calculator+, by James Fisher
// Include the iostream library
#include <iostream>
//Use the standard namespace
using namespace std;
void main ( )
{
goto main;
{
// Declare the variables
float Number_1;
float Number_2;
float Result;
float Restart;
int Which_Calculation;
int Restart;
int main;
// Give instructions
cout << "Choose a task. Press 1 to add, 2 to subtract, 3 to multiply, and 4 to divide." << endl;
cin >> Which_Calculation;
// Get numbers
cout << "Please enter the first number." << endl;
cin >> Number_1;
cout << "Please enter the second number." << endl;
cin >> Number_2;
if (Which_Calculation == 1)
{
// Calculate the result
Result = Number_1 + Number_2;
}
if (Which_Calculation == 2)
{
// Calculate the result
Result = Number_1 - Number_2;
}
if (Which_Calculation == 3)
{
// Calculate the result
Result = Number_1 * Number_2;
}
if (Which_Calculation == 4)
{
// Calculate the result
Result = Number_1 / Number_2;
}
// Print the answer is...
cout << "The answer is..." << endl;
// Print the result
cout << Result << endl;
}
// Ask if the user would like to do another problem
cout << "Would you like to do another problem?." << endl;
cin >> Restart >> endl;
if (Restart == 1)
{
// Go to the start
goto main;
}
if (Restart == 2)
{
// end the program
system ("PAUSE");
cout << "Goodbye. Hit any key to exit." << endl;
}
} |
The error says "1>c:\users\Admin\documents\visual studio 2010\projects\calculator\calculator\calculator.cpp(66): error C2065: 'Restart' : undeclared identifier"
_________________
It was nice while it lasted. |
|
| Back to top |
|
 |
Aviar³ Grandmaster Cheater
Reputation: 50
Joined: 03 Jan 2008 Posts: 655 Location: Canada
|
Posted: Tue Mar 01, 2011 11:27 am Post subject: |
|
|
Haven't declared Restart as a variable or function or pretty much anything. Error is on line 66 or 65.
P.S.: Why the fuck are you using goto?
_________________
This is the inception of deception, checking the depth of your perception.
 |
|
| Back to top |
|
 |
Aniblaze Grandmaster Cheater Supreme
Reputation: 138
Joined: 23 Apr 2006 Posts: 1757 Location: The Netherlands
|
Posted: Tue Mar 01, 2011 11:29 am Post subject: |
|
|
| FYI, this is a recursive function gone horribly, horribly wrong. For the love of god, don't use goto. It's like the AIDS, but for code.
|
|
| Back to top |
|
 |
The Fish Expert Cheater
Reputation: 3
Joined: 11 Aug 2008 Posts: 114 Location: Ohio bitch
|
Posted: Tue Mar 01, 2011 11:30 am Post subject: |
|
|
| Aviar³ wrote: | Haven't declared Restart as a variable or function or pretty much anything. Error is on line 66 or 65.
P.S.: Why the fuck are you using goto? |
Couldn't figure out how to do a loop.
As stated yesterday, I don't have a book or anything.
_________________
It was nice while it lasted. |
|
| Back to top |
|
 |
Aniblaze Grandmaster Cheater Supreme
Reputation: 138
Joined: 23 Apr 2006 Posts: 1757 Location: The Netherlands
|
Posted: Tue Mar 01, 2011 11:31 am Post subject: |
|
|
| The Fish wrote: | | Aviar³ wrote: | Haven't declared Restart as a variable or function or pretty much anything. Error is on line 66 or 65.
P.S.: Why the fuck are you using goto? |
Couldn't figure out how to do a loop.
As stated yesterday, I don't have a book or anything. |
You can call the main function again.
|
|
| Back to top |
|
 |
Noz3001 I'm a spammer
Reputation: 26
Joined: 29 May 2006 Posts: 6220 Location: /dev/null
|
Posted: Tue Mar 01, 2011 11:31 am Post subject: |
|
|
| The Fish wrote: | | Aviar³ wrote: | Haven't declared Restart as a variable or function or pretty much anything. Error is on line 66 or 65.
P.S.: Why the fuck are you using goto? |
Couldn't figure out how to do a loop.
As stated yesterday, I don't have a book or anything. |
Go out and buy one before you become a terrible programmer and make everyone else's life difficult
|
|
| Back to top |
|
 |
The Fish Expert Cheater
Reputation: 3
Joined: 11 Aug 2008 Posts: 114 Location: Ohio bitch
|
Posted: Tue Mar 01, 2011 11:32 am Post subject: |
|
|
| Noz3001 wrote: | | The Fish wrote: | | Aviar³ wrote: | Haven't declared Restart as a variable or function or pretty much anything. Error is on line 66 or 65.
P.S.: Why the fuck are you using goto? |
Couldn't figure out how to do a loop.
As stated yesterday, I don't have a book or anything. |
Go out and buy one before you become a terrible programmer and make everyone else's life difficult |
I'm dead broke.
_________________
It was nice while it lasted. |
|
| Back to top |
|
 |
Aviar³ Grandmaster Cheater
Reputation: 50
Joined: 03 Jan 2008 Posts: 655 Location: Canada
|
Posted: Tue Mar 01, 2011 11:32 am Post subject: |
|
|
| Augustine wrote: | | FYI, this is a recursive function gone horribly, horribly wrong. For the love of god, don't use goto. It's like the AIDS, but for code. |
I can't tell since theres so many parenthesis, but it looks like a scope problem. Also, I'd think it's safe enough to say goto IS the AIDs of code. People tended to overuse it in fortran and tons of other legacy code, everyone raged.
P.S.: Get an ebook. Plenty of free online tutorials and ebooks, particularly when it comes to C/C++.
_________________
This is the inception of deception, checking the depth of your perception.
 |
|
| Back to top |
|
 |
The Fish Expert Cheater
Reputation: 3
Joined: 11 Aug 2008 Posts: 114 Location: Ohio bitch
|
Posted: Tue Mar 01, 2011 11:34 am Post subject: |
|
|
| Augustine wrote: | | You can call the main function again. |
How do I do that?
_________________
It was nice while it lasted. |
|
| Back to top |
|
 |
Aniblaze Grandmaster Cheater Supreme
Reputation: 138
Joined: 23 Apr 2006 Posts: 1757 Location: The Netherlands
|
Posted: Tue Mar 01, 2011 12:18 pm Post subject: |
|
|
Very crude reformatting of your code, but should make things much clearer. Look at the calculate() function, and the second-to-last if statement in it. It shows what I mean with the term 'recursion'. 'this' refers to the class itself (the class calls functions or variables from itself when using this). In the main, all that has to be done is create a 'new' instance of the Calculator() class, and then call the function I want to use. Object oriented programming really makes your life a lot easier, as shown below.
| Code: | // Calculator+, by James Fisher
// Include the iostream library
#include <iostream>
//Use the standard namespace
using namespace std;
class Calculator {
public:
// Constructor of the class
Calculator();
// Function which will perform the calculations
void calculate();
private:
// Private variables required only in the calculator class
float Number_1;
float Number_2;
float Result;
float Restart;
int Which_Calculation;
};
Calculator::Calculator() {}
void Calculator::calculate() {
// Give instructions
cout << "Choose a task. Press 1 to add, 2 to subtract, 3 to multiply, and 4 to divide." << endl;
cin >> this->Which_Calculation;
// Get numbers
cout << "Please enter the first number." << endl;
cin >> this->Number_1;
cout << "Please enter the second number." << endl;
cin >> this->Number_2;
switch(this->Which_Calculation) {
case 1:
this->Result = this->Number_1 + this->Number_2;
break;
case 2:
this->Result = this->Number_1 - this->Number_2;
break;
case 3:
this->Result = this->Number_1 * this->Number_2;
break;
case 4:
this->Result = this->Number_1 / this->Number_2;
break;
}
// Print the answer is...
cout << "The answer is..." << endl;
// Print the result
cout << this->Result << endl;
// Ask if the user would like to do another problem
cout << "Would you like to do another problem?." << endl;
cin >> this->Restart;
// Recursive action
if (this->Restart == 1)
this->calculate();
// Terminate the program
if (this->Restart == 2)
cout << "Goodbye. Hit any key to exit." << endl;
}
void main ()
{
Calculator* calc = new Calculator();
calc->calculate();
} |
|
|
| Back to top |
|
 |
Aviar³ Grandmaster Cheater
Reputation: 50
Joined: 03 Jan 2008 Posts: 655 Location: Canada
|
Posted: Tue Mar 01, 2011 12:20 pm Post subject: |
|
|
It's sexy because he did it right.
_________________
This is the inception of deception, checking the depth of your perception.
 |
|
| Back to top |
|
 |
DeletedUser14087 I post too much
Reputation: 2
Joined: 21 Jun 2006 Posts: 3069
|
Posted: Tue Mar 01, 2011 12:22 pm Post subject: |
|
|
| The Fish wrote: | | Noz3001 wrote: | | The Fish wrote: | | Aviar³ wrote: | Haven't declared Restart as a variable or function or pretty much anything. Error is on line 66 or 65.
P.S.: Why the fuck are you using goto? |
Couldn't figure out how to do a loop.
As stated yesterday, I don't have a book or anything. |
Go out and buy one before you become a terrible programmer and make everyone else's life difficult |
I'm dead broke. |
For the love of god, ever heard of Internet ? eBooks ? Warez ? omg
|
|
| Back to top |
|
 |
The Fish Expert Cheater
Reputation: 3
Joined: 11 Aug 2008 Posts: 114 Location: Ohio bitch
|
Posted: Tue Mar 01, 2011 12:45 pm Post subject: |
|
|
| Augustine wrote: | Very crude reformatting of your code, but should make things much clearer. Look at the calculate() function, and the second-to-last if statement in it. It shows what I mean with the term 'recursion'. 'this' refers to the class itself (the class calls functions or variables from itself when using this). In the main, all that has to be done is create a 'new' instance of the Calculator() class, and then call the function I want to use. Object oriented programming really makes your life a lot easier, as shown below.
| Code: | // Calculator+, by James Fisher
// Include the iostream library
#include <iostream>
//Use the standard namespace
using namespace std;
class Calculator {
public:
// Constructor of the class
Calculator();
// Function which will perform the calculations
void calculate();
private:
// Private variables required only in the calculator class
float Number_1;
float Number_2;
float Result;
float Restart;
int Which_Calculation;
};
Calculator::Calculator() {}
void Calculator::calculate() {
// Give instructions
cout << "Choose a task. Press 1 to add, 2 to subtract, 3 to multiply, and 4 to divide." << endl;
cin >> this->Which_Calculation;
// Get numbers
cout << "Please enter the first number." << endl;
cin >> this->Number_1;
cout << "Please enter the second number." << endl;
cin >> this->Number_2;
switch(this->Which_Calculation) {
case 1:
this->Result = this->Number_1 + this->Number_2;
break;
case 2:
this->Result = this->Number_1 - this->Number_2;
break;
case 3:
this->Result = this->Number_1 * this->Number_2;
break;
case 4:
this->Result = this->Number_1 / this->Number_2;
break;
}
// Print the answer is...
cout << "The answer is..." << endl;
// Print the result
cout << this->Result << endl;
// Ask if the user would like to do another problem
cout << "Would you like to do another problem?." << endl;
cin >> this->Restart;
// Recursive action
if (this->Restart == 1)
this->calculate();
// Terminate the program
if (this->Restart == 2)
cout << "Goodbye. Hit any key to exit." << endl;
}
void main ()
{
Calculator* calc = new Calculator();
calc->calculate();
} |
|
I'm gonna rip this apart now to see the differences between mine and yours, and to see how yours is better.
Thanks.
_________________
It was nice while it lasted. |
|
| Back to top |
|
 |
Aniblaze Grandmaster Cheater Supreme
Reputation: 138
Joined: 23 Apr 2006 Posts: 1757 Location: The Netherlands
|
Posted: Tue Mar 01, 2011 1:11 pm Post subject: |
|
|
Good luck. If you have any questions, be sure to post them here .
|
|
| Back to top |
|
 |
|