What Are the Errors in This C++ Code?

  • Thread starter Demon117
  • Start date
  • Tags
    Code
In summary, the code had several errors that needed to be fixed in order for it to compile and run correctly. These included syntax errors such as using a semi-colon after #include directives and missing semi-colons at the end of statements. There were also errors with undeclared variables and missing function prototypes. The code also had some logical errors, such as using the || operator incorrectly and attempting to return a value from a function declared as void. After fixing these errors, the code was able to compile and run successfully.
  • #1
Demon117
165
1
Code:
#include <iostream>;
using namespace std;
double w = 3.4;
bool p = true;
long foo(int a, int b);
void goo(int &a, double b);
float soo(long a, double b);
bool too(double b);
int boo();
int main()
{
	int c,d;
	c = 4;
	d = c * 3;
	cout << foo(c,d) << endl;
	cout << c << " " << d << endl;
	goo (c,d);
	cout << d << endl;
	cout << soo(c,d) << endl;
	cout << 23 || too(d) << endl;
	for (int i = 0; i < 4; i++) cout << boo();
	cout << endl;
	return 0;
}
/*****************************************************/
long foo(long a, int b)
{
	a = 4;
	w = a * 3.1;
	return a * b;
}
/****************************************************/
void goo(long &a, double b)
{
	a = b * 4 * w;
}
/****************************************************/
float soo(long a, double b)
{
	return b * w * a;
}
/****************************************************/
bool too(double b)
{
	return a == b && true || 0;
}
/****************************************************
	this function should generate a number one higher than the previous time called */
int boo()
{
	static int f;
	f = 0;
	f ++;
	return f;
}
/****************************************************/
void roo (single &a, single b)
{
	single foo; 	foo = a; 	a = b; 	b = foo;
}
/****************************************************
when should you use a static varable vs a global variable?	*/
void loo ()
{
	p = !p;
	return p
}

Can anyone point out the errors? I get confused when I try to read through this and I've been trying to figure it out for a few days now.
 
Technology news on Phys.org
  • #2
I'm going to take a wild one and guess that it's boo() that is your problem, in that it always returns 1 -- because you set "f = 0" on every call. Try this instead: "static int f = 0; f++;"

Or maybe there're other issues? In which case it would be convenient for us if you mentioned them explicitly...
 
  • #3
matumich26 said:
Can anyone point out the errors? I get confused when I try to read through this and I've been trying to figure it out for a few days now.
I tried to compile it and got quite a number of errors. Let's go through them one by one:
Code:
h1.cpp:1:20: warning: extra tokens at end of #include directive

Not supposed to use a semi-colon after #includes, or generally after preprocessor directives.

Code:
matumish1.cpp: In function ‘int main()’:
matumish1.cpp:20: error: invalid operands of types ‘bool’ and ‘<unresolved overloaded function type>’ to binary ‘operator<<’
Code:
cout << 23 || too(d) << endl;

I have no clue what you're trying to do here. However, that expression 23 || too(d) would need to be in parentheses. Regardless, || is a short-circuit or and since 23 will always evaluate to true, the too(d) part will never be run. Can't fix it if I don't know what you want to do.

Code:
matumish1.cpp: In function ‘bool too(double)’:
matumish1.cpp:45: error: ‘a’ was not declared in this scope

This one should be obvious. No variable 'a' is visible to that function. It should probably be passed into the function.

Code:
matumish1.cpp: At global scope:
matumish1.cpp:57: error: variable or field ‘roo’ declared void

You forgot the function prototype for function 'roo'

Code:
matumish1.cpp:57: error: ‘single’ was not declared in this scope
matumish1.cpp:57: error: ‘a’ was not declared in this scope
matumish1.cpp:57: error: ‘single’ was not declared in this scope

All because there's no such type as 'single'. Again, I don't know what you intended. Maybe you meant to typedef a new type called 'single'?

Code:
matumish1.cpp: In function ‘void loo()’:
matumish1.cpp:66: error: return-statement with a value, in function returning 'void'

Hard to put it clearer. You returned a value from a function you declared as not returning anything (void).

Code:
matumish1.cpp:67: error: expected ‘;’ before ‘}’ token

Missing semi-colon after the 'return p'

I should mention that there are few reasons to use globals. There are such cases, but this is not one of them. For the most part, you should avoid using globals and instead declare them in your main and pass them to functions that need them.

Of course, if you're using classes, some globals can become instance variables instead when they relate to keeping the current state of the object.

And schip666! is probably right about the 'static int f = 0;' thing.

