This is a read-only archive. No new posts or registrations. Privacy Page
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.

Looking for the outcome? Ask AI

#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;
}

Ask AI

AI can make mistakes. Check the cited posts. Archived advice can be out-of-date

Don't include personal information. Questions and selected public posts go to OpenAI. About Ask AI