C/C++ What Are the Errors in This C++ Code?

  • Thread starter Thread starter Demon117
  • Start date Start date
  • Tags Tags
    Code
AI Thread Summary
The discussion centers on a C++ code snippet that contains multiple errors and warnings. Key issues identified include the incorrect use of a semicolon after the `#include` directive, invalid operands in a `cout` statement, and undeclared variables in functions. The `boo()` function is highlighted for always returning 1 due to the incorrect initialization of its static variable. Additionally, there are problems with the `roo()` function due to the use of an undeclared type `single`, and the `loo()` function improperly attempts to return a value despite being declared as `void`. Suggestions for improvement include avoiding global variables, using proper function prototypes, and enhancing code readability with spacing. A corrected version of the code is provided, demonstrating proper syntax and structure.
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;
}
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...

Similar threads

Replies
5
Views
3K
Replies
22
Views
3K
Replies
23
Views
2K
Replies
7
Views
1K
Replies
1
Views
2K
Replies
35
Views
4K
Replies
23
Views
2K
Replies
17
Views
2K
Back
Top