C++ help please
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 <<
}
Quote:Original post by jimslobnamespace 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.
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;
}
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;
}
Quote:Original post by WanMasterQuote:Original post by jimslobnamespace 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).
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 ...
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 ...
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.
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
Quote:Original post by ArthY303Your 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.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
It might return zero on its own but its better to control what is return and its good to follow syntax
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement