What Are the Errors in This C++ Code?

  • Context: C/C++ 
  • Thread starter Thread starter Demon117
  • Start date Start date
  • Tags Tags
    Code
Click For Summary

Discussion Overview

The discussion focuses on identifying errors in a provided C++ code snippet. Participants analyze various aspects of the code, including syntax issues, variable scope, and function definitions. The scope includes technical explanations and debugging related to programming in C++.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Homework-related

Main Points Raised

  • One participant points out that the use of a semicolon after the #include directive is incorrect.
  • Another participant suggests that the function boo() always returns 1 due to resetting the static variable f to 0 on each call.
  • A participant highlights that the expression 23 || too(d) in the cout statement is problematic and needs parentheses for clarity.
  • Concerns are raised about the variable 'a' not being declared in the function too(double), indicating a potential scope issue.
  • Errors related to the function roo() are noted, specifically the absence of a type declaration for 'single'.
  • It is mentioned that the function loo() incorrectly attempts to return a value despite being declared as void.
  • Formatting suggestions are made regarding spacing in the code and the use of blank lines for readability.

Areas of Agreement / Disagreement

Participants generally agree that there are multiple errors in the code, but there is no consensus on a single solution or approach to fixing all issues. Different participants propose various corrections and alternatives.

Contextual Notes

Some errors are dependent on the specific context of variable declarations and function prototypes, which may not be fully addressed in the discussion. Additionally, the discussion does not resolve the best practices regarding the use of global versus static variables.

Demon117
Messages
162
Reaction score
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
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...
 
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!
 
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;
}
 

Similar threads

  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 22 ·
Replies
22
Views
4K
  • · Replies 23 ·
Replies
23
Views
3K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 7 ·
Replies
7
Views
1K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 4 ·
Replies
4
Views
10K