View previous topic :: View next topic |
Author |
Message |
Travis13 Expert Cheater
Reputation: 0
Joined: 17 Feb 2007 Posts: 199
|
Posted: Sun Dec 19, 2010 3:39 pm Post subject: Help with easy code, c++ |
|
|
Hey guys I'm just starting c++ again as I taught myself a bit before. Anyways, I just wanted to test my memory and see if I could write something small first and I wrote this but it won't compile saying "expected ',' or ';' before '{' in line 9 (around the int main(); line)...here is the code, thanks in advance
edit: i KNOW you're not supposed to use SYSTEM anything, it's just for personal trial and error coding tho, nothing released
Code: |
#include <iostream>
using namespace std;
double a;
double b;
int choice;
int main();
{
goto calculator();
double calculator();
{
cout << "Enter a number" << endl;
cin >> a;
cout << "Enter another number" << endl;
cin >> b;
cout << "The sum of the two numbers is: " << (a + b) << endl;
cout << "Try again? y or n" << endl;
cin >> choice;
}
switch (choice);
{
case 'y': goto calculator();
break;
case 'n': system("PAUSE");
break;
}
}
|
_________________
Learning C++, trying, failing, never gonna give up tho  |
|
Back to top |
|
 |
Jesper Grandmaster Cheater Supreme
Reputation: 9
Joined: 21 Feb 2007 Posts: 1156
|
Posted: Sun Dec 19, 2010 3:50 pm Post subject: |
|
|
I may be wrong but try removing the ; behind main().
|
|
Back to top |
|
 |
M.CORP Grandmaster Cheater Supreme
Reputation: 28
Joined: 28 Oct 2009 Posts: 1010
|
Posted: Sun Dec 19, 2010 5:13 pm Post subject: |
|
|
Why is a function inside the int main?
I think you are suppose to place it on top of the code first since you made 'calculator' a function then remove goto calculator.
EDIT:
Done, fixed your code:
Code: | #include <iostream>
using namespace std;
int calculator(){
double a;
double b;
int choice;
cout << "Enter a number" << endl;
cin >> a;
cout << "Enter another number" << endl;
cin >> b;
cout << "The sum of the two numbers is: " << (a + b) << endl;
cout << "Try again? y or n" << endl;
cin >> choice;
system("PAUSE");
switch (choice)
{
case 'y': calculator();
break;
case 'n': system("PAUSE");
break;
default:
system("PAUSE");
break;
}
}
int main()
{
calculator();
} |
_________________
|
|
Back to top |
|
 |
Slugsnack Grandmaster Cheater Supreme
Reputation: 71
Joined: 24 Jan 2007 Posts: 1857
|
Posted: Sun Dec 19, 2010 5:25 pm Post subject: |
|
|
Your calculate function is failing to return the int it promises to..
|
|
Back to top |
|
 |
M.CORP Grandmaster Cheater Supreme
Reputation: 28
Joined: 28 Oct 2009 Posts: 1010
|
Posted: Sun Dec 19, 2010 6:26 pm Post subject: |
|
|
Slugsnack wrote: | Your calculate function is failing to return the int it promises to.. |
Add return(a); to return the value?
_________________
|
|
Back to top |
|
 |
Travis13 Expert Cheater
Reputation: 0
Joined: 17 Feb 2007 Posts: 199
|
Posted: Sun Dec 19, 2010 9:11 pm Post subject: |
|
|
+=Marvin=+ wrote: | Why is a function inside the int main?
I think you are suppose to place it on top of the code first since you made 'calculator' a function then remove goto calculator.
EDIT:
Done, fixed your code:
Code: | #include <iostream>
using namespace std;
int calculator(){
double a;
double b;
int choice;
cout << "Enter a number" << endl;
cin >> a;
cout << "Enter another number" << endl;
cin >> b;
cout << "The sum of the two numbers is: " << (a + b) << endl;
cout << "Try again? y or n" << endl;
cin >> choice;
system("PAUSE");
switch (choice)
{
case 'y': calculator();
break;
case 'n': system("PAUSE");
break;
default:
system("PAUSE");
break;
}
}
int main()
{
calculator();
} |
|
Thanks, your code worked, I don't think your supposed to put functions inside main. Except the switch statement doesn't work as it just closes no matter what you write
_________________
Learning C++, trying, failing, never gonna give up tho  |
|
Back to top |
|
 |
hcavolsdsadgadsg I'm a spammer
Reputation: 26
Joined: 11 Jun 2007 Posts: 5801
|
Posted: Sun Dec 19, 2010 11:52 pm Post subject: |
|
|
why wouldn't functions be allowed in main()?
recursive main() is what is not allowed.
|
|
Back to top |
|
 |
Slugsnack Grandmaster Cheater Supreme
Reputation: 71
Joined: 24 Jan 2007 Posts: 1857
|
Posted: Mon Dec 20, 2010 7:09 am Post subject: |
|
|
+=Marvin=+ wrote: | Slugsnack wrote: | Your calculate function is failing to return the int it promises to.. |
Add return(a); to return the value? |
Don't need to put a in brackets. 'return a;' would be fine. Might need to cast it to int before that though since it's double. However since you never use the return, you may as well change it to a void function.
|
|
Back to top |
|
 |
AtheistCrusader Grandmaster Cheater
Reputation: 6
Joined: 23 Sep 2006 Posts: 681
|
Posted: Mon Dec 20, 2010 9:07 am Post subject: |
|
|
Hmm, instead of int a,int b you should int a =0;int b=0; Good coding practice
Also, cin first into a string, check if number, then copy to int.
|
|
Back to top |
|
 |
|