Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


Help with easy code, c++

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
Travis13
Expert Cheater
Reputation: 0

Joined: 17 Feb 2007
Posts: 199

PostPosted: Sun Dec 19, 2010 3:39 pm    Post subject: Help with easy code, c++ Reply with quote

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 Razz
Back to top
View user's profile Send private message MSN Messenger
Jesper
Grandmaster Cheater Supreme
Reputation: 9

Joined: 21 Feb 2007
Posts: 1156

PostPosted: Sun Dec 19, 2010 3:50 pm    Post subject: Reply with quote

I may be wrong but try removing the ; behind main().
Back to top
View user's profile Send private message
M.CORP
Grandmaster Cheater Supreme
Reputation: 28

Joined: 28 Oct 2009
Posts: 1010

PostPosted: Sun Dec 19, 2010 5:13 pm    Post subject: Reply with quote

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();
}

_________________
Shameless Self Advertising Very Happy!
Steam
Just lurking around...
Back to top
View user's profile Send private message
Slugsnack
Grandmaster Cheater Supreme
Reputation: 71

Joined: 24 Jan 2007
Posts: 1857

PostPosted: Sun Dec 19, 2010 5:25 pm    Post subject: Reply with quote

Your calculate function is failing to return the int it promises to..
Back to top
View user's profile Send private message
M.CORP
Grandmaster Cheater Supreme
Reputation: 28

Joined: 28 Oct 2009
Posts: 1010

PostPosted: Sun Dec 19, 2010 6:26 pm    Post subject: Reply with quote

Slugsnack wrote:
Your calculate function is failing to return the int it promises to..

Add return(a); to return the value?

_________________
Shameless Self Advertising Very Happy!
Steam
Just lurking around...
Back to top
View user's profile Send private message
Travis13
Expert Cheater
Reputation: 0

Joined: 17 Feb 2007
Posts: 199

PostPosted: Sun Dec 19, 2010 9:11 pm    Post subject: Reply with quote

+=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 Razz
Back to top
View user's profile Send private message MSN Messenger
hcavolsdsadgadsg
I'm a spammer
Reputation: 26

Joined: 11 Jun 2007
Posts: 5801

PostPosted: Sun Dec 19, 2010 11:52 pm    Post subject: Reply with quote

why wouldn't functions be allowed in main()?

recursive main() is what is not allowed.
Back to top
View user's profile Send private message
Slugsnack
Grandmaster Cheater Supreme
Reputation: 71

Joined: 24 Jan 2007
Posts: 1857

PostPosted: Mon Dec 20, 2010 7:09 am    Post subject: Reply with quote

+=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
View user's profile Send private message
AtheistCrusader
Grandmaster Cheater
Reputation: 6

Joined: 23 Sep 2006
Posts: 681

PostPosted: Mon Dec 20, 2010 9:07 am    Post subject: Reply with quote

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
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites