Solved

Error when compiling

Posted on 2004-04-20
16
587 Views
Last Modified: 2010-07-27
leflon, are yaw there?
Below is my simple program.  I'm new and learing c++.  I can't find my error in this.  When I try to compile it, I receive this error:
: error C2239: unexpected token '{' following declaration of 'main'
Error executing cl.exe.
Any suggestions?


int main
{
  int Pennies = 0;
  int OrgPennies = 0;  // for storing the original amount of pennies.

  // This program will calculate any amount of Pennies into: $100's, $50's, $20's, $10's , $5's, 1's, Quarters, Dimes, Nickles and the remaining pennies.
  cin >> Pennies;
  // Possible error handling.
  OrgPennies = Pennies;

  // Splitting up the money
  int Bill100 = Pennies/10000;
  Pennies = Pennies%10000;

  int Bill50 = Pennies/5000;
  Pennies = Pennies%5000;

  int Bill20 = Pennies/2000;
  Pennies = Pennies%2000;

  int Bill10 = Pennies/1000;
  Pennies = Pennies%1000;

  int Bill5 = Pennies/500;
  Pennies = Pennies%1000;

  int Bill1 = Pennies/100;
  Pennies = Pennies%100;

  int Coin25 = Pennies/25;
  Pennies = Pennies%25;

  int Coin10 = Pennies/10;
  Pennies = Pennies%10;

  int Coin5 = Pennies/5;
  Pennies = Pennies%5;

  int Coin1 = Pennies/1;
  Pennies = Pennies%1;

  cout << "For " << OrgPennies << " You will get:" << endl;

  cout << Bill100 << "One Hundred dollar bills" << endl;

  cout << Bill50 << "Fifty dollar bills" << endl;

  cout << Bill20 << "Twenty dollar bills" << endl;

  cout << Bill10 << "Ten dollar bills" << endl;

  cout << Bill5 << "Five dollar bills" << endl;

  cout << Bill1 << "One dollar bills" << endl;

  cout << Coin25 << "Quarters" << endl;

  cout << Coin10 << "Dimes" << endl;

  cout << Coin5 << "Nickels" << endl;

  cout << Coin1 << "Pennies" << endl;

  cout << Pennies << "pennies" << endl;

  return 0;  // program end
}
0
Comment
Question by:super_man_1976
  • 6
  • 5
  • 4
  • +1
16 Comments
 
LVL 10

Assisted Solution

by:Sys_Prog
Sys_Prog earned 20 total points
Comment Utility
() is missing after main
it should be
int main ()

amit
0
 
LVL 22

Assisted Solution

by:grg99
grg99 earned 20 total points
Comment Utility
main is passed two arguments, so should be declared as "int main( int argc, char * argv[] )"



0
 
LVL 10

Expert Comment

by:Sys_Prog
Comment Utility
>>  main is passed two arguments, so should be declared as "int main( int argc, char * argv[] )"

Where are the two arguments required in his code..........This is required only if u plan to process/use command line parameters

Amit
0
 

Author Comment

by:super_man_1976
Comment Utility
I just tryied both of those sugestions and unfortuinally I still get errors
If I try just the int main () I get 15 error(s), 1 warning(s) Most of the errors said "illegal, right operand has type 'char"
when I tried "int main( int argc, char * argv[] )" I get the same errors as below:

error C2065: 'cin' : undeclared identifier
warning C4552: '>>' : operator has no effect; expected operator with side-effect
(42) : error C2065: 'cout' : undeclared identifier
(42) : error C2297: '<<' : illegal, right operand has type 'char [5]'
(42) : error C2065: 'endl' : undeclared identifier
(44) : error C2297: '<<' : illegal, right operand has type 'char [25]'
(46) : error C2297: '<<' : illegal, right operand has type 'char [19]'
(48) : error C2297: '<<' : illegal, right operand has type 'char [20]'
(50) : error C2297: '<<' : illegal, right operand has type 'char [17]'
(52) : error C2297: '<<' : illegal, right operand has type 'char [18]'
(54) : error C2297: '<<' : illegal, right operand has type 'char [17]'
(56) : error C2297: '<<' : illegal, right operand has type 'char [9]'
(58) : error C2297: '<<' : illegal, right operand has type 'char [6]'
(60) : error C2297: '<<' : illegal, right operand has type 'char [8]'
(62) : error C2297: '<<' : illegal, right operand has type 'char [8]'
(64) : error C2297: '<<' : illegal, right operand has type 'char [8]'
Error executing cl.exe.

15 error(s), 1 warning(s)
0
 
LVL 10

Expert Comment

by:Sys_Prog
Comment Utility
Inlcude

#include <iostream> in your program

Also,
EITHER include the foloowing statement at top after includes
using namespace std;
OR
replace cout and cin with std::cout and std::cin respectively

Amit
0
 

Author Comment

by:super_man_1976
Comment Utility
Where does that go? At the very top like this?

#include <iostream>
int main ()

0
 
LVL 10

Expert Comment

by:Sys_Prog
Comment Utility
Your code should be

#include <iostream>

using namespace std ;

int main () {
      // Your code
}


Amit
0
 
LVL 7

Accepted Solution

