Solved
When I try to compile this program i keep getting these errors:
(50) : error C2059: syntax error :
‘<=’(50) : error C2143: syntax error
: missing ‘;’ before ‘{‘(51) : error
C2059: syntax error : ‘>’(51) : error
C2143: syntax error : missing ‘;’
before ‘{‘(62) : error C2059: syntax
error : ‘else’(62) : error C2143:
syntax error : missing ‘;’ before ‘{‘
#include <iostream>
#include <string>
#include <cassert>
using namespace std;
class income {
private:
double incm;
double subtract;
double taxRate;
double add;
char status;
public:
void setStatus ( char stats ) { status = stats; }
void setIncm (double in ) { incm = in; }
void setSubtract ( double sub ) { subtract = sub; }
void setTaxRate ( double rate ) { taxRate = rate; }
void setAdd ( double Add ) { add = Add; }
char getStatus () { return status; }
double getIncm () { return incm; }
double getsubtract () { return subtract; }
double getTaxRate () { return taxRate; }
double getAdd () { return add; }
void calcIncome ();
};
//calcIncome
int main () {
income _new;
double ajIncome = 0, _incm = 0;
char status = ' ';
bool done = false;
while ( !done ) {
cout << "Please enter your TAXABLE INCOME:\n" << endl;
cin >> _incm;
if(cin.fail()) { cin.clear(); }
if ( _incm <= 0) { cout << "the income must be greater than 0... \n" << endl; }
if ( _incm > 0) { done = true; _new.setIncm(_incm); }
}
done = false;
char stt [2] = " ";
while ( !done ) {
cout << "Please declare weather you are filing taxes jointly or single" << "\n";
cout << "\t's' = single\n\t'm' = married" << endl;
cin >> stt;
if(cin.fail()) { cin.clear(); }
if ( status == 's' || status == 'm' ) { done = true; _new.setStatus(stt[0]); }
//if else { }
}
return 0;
};
This is part of a homework assignment so any pointers on bettering my programing would be **great**
Note:I am using Windows 7 with VS express C++ 2008
asked Jan 13, 2010 at 15:44
WallterWallter
4,2756 gold badges29 silver badges33 bronze badges
3
income
is the name of your class. _incm
is the name of your variable. Perhaps you meant this (notice the use of _incm
not income
):
if (_incm <= 0) { cout << "the income must be greater than 0... \n" << endl; }
if (_incm > 0) { done = true; _new.setIncm(_incm); }
Frequently you use CamelCase for class names and lowercase for instance variable names. Since C++ is case-sensitive, they wouldn’t conflict each other if they use different case.
answered Jan 13, 2010 at 15:47
Jon-EricJon-Eric
17k9 gold badges65 silver badges97 bronze badges
Your variable is named incom
, not income
. income
refers to a type, so the compiler gets confused and you get a syntax error when you try to compare that type against a value in line 50.
One note for bettering you programming would be to use more distinct variable names to avoid such confusions…
answered Jan 13, 2010 at 15:46
sthsth
223k53 gold badges283 silver badges367 bronze badges
1
Search code, repositories, users, issues, pull requests…
Provide feedback
Saved searches
Use saved searches to filter your results more quickly
Sign up
Hey;
I’m kinda new to C++, but I’m getting a syntax error for my return statement in my function. Here’s my code:
// Physics Problem Solver.cpp : Defines the entry point for the console application.
//
#include «stdafx.h»
#include <iostream>
#include <stdlib.h>
#include <windows.h>
#include <conio.h>
#include <string.h>
#include <sstream>
void welcome();
int mainMenu();
void kinematics();
void dynamics();
void cmwe();
void motionAndWaves();
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
welcome();
return 0;
}
//===========================================================================================================================================================================================================
//Functions
//===========================================================================================================================================================================================================
void welcome()
{
cout << «Welcome to the Physics 20 Problem Solver. Please select your unit:» << endl;
int menuSelection = mainMenu();
system(«PAUSE»);
}
//===========================================================================================================================================================================================================
int mainMenu()
{
int menuSelection = 1;
char input = _getch();
if (int(input) = 31)
menuSelection++;
if (int(input) = 30)
menuSelection—;
if (menuSelection < 1)
menuSelection = 1;
if (menuSelection > 4)
menuSelection = 4;
do
{
switch(menuSelection)
{
case 1:
cout << «KINEMATICS\nDynamics\nCircular Motion, Work, and Energy\nOscillary Motion and Mechanical Waves» << endl;
break;
case 2:
cout << «Kinematics\nDYNAMICS\nCircular Motion, Work, and Energy\nOscillary Motion and Mechanical Waves» << endl;
break;
case 3:
cout << «Kinematics\nDynamics\nCIRCULAR MOTION, WORK, AND ENERGY\nOscillar Motion and Mechanical Waves» << endl;
break;
case 4:
cout << «Kinematics\nDynamics\nCircular Motion, Work, and Energy\nOSCILLAR MOTION AND MECHANICAL WAVES» << endl;
break;
}
while (int(input) != 13);
}
return menuSelection;
}
I don’t know why I’m getting this error, it’s the only return statement there. When I comment out the return line, I get a syntax error on my last curly brace. What’s wrong with the damn curly brace…I really don’t know. Been sitting here staring at it for an hour, this is the part of programming I hate.
- Forum
- Beginners
- error C2059: syntax error : ‘return’
error C2059: syntax error : ‘return’
Hey guys, I’m relatively new to c++ , and I’m having trouble with my program I’m writing. It’s supposed to ask user input for squares and rectangles, but it’s not compiling right. Any kind of help or hints would be very much appreciated. Thanks in advance.
|
|
The errors I am getting are:
error C2059: syntax error : ‘return’
error C2059: syntax error : ‘return’
error C2059: syntax error : ‘return’
I’m not sure if I formatted the code right since it’s my first time posting here, I will fix it if it didn’t.
You never return anything in any of your functions. For example —
|
|
You want to return the menu choice right? Ofc you do! You then have to return it like this —
return menuChoice;
Then in main —
|
|
If say, the user chooses menu option 2. menuChoice will be equal to 2. Which works fine
Oops, I forgot to type those in. I was in the middle of trying to figure it out.
I did however get the program to work. Just needed to get rid of the do {}, it was unnecessary.
Thanks for the help!
Glad it worked out
Topic archived. No new replies allowed.
Recommended Answers
Your signatures are inconsistant. Once you fix there will be other problems…
void writeNode(NodeType* node);
void writeNode(NodeType node)
Jump to Post
What is eTree??
void Expression::display() const { writeNode(eTree*); //error here }
Jump to Post
do you want to pass eTree * or perhaps &eTree
Jump to Post
All 16 Replies
Edited
by template<> because:
n/a
oh shoot, sorry
//expression.h
#ifndef EXPRESSION_H
#define EXPRESSION_H
namespace Student
{
enum TagType //For labeling data content in a binary expression tree node.
{
INT_ONLY,
SUB_NODE
};
struct NodeType //For nodes in a binary expression tree.
{
TagType tag;
union //Note that this union type is "anonymous".
{
int intValue;
struct //And this struct type is also anonymous.
{
NodeType* left;
char op;
NodeType* right;
};
};
};
class Expression
{
public:
bool Expression::isRead
(
istream& inStream //inout
);
/**<
Determines if a valid expression can be read from inStream and, if so,
reads and stores the expression.
@param inStream The input stream from which the expression is read.
@return true if an expression has been successfully read from inStream,
otherwise false.
@pre inStream has been initiallzed and is open for reading.
@post inStream is still open for reading. An expression may or may not
have been successfully read.
*/
void display() const;
/**<
Displays an arithmetic expression. If the expression is a single
non-negative integer, it simply displays that integer. Otherwise
it displays the expression fully parenthesized, with a single
blank space before, and a single blank space after, each operator.
@pre This expression has been initialized.
@post This expression has been displayed, as above, starting from
the current cursor position. Note too that this method does not
terminate the line on which the expression is displayed.
*/
int getValue() const;
/**<
Returns the value of this expression.
*/
private:
NodeType eTree;
};
} //End of this part of namespace Student
#endif
also, the isRead function is taken care of by my profs .obj file. So I don’t have a written function for that
Edited
by bailsb because:
n/a
Your signatures are inconsistant. Once you fix there will be other problems…
void writeNode(NodeType* node);
void writeNode(NodeType node)
Edited
by template<> because:
n/a
ok, that has been fixed, but I’m still getting the same errors. The new code
#include<iostream>
using namespace std;
#include "expression.h"
namespace Student
{
void writeNode(NodeType* node);
/*
Displays an arithmetic expression.
@pre The node is initialized.
@post This node has been displayed.
*/
int answer(NodeType* node);
/*
returns the value of the SUB_NODE, or the value
if node is just an int.
*/
void Expression::display() const
{
writeNode(eTree*); //error here
}
int Expression::getValue() const
{
return answer(eTree*); //error here
}
void writeNode(NodeType* node)
{
if(node->tag == SUB_NODE)
{
cout<<"(";
writeNode(node->left);
cout<< node->op;
writeNode(node->right);
cout<<")";
}
else if(node->tag == INT_ONLY)
cout<<node->intValue;
}
int answer(NodeType* node)
{
if(node->tag == INT_ONLY)
return node->intValue;
else
{
switch(node->op)
{
case '+':
return answer(node->left) + answer(node->right);
case '-':
return answer(node->left) - answer(node->right);
case '*':
return answer(node->left) * answer(node->right);
case '/':
return answer(node->left) / answer(node->right);
}
}
}
}
Edited
by bailsb because:
n/a
What is eTree??
void Expression::display() const
{
writeNode(eTree*); //error here
}
Edited
by mike_2000_17 because:
Fixed formatting
it is the head node of the expression tree.
do you want to pass eTree * or perhaps &eTree
^^yes actually I do, but if i change that eTree is a const NodeType* while each recursive call will simply be NodeType*
try this and see what happens, then figure out how it should be fixed
struct node_t
{
node_t *left;
node_t *right;
};
void something(node_t *)
{
}
int main()
{
node_t node;
something(node*); // #1 what is this??
something(&node); // #2 pass a pointer to node
}
Edited
by template<> because:
n/a
Taywin
312
Posting Virtuoso
Line 35 in class Expression, you are missing semicolon at the end.
istream& inStream //inout
Edited
by Taywin because:
n/a
thats a parameter for the function
Also, just out of curiosity, why did you decide to use an anonymous struct below?
struct NodeType //For nodes in a binary expression tree.
{
TagType tag;
union //Note that this union type is "anonymous".
{
int intValue;
struct //And this struct type is also anonymous.
{
NodeType* left;
char op;
NodeType* right;
};
};
};
Edited
by template<> because:
n/a
that part was done by my prof. In all honesty, I’m not quite sure
Edited
by bailsb because:
n/a
If eTree is a NodeType you have to call function(NodeType* node)
like so: function(&eTree);
Ask him why when you get a chance, probably has a good reason, although anonymous struct is not part of the c++ standard to the best of my knowledge.
Will do, I have found a way that works. Thanks for your help. In case your curious this is my new code
#include<iostream>
using namespace std;
#include "expression.h"
namespace Student
{
void writeNode(const NodeType* node);
/*
Displays an arithmetic expression.
@pre The node is initialized.
@post This node has been displayed.
*/
int answer(const NodeType* node);
/*
returns the value of the SUB_NODE, or the value
if node is just an int.
*/
void Expression::display() const
{
writeNode(&eTree);
}
int Expression::getValue() const
{
return answer(&eTree);
}
void writeNode(const NodeType* node)
{
if(node->tag == SUB_NODE)
{
cout<<"(";
writeNode(node->left);
cout<< " "<<node->op<<" ";
writeNode(node->right);
cout<<")";
}
else if(node->tag == INT_ONLY)
cout<<node->intValue;
}
int answer(const NodeType* node)
{
if(node->tag == INT_ONLY)
return node->intValue;
else
{
switch(node->op)
{
case '+':
return answer(node->left) + answer(node->right);
case '-':
return answer(node->left) - answer(node->right);
case '*':
return answer(node->left) * answer(node->right);
case '/':
return answer(node->left) / answer(node->right);
}
}
}
}
Now I have the functions with const parameters and NodeType* node
Reply to this topic
Be a part of the DaniWeb community
We’re a friendly, industry-focused community of developers, IT pros, digital marketers,
and technology enthusiasts meeting, networking, learning, and sharing knowledge.