Sign in to follow this  
jimslob

C++ help please

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
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
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
Quote:
Original post by owiley
It might return zero on its own but its better to control what is return and its good to follow syntax
Just to carify: It will (as opposed to "might") return zero on its own if you omit the return statement, so says the standard.
I prefer not to explicitly return if I don't have to, main isn't exclusive to that mantra either:

void add_four(int & x)
{
x += 4;
return; // not needed
}

int main()
{
int x = 5;
add_four(x);
return 0; // not needed
}
It is merely incidental that the return type of add_four is a void and the return type of main is an int.

Share this post


Link to post
Share on other sites
Quote:
Original post by jimslob
I'm a new C++ student, and I feel retarded.
Don't worry, C++ is a hard language, it's as simple as that.

Quote:

1 to 50 pen * .50 each
51 to 100pen * .48 each
101 to 499pen * .45 each
500+pen * .42 each;
As pointed out already, this bit just isn't C++ code at all, the compiler will get confused and spew a ton of errors your way.

Quote:
cin >> myPens;
You can't introduce variables this way, you need to explicitly declare them before you can use them, presumably you want myPens to be an integer, so you'll need:

int myPens;

... somewhere prior to the above quoted line.

Quote:
cout << endl <<
This line isn't complete yet, the compiler won't just ignore it and it certainly won't like it.

Share this post


Link to post
Share on other sites
As it obviously states in the top "//specification: find errors in this program", that's most likely the homework assignment. The code was probably put in the exact way it's showed and he's supposed to find the errors.

When he obviously states that its a homework assignment and the only thing he's tried to do it build the project.... you should stop and let him try first.

Share this post


Link to post
Share on other sites
Quote:
Original post by dmatter
Quote:
Original post by owiley
It might return zero on its own but its better to control what is return and its good to follow syntax
Just to carify: It will (as opposed to "might") return zero on its own if you omit the return statement, so says the standard.
I prefer not to explicitly return if I don't have to, main isn't exclusive to that mantra either:

void add_four(int & x)
{
x += 4;
return; // not needed
}

int main()
{
int x = 5;
add_four(x);
return 0; // not needed
}
It is merely incidental that the return type of add_four is a void and the return type of main is an int.




I thought main would return some other number if it failed in some instance,no?

Share this post


Link to post
Share on other sites
Quote:
Original post by tnutty
I thought main would return some other number if it failed in some instance,no?
If the program terminates before reaching the end of main then it can return an appropriate signal to the host environment although I believe it is entirely implementation dependant as to exactly what signal gets returned (if any). There are some standard signal macros (see <csignal>) with implementation defined values. In any case, these values aren't actually being returned by main itself.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this