Hello all, i'm new to C++. I'm struggling to run this simple program..
I created a file called source.cpp that the code below. I have no other files in the same project..

Spec: Windows vista, Visual c++ 2008 compiler, 32bits

#include <iostream.h>
#include <time.h>

void main() {
	char sdate[9];
	char stime[9];
	_strdate( sdate );
	_strtime( stime );
	cout << "time: " << stime << " date: " << sdate;
	//cout << "Hi ";
	return 0;
}

1>time - 1 error(s), 0 warning(s)

Recommended Answers

All 8 Replies

And what errors are you getting?

where do I find the error in detail? All isee is "1>time - 1 error(s), 0 warning(s)".

Thanks.

Firstly, you should be creating it as a Win32 console project.
Secondly, #include <iostream> , as iostream.h is non-standard. Start with those and it should give you two warnings and an error. The warnings are about using your two time functions with an _s at the end, which makes them safer from buffer overflows which can be a security issue down the line. You need a using statement to clarify your cout call.

P.S. find the errors in the output tab at the bottom of the screen, after you've run your build

This seems to have fixed it. I also changed to #include <iostream>

std::cout << "time: " << stime << " date: " << sdate;

Thank you. How would you write it?

#include <iostream>
#include <time.h>

void main() {
	char sdate[9];
	char stime[9];
	_strdate( sdate );
	_strtime( stime );
	std::cout << "time: " << stime << " date: " << sdate;
	//cout << "Hi ";
	//return 0;
	system("pause >nul");
}

you can also write using std::cout; at the top to cover every time you use it (or using namespace std; , but that's not encouraged as it defeats the purpose of having a namespace)

Oh yeah, and don't forget, void mainers are doomed.

why is "void" doom'd?

Also is there a for loop like below in C++?

myArray[];
for (each in myArray) {
do this;
}

The main function is supposed to return a status to the invoking program (e.g., the operating system) so when people have declared main() as returning an int and they say return 0; they really mean that the program executed successfully (there are other error codes used in specific circumstances where the execution has been interrupted). It is "doomed" because a C++ program abiding by the standard should have a main returning int. void main() is outdated.

For your second question, yes, but it's slightly more complicated than in C#. You still need a collection of some sort, but your collection must of the Standard Template Library variety (e.g., vector). Check out http://www.cplusplus.com/reference/algorithm/for_each/ to see all the requirements.

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.