#include <iostream>
using namespace std;

int Main()
{
	int i = k + 1;
	cout <<i ++ << << endl;

	int i = 1;
	cout << i++ << << end1;

	return 0;
}

Please help me? What is wrong with this code? I am a C++ Noob!
I just started and I can not figure it out?

Recommended Answers

All 6 Replies

1: Where did k come from? i don't see it declared anywhere.

2: you are declaring i twice...that could cause some problems.


3: What exactly are you trying to do?


Add code tags

1: Where did k come from? i don't see it declared anywhere.

2: you are declaring i twice...that could cause some problems.


3: What exactly are you trying to do?

Well this is a problem out of a textbook, and it says to identify the errors and fix the following code?

How would I go about declaring that k?
Thank you so much for your time, im still a noob :o(

To build on what shindu mentioned...

K needs to be declared before using it.
int k = 0;
k=k+2;

The above is OK because k is declared as an int variable before it is used.

The variable i is declared twice (int i). It should only be declared once.

P.S. those are not the only errors

#include <iostream>
using namespace std;

int Main()
{
	int i = k + 1;
	cout <<i ++ << << endl;

	int i = 1;
	cout << i++ << << end1;

	return 0;
}

first of all.

int i =k + 1;

this line declares a variable of type int and assigns the value of (k+1) to it. Unfortunately k has not been declared so it wont work.

second

cout <<i ++ << << endl;

This line outputs to the console the value of i and then increments it by 1. then after the << is nothing which will cause an error in compiling.

third

int i = 1;

a redeclaration of i?

forth
in the second cout you have "end1;" which means nothing it should be "endl;"
again what is this program supposed to do anyways? out put 1 twice?

commented: Good post! +8

Thanks Harley, I only have 2 errors left now!

The last 2 errors say this:

error C2065: 'end1' : undeclared identifier

At the end of your cout << i++ << endl;

end1 should be endl. it ends the line & puts the cursor on the next line. your compiler should give you the line of the error to make them easier to find.

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.