Not a big thing, but I wouldn't put a space between 'f' and '++'. I've never seen anyone put a space there, but 'f++' is preferable to 'f ++' in my books.

Lastly, you should put some blank lines between logical blocks of code to make it more readable. Between functions, between variable declarations and following code, etc.

Takes a little bit to get the syntax of C++ down, but you'll get there. Once you learn it, it will come in handy with a few other languages with similar syntax like Java. Good luck!
 
  • #4
The code has lots of errors. Here is a working version (other choices could have been made). It might be useful to one at a time, substitute in the errors into the working code and see why they fail.

Code:
long foo(long a, int b);
void goo(int &a, int b);
float soo(long a, double b);
bool too(double b);
int boo();
int main()
{
	int c,d;
	c = 4;
	d = c * 3;
	cout << foo(c,d) << endl;
	cout << c << " " << d << endl;
	goo (c,d);
	cout << d << endl;
	cout << soo(c,d) << endl;
	cout << 23 << too(d) << endl;
	for (int i = 0; i < 4; i++) cout << boo();
	cout << endl;
	return 0;
}
/*****************************************************/
long foo(long a, int b)
{
	a = 4;
	w = a * 3.1;
	return a * b;
}
/****************************************************/
void goo(int &a, int b)
{
	a = b * 4 * w;
}
/****************************************************/
float soo(long a, double b)
{
	return b * w * a;
}
/****************************************************/
bool too(double b)
{
	bool a = b && true || 0;
	return a;
}
/****************************************************
	this function should generate a number one higher than the previous time called */
int boo()
{
	static int f;
	f = 0;
	f ++;
	return f;
}
/****************************************************/
void roo (float &a, float b)
{
	float foo2= a; 	
	a = b; 	
	b = foo2;
	return;
}
/****************************************************
when should you use a static varable vs a global variable?	*/
void loo ()
{
	p = !p;
	return;
}
 
  • #5


I would first analyze the code and identify any potential errors or issues. Here are some potential errors in the code:

1. The function foo() is declared as returning a long data type, but the definition of the function has a return type of void. This should be corrected to match the declaration.

2. In the function too(), the variable "a" is used in the return statement, but it is not declared or defined anywhere in the function. This could lead to errors.

3. The function boo() is declared as returning an int data type, but the definition of the function has a return type of void. This should be corrected to match the declaration.

4. In the function roo(), the parameters "single &a" and "single b" are not declared or defined anywhere in the code. This could lead to errors.

5. The function loo() does not have a return type specified, but it is declared as returning a bool data type. This should be corrected to specify a return type.

6. The function loo() changes the value of the global variable "p" without any clear purpose or explanation. This could lead to unexpected results and should be revised.

7. The code does not have any comments or explanations, making it difficult to understand the purpose of each function and variable.

To fix these errors, the code should be carefully reviewed and revised. Comments and explanations should also be added to make the code more understandable. Additionally, it would be helpful to follow good coding practices such as using meaningful variable and function names, and avoiding global variables unless necessary.
 

1. How do I know if my code needs fixing?

There are a few signs that your code may need fixing. These include unexpected errors or bugs, slow performance, or difficulty understanding or maintaining the code. If you notice any of these issues, it may be time to take a closer look at your code.

2. What are some common mistakes to look for when trying to fix code?

Some common mistakes to look for include typos, missing or incorrect syntax, logic errors, and incorrect data types. It's also important to check for any unused or redundant code that may be causing issues.

3. How can I efficiently debug my code?

There are a few strategies for efficient code debugging. One approach is to use a debugger tool, which allows you to step through your code and track variable values. Another approach is to add console.log statements throughout your code to track the flow and values of variables.

4. What should I do if I can't fix the code on my own?

If you're having trouble fixing the code on your own, it may be helpful to seek assistance from a colleague or mentor. You can also try looking for solutions online, such as on coding forums or through documentation for the programming language or framework you're using.

5. How can I prevent code from needing to be fixed in the future?

One way to prevent code from needing to be fixed in the future is to follow best practices for coding, such as writing clean and organized code, using meaningful variable and function names, and commenting your code. It's also important to test your code thoroughly before deploying it and to regularly review and refactor your code as needed.

Similar threads

  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
22
Views
2K
  • Programming and Computer Science
Replies
23
Views
2K
  • Programming and Computer Science
Replies
23
Views
1K
  • Programming and Computer Science
2
Replies
36
Views
2K
Replies
10
Views
958
  • Programming and Computer Science
Replies
11
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
7
Views
894
  • Programming and Computer Science
Replies
12
Views
1K
Back
Top