Hi,

I'm just trying out something with returning values to a variable and I've encountered a problem.

Basically the program is supposed to return the value of

string x

as either true or false from the

isEven

function, then the

void output

function should output the value of

string x

The problem is that at line 8, my IDE says,

ERROR: expected a declaration

The code :

#include "stdafx.h"
#include "stdio.h"
#include <iostream>


using namespace std;

	void output {
		cout << x;
		cin.get();
	}
	
	string IsEven(string x)
	{
		if ( 100 / 3 % 2 == 0 ) return "even";
		else return "odd";
	}

Dani AI

Generated

Nice catch by — that fixed the immediate syntax error reported by the compiler. A few follow-ups that commonly bite beginners and are worth addressing so the program behaves as intended.

Prefer explicit parameters and appropriate types. Make output accept the string to print instead of relying on a global x, and make your parity check operate on an integer. Returning a bool for even/odd and converting to text at the call-site keeps responsibilities clear. For example, define a parity function that takes an integer and an output routine that takes a string parameter:

bool isEven(int n) {
    return (n % 2) == 0;
}

void printLine(const std::string &text) {
    std::cout << text << '\n';
}

If your input is a string, convert it safely to an integer before testing. Use std::stoi (or std::strtol for C-style conversion) and catch std::invalid_argument/std::out_of_range to handle bad input. Example flow:

try {
    int value = std::stoi(userString);
    printLine(isEven(value) ? "even" : "odd");
} catch (const std::exception &) {
    printLine("input is not a valid integer");
}

Other practical tips: include <string> when using std::string; if you are using Visual Studio and stdafx.h is present, it must be the first include in the .cpp file. Avoid using namespace std; in headers and prefer std:: qualifiers in larger projects. Finally, build incrementally: fix one compiler error at a time, then run to check logic; that approach catches both syntax and runtime issues quickly.

These changes improve clarity, prevent scope bugs with x, and make the parity logic correct and reusable.

Recommended Answers

All 2 Replies

You need a pair of brackets even if you don't provide arguments for a function. ;)

Thank you very much. It worked!

void output () {
		cout << x;
		cin.get();
	}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.