Many Errors. Such confusion.

I need some help, I am completely new, and I'm getting these errors, which I don't understand. If someone could correct my code and or explain what these errors are and why they are happening that would be superb.



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <string>

int main()
{
    
}
int playerInput
{
    std::cout << "type 'help' for more information";
    std::string playerEnter = std::cin;
    if (playerEnter = help)
    {
        std::cout << "help placeholder";
    }
}

int playerTurn()
{
    int turnNumbers = turnNumbers + 1;
}



..\Playground\:10:52: error: expected '}' before ';' token
std::cout << "type 'help' for more information";
^
..\Playground\:10:15: error: invalid user-defined conversion from 'std::basic_ostream<char>' to 'int' [-fpermissive]
std::cout << "type 'help' for more information";
^
In file included from ..\Playground\\include\c++\ios:44:0,
from ..\Playground\\include\c++\ostream:38,
from ..\Playground\\include\c++\iostream:39,
from ..\Playground\:1:
..\Playground\\include\c++\bits\basic_ios.h:115:7: note: candidate is: std::basic_ios<_CharT, _Traits>::operator void*() const [with _CharT = char; _Traits = std::char_traits<char>] <near match>
operator void*() const
^
..\Playground\\include\c++\bits\basic_ios.h:115:7: note: no known conversion for implicit 'this' parameter from 'void*' to 'int'
..\Playground\:11:36: error: conversion from 'std::istream {aka std::basic_istream<char>}' to non-scalar type 'std::string {aka std::basic_string<char>}' requested
std::string playerEnter = std::cin;
^
..\Playground\:12:5: error: expected unqualified-id before 'if'
if (playerEnter = help)
^
..\Playground\:16:1: error: expected declaration before '}' token
}
^
Compare line 10 to line 4, do you see any differences?

What are you trying to do on line 11?

Beware, there is a difference between the assignment operator= and the comparison operator==.

Do you really want to do an assignment on line12.

Lastly you promised to return an item from your functions so you must return something from those functions.

When you get a long list of errors always start looking at the top. Many of the latter ones are probably caused indirectly by the first one.

The first error says that the compiler expected to find an ending curly bracket (}) before the semicolon (;) on line 10. This is a cryptic error message if you don't know exactly how the C++ code is parsed but what happens is that the compiler thinks that you are trying to define an int variable named playerInput and when it sees the opening curly bracket ({) on line 9 it expects a value followed by an ending curly bracket.

type variable-name { initial-value } ;

The reason why the compiler see playerInput as a variable is because there are no parentheses after the function name on line 8.

 
int playerInput()

If you recompile the program with this change you'll see that the first error is now gone. There will still be other errors but if you keep repeating this process of fixing the first error you should be able to make it work eventually.


To be honest I have to admit that I rarely analyze the error message as much as I did here. Usually when I get this kind of error message I just look at the line or the one above to see if I can spot something wrong. Usually that is enough for me to fix the error but I guess this takes a bit of practice. Only if I can't find the error at first glance will I analyze the error message further.
Last edited on
Line 8 is a function like line 18 and needs ().

Line 11 is incomplete

Add comments, with a program this small and incomplete no one can tell what your trying to do.

Hello garronj,

Here is your program with comments. It goes with jlb's and Peter87's assement of the problems.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>
#include <string>

int playerInput();  // <--- Missing prototypes.
int playerTurn();

int main()
{

}

int playerInput()  // <--- Missing ().
{
	std::cout << "type 'help' for more information";
	std::string playerEnter;  // <--- cin does not ork the way you had it. These two lines correct the error.
	std::cin >> playerEnter;

	if (playerEnter == "help")  //  Missing quotes around 'help' and should compare (==) not assign (=).
	{
		std::cout << "help placeholder";
	}

	//  <--- Missing return or should it be a "void" return because there is nothing to return?
}

int playerTurn()
{
	int turnNumbers = turnNumbers + 1;  // <--- Does not work this way.
	/*
	int turnNumbers{};  // <--- Initialize the variable.
	turnNumbers = turnNumbers + 1;
	or turnNumbers += 1;
	or turnNumbers++;
	*/
	//  <--- Missing return or should it be a "void" return because there is nothing to return?
}


In the "playerTuen" function "turnNumbers" is an uninitialized variable. On my computer this is usually a number like "-858993460". So if it would compile you would be adding one to that number. Not what you really want.

Hope this helps,

Andy
I appear to have pasted an old version of the code. Sorry about that, some of these errors I had already found, so you didn't have to do as much work as you did, but thank you anyway! Some of this really helped!
When it comes to dealing with high amount of errors you get to know that there is high complexity within the code. Those kind of errors can be detected by programs as checkmarx but might as well be avoided by coding slowly and detecting those on the go.
Good luck!
Topic archived. No new replies allowed.