What Are the Errors in This C++ Code?

  • Context: C/C++ 
  • Thread starter Thread starter Demon117
  • Start date Start date
  • Tags Tags
    Code
Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
3 replies · 2K views
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.
 
Physics 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;
}