by:
leflon earned 20 total points
Comment Utility
Hi super_man_1976,

yes exactly that way, but don't forget the using namespace std;

#include <iostream>
using namespace std;
int main/int argc, char* argv[])
{
...// your prog (with the return 0; at the end
}

leflon
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 7

Expert Comment

by:leflon
Comment Utility
super_man_1976,

oops, typo must be
int main(int argc, char* argv[])

1.) if you use a function or object (datatype, etc.) in C++ you have to eventually (mainly most of the times) include a so called header file at the beginnig of the cpp file you use the function in.
as you use cout here you have to include the proper file iostream (the <> tells the compiler to search for this file in the specified input-file dirs).

2.) the vars argc and argv after main tell you the number (argc) and the text (argv, array of strings) of the parameters you pass to the prog on the command line.
so a call of:
  prog.exe first second third
will give you
  argc = 4
  argv[0] = prog.exe
  argv[1] = first
  argv[2] = second
  argv[3] = third

as you are new to c++ let me ask you:
do you already have a book which helps you on your way to learning and understanding the language?
if not we sure can make a few helpful recommandations. :-)

cheers
leflon
0
 
LVL 7

Expert Comment

by:leflon
Comment Utility
super_man_1976,

"Thinking in C++" is a good (and free) book, especially for people you want to learn the language, you may want to take a look at

http://mindview.net/Books/TICPP/ThinkingInCPP2e.html

leflon
0
 

Author Comment

by:super_man_1976
Comment Utility
Thanks a whole bunch leflon for all the help the last few days.  I'll be working on my prog tonight and try what you recommended.  The only book I have is Sam's Teach your self C++ in 21 days, but I haven't really done a whole lot with it yet.  Thanks for the link above, I'll check it out.  There are so many books to choose from.  Anyway, I'll post back tonight.

0
 
LVL 7

Expert Comment

by:leflon
Comment Utility
no prob, glad i can be of any help!
take your time (to read one or two of the many books) and dont't hesitate to ask if you need further help.
and you better reward the other guys who helped you in this thread, so they come back and do so again ;)

leflon
0
 

Author Comment

by:super_man_1976
Comment Utility
Thanks for the EE advice, as I'm new to it.  This has got to be the best darn resource for informatin that I've ever been to!  
Be back soon.
0
 

Author Comment

by:super_man_1976
Comment Utility
leflon,
My program actually works!  There is just one more thing I'm struggling with.  When I first execute the program it just has the blinking curser.  I want to have the question displayed "How many pennies do you have?”  I can't seem to get it right.  Below is the up to date code for my simple prog.  Man, this is has been a great learning experience!  By the way that book look nice, thanks.


#include <iostream>
using namespace std;
int main(int argc, char* argv[])
{
  int Pennies = 0;
  int OrgPennies = 0;  // For storing the original amount of pennies.

  /* This program will calculate any amount of Pennies into:
                                                                                          $100's
                                                                                          $50's
                                                                                          $20's
                                                                                          $10's
                                                                                          $5's
                                                                                          $1's
                                                                                          Quarters
                                                                                          Dimes
                                                                                          Nickles
                                                                                          Remaining pennies */
  cin >> Pennies;
  // Possible error handling.
  OrgPennies = Pennies;

  // Splitting up the money
  int Bill100 = Pennies/10000;
  Pennies = Pennies%10000;

  int Bill50 = Pennies/5000;
  Pennies = Pennies%5000;

  int Bill20 = Pennies/2000;
  Pennies = Pennies%2000;

  int Bill10 = Pennies/1000;
  Pennies = Pennies%1000;

  int Bill5 = Pennies/500;
  Pennies = Pennies%1000;

  int Bill1 = Pennies/100;
  Pennies = Pennies%100;

  int Coin25 = Pennies/25;
  Pennies = Pennies%25;

  int Coin10 = Pennies/10;
  Pennies = Pennies%10;

  int Coin5 = Pennies/5;
  Pennies = Pennies%5;

  int Coin1 = Pennies/1;
  Pennies = Pennies%1;

  cout << "For " << OrgPennies << " pennies you will receive:" << endl;

  cout << Bill100 << " One Hundred dollar bills " << endl;

  cout << Bill50 << " Fifty dollar bills " << endl;

  cout << Bill20 << " Twenty dollar bills " << endl;

  cout << Bill10 << " Ten dollar bills " << endl;

  cout << Bill5 << " Five dollar bills " << endl;

  cout << Bill1 << " One dollar bills " << endl;

  cout << Coin25 << " Quarters " << endl;

  cout << Coin10 << " Dimes " << endl;

  cout << Coin5 << " Nickels " << endl;

  cout << Coin1 << " Pennies " << endl;

  return 0;  // program end
}
0
 
LVL 7

Expert Comment

by:leflon
Comment Utility
hi

just take a look at one of your earlier questions. you hab this initial output (describing the use of your prog) in one of your posts there.
ckeck:
http:Q_20955223.html#10845828

just enter the same two cout statements in front of your cin >> Pennies, line

leflon
0
 

Author Comment

by:super_man_1976
Comment Utility
O yeah, that make obvious sense.  Thanks again teach!


0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now