(beginner) this simple code just errors

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2009
Posts: 8
Reputation: efecto is an unknown quantity at this point 
Solved Threads: 0
efecto efecto is offline Offline
Newbie Poster

(beginner) this simple code just errors

 
0
  #1
30 Days Ago
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

  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)
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 1,860
Reputation: twomers has a spectacular aura about twomers has a spectacular aura about twomers has a spectacular aura about 
Solved Threads: 55
twomers's Avatar
twomers twomers is offline Offline
Posting Virtuoso
 
0
  #2
30 Days Ago
And what errors are you getting?
I blag!?
"Mr Kitty, you have to live in the attic now. Here, write a diary."
I am the Walrus!
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 8
Reputation: efecto is an unknown quantity at this point 
Solved Threads: 0
efecto efecto is offline Offline
Newbie Poster
 
0
  #3
30 Days Ago
where do I find the error in detail? All isee is "1>time - 1 error(s), 0 warning(s)".

Thanks.
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 312
Reputation: jonsca is an unknown quantity at this point 
Solved Threads: 34
jonsca jonsca is offline Offline
Posting Whiz
 
0
  #4
30 Days Ago
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; 30 Days Ago at 5:10 am.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 8
Reputation: efecto is an unknown quantity at this point 
Solved Threads: 0
efecto efecto is offline Offline
Newbie Poster
 
0
  #5
30 Days Ago
This seems to have fixed it. I also changed to #include <iostream>
  1. std::cout << "time: " << stime << " date: " << sdate;

Thank you. How would you write it?

  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. }
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 312
Reputation: jonsca is an unknown quantity at this point 
Solved Threads: 34
jonsca jonsca is offline Offline
Posting Whiz
 
1
  #6
30 Days Ago
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)
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 73
Reputation: pspwxp fan is an unknown quantity at this point 
Solved Threads: 6
pspwxp fan pspwxp fan is offline Offline
Junior Poster in Training
 
0
  #7
30 Days Ago
Oh yeah, and don't forget, void mainers are doomed.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 8
Reputation: efecto is an unknown quantity at this point 
Solved Threads: 0
efecto efecto is offline Offline
Newbie Poster
 
0
  #8
27 Days Ago
why is "void" doom'd?

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

myArray[];
for (each in myArray) {
do this;
}
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 312
Reputation: jonsca is an unknown quantity at this point 
Solved Threads: 34
jonsca jonsca is offline Offline
Posting Whiz
 
1
  #9
27 Days Ago
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; 27 Days Ago at 5:04 pm.
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC