This is a read-only archive. No new posts or registrations. Privacy Page Still need help? Ask the volunteers at Geeks to Go, free.
Software

What's wrong with my code?

1 min read

This thread's last reply is from . Advice, software, and links below may be out of date — treat specific steps and download links with caution.

#include using namespace std; int main() { int i = k + 1; cout << i++ << << endl; int i = 1; cout << i++ << << endl; return 0; }
Hi :welcome:

This line throws an error: int i = k + 1;
because k is not declared before being used.


This line: cout << i++ <<_<< endl;
is missing an expression, you could do it like this: cout << i++ << "" << endl;

You can't redeclare the variable i again as it was already declared, use another variable name instead: int i = 1;

Note: It's always a good idea to initialize your variables before using it in a computation.

Here is a working sample based on your code:

/* 
 * File:   test.cpp
 */

#include 

using namespace std;

int main() {
	int i = 0; // declaration and initialization

	i = i + 1;
	cout << "sum of i:" << i << endl;

	int x = 1; // declaration and initialization, another variable
	cout << "value of x:" << x << endl;

	return 0;
}

Still having this problem?

This topic is from the What the Tech archive and can't take new replies. Geeks to Go is an active, free help community run by volunteers. Post your question there and a person, not a bot, will work through it with you.

Ask on Geeks to Go →