• Advertisement
Sign in to follow this  

C++ help please

This topic is 3195 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm a new C++ student, and I feel retarded. Doing an assignment for class and when i try rebuilding i'm getting syntax error 2059 'constant'. I was doing the rebuilding because I was told it is a good way to check your work. Any help I could get would be much appreciated. Here's what I have so far: //specification: this program calculates the price of pens based on the amount ordered //specification: find errors in this program #include "stdafx.h" #include <iostream> using namespace std; namespace pen { 1 to 50 pen * .50 each 51 to 100pen * .48 each 101 to 499pen * .45 each 500+pen * .42 each; } int main() { //ask the user how many pens they would like to order cout << endl << "How many pens would you like to order?\n"; cin >> myPens; cout << endl << }

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by jimslob

namespace pen {
1 to 50 pen * .50 each
51 to 100pen * .48 each
101 to 499pen * .45 each
500+pen * .42 each;
}

That's not valid C++. Looks more like it's part of the problem description.

Share this post


Link to post
Share on other sites
Ok ... first of all like WanMaster said the stuff that you are doing in the Pen namespace is not valid in C++.
Other errors : the myPens variable is not declared anywhere.
This line will not compile : cout << endl << .
main() needs to return a value.

#include <iostream>

using namespace std;

int main()
{
int myPens = 0;
double price = 0;

cout <<"How many pens would you like to order : ";
cin >> myPens;

if(myPens <= 0)
price = 0;

else if(myPens >= 1 && myPens <= 50)
price = myPens * 0.50;

else if(myPens >= 51 && myPens <= 100)
price = myPens * 0.48;

else if(myPens >= 101 && myPens <= 499)
price = myPens * 0.45;

else
price = myPens * 0.42;

cout << endl << "The total price is : " << price << endl;

return 0;
}

Share this post


Link to post
Share on other sites
Quote:
Original post by WanMaster
Quote:
Original post by jimslob

namespace pen {
1 to 50 pen * .50 each
51 to 100pen * .48 each
101 to 499pen * .45 each
500+pen * .42 each;
}

That's not valid C++. Looks more like it's part of the problem description.


Not sure what would compel him to do that. Maybe he was use to a different
language(maybe VB).

Share this post


Link to post
Share on other sites
CirdanValen : We really shouldn't be doing his homework for him. :P

Yeah , I know ... my bad . The namespace thing is really , really wrong ... so I thought I'd help the guy;

Perhaps he had been coding in another language which allowed this stuff ...

Share this post


Link to post
Share on other sites
Quote:
Original post by ArthY303
main() needs to return a value.

It will return 0 by default.

Quote:
Original post by tnutty
Maybe he was use to a different language(maybe VB).

Perhaps, but it certainly isn't valid VB either, and to be honest I can't think of any other common language where this syntax would fly.


I suggest to OP to learn some basic C++ first before starting work on this assignment. You indicated it was home work, so I presume you been to some classes or have at least access to learning material.

Share this post


Link to post
Share on other sites
Quote:
Original post by ArthY303
Quote:
Original post by ArthY303
main() needs to return a value.

WanMaster : It will return 0 by default.

I know but most people explicitly return 0 at the end of the main() function
Your statement still wasn't correct and in for beginners we try not to let incorrect statements stand. But I agree with you and tend to have 'return 0;' at the end of my main function because I happen to like consistency rather than coding to an exception just because it exists.

Share this post


Link to post
Share on other sites
It might return zero on its own but its better to control what is return and its good to follow syntax

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement