944,043 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 430
  • C++ RSS
Oct 29th, 2009
0

(beginner) this simple code just errors

Expand Post »
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

C++ Syntax (Toggle Plain Text)
  1. #include <iostream.h>
  2. #include <time.h>
  3.  
  4. void main() {
  5. char sdate[9];
  6. char stime[9];
  7. _strdate( sdate );
  8. _strtime( stime );
  9. cout << "time: " << stime << " date: " << sdate;
  10. //cout << "Hi ";
  11. return 0;
  12. }

1>time - 1 error(s), 0 warning(s)
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
efecto is offline Offline
11 posts
since Oct 2009
Oct 29th, 2009
0
Re: (beginner) this simple code just errors
And what errors are you getting?
Reputation Points: 453
Solved Threads: 57
Posting Virtuoso
twomers is offline Offline
1,873 posts
since May 2007
Oct 29th, 2009
0
Re: (beginner) this simple code just errors
where do I find the error in detail? All isee is "1>time - 1 error(s), 0 warning(s)".

Thanks.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
efecto is offline Offline
11 posts
since Oct 2009
Oct 29th, 2009
0
Re: (beginner) this simple code just errors
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
Last edited by jonsca; Oct 29th, 2009 at 5:10 am.
Sponsor
Featured Poster
Reputation Points: 1165
Solved Threads: 578
Quantitative Phrenologist
jonsca is offline Offline
4,271 posts
since Sep 2009
Oct 29th, 2009
0
Re: (beginner) this simple code just errors
This seems to have fixed it. I also changed to #include <iostream>
C++ Syntax (Toggle Plain Text)
  1. std::cout << "time: " << stime << " date: " << sdate;

Thank you. How would you write it?

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <time.h>
  3.  
  4. void main() {
  5. char sdate[9];
  6. char stime[9];
  7. _strdate( sdate );
  8. _strtime( stime );
  9. std::cout << "time: " << stime << " date: " << sdate;
  10. //cout << "Hi ";
  11. //return 0;
  12. system("pause >nul");
  13. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
efecto is offline Offline
11 posts
since Oct 2009
Oct 29th, 2009
1
Re: (beginner) this simple code just errors
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)
Sponsor
Featured Poster
Reputation Points: 1165
Solved Threads: 578
Quantitative Phrenologist
jonsca is offline Offline
4,271 posts
since Sep 2009
Oct 29th, 2009
0
Re: (beginner) this simple code just errors
Oh yeah, and don't forget, void mainers are doomed.
Reputation Points: 43
Solved Threads: 7
Junior Poster in Training
pspwxp fan is offline Offline
93 posts
since Feb 2009
Oct 31st, 2009
0
Re: (beginner) this simple code just errors
why is "void" doom'd?

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

myArray[];
for (each in myArray) {
do this;
}
Reputation Points: 10
Solved Threads: 0
Newbie Poster
efecto is offline Offline
11 posts
since Oct 2009
Oct 31st, 2009
1
Re: (beginner) this simple code just errors
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.
Last edited by jonsca; Oct 31st, 2009 at 5:04 pm.
Sponsor
Featured Poster
Reputation Points: 1165
Solved Threads: 578
Quantitative Phrenologist
jonsca is offline Offline
4,271 posts
since Sep 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: plz help me with this
Next Thread in C++ Forum Timeline: programming question





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC