Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

How to make it work?

c++ array characters

  • Please log in to reply
7 replies to this topic

#1 newbeme

newbeme

    CC Lurker

  • New Member
  • Pip
  • 4 posts
  • Programming Language:Others
  • Learning:C++

Posted 24 September 2012 - 04:22 AM

i want to save( the current time and display it later if the user choose display)
i have no idea what to do... Need your help!




#include <ctime>
#include <iostream>
#include <conio.h>
#include <string.h>


using namespace std;
static char Curtime[ 256 ] ;
int disp ();
int num,n=0;
char stime[256];
int main()
{
  static time_t raw ;
  time( &raw ) ;
  static tm* timeinfo ;
  timeinfo = localtime( &raw ) ;
  strftime( Curtime, 255, "- %a %b %d %Y %X -\n", timeinfo) ;
  disp ();	
  getch();
return 0;
}
int disp ()
{

  cout<<Curtime;
   cout<<"\n\t1.) To Current Time Save";
  cout<<"\n\t2.) Display all save.\n\n\tOption Selected: ";
  cin>>num;
  if (num==1)
  {
	 cout<<"\n\tData Saved";
	 strcpy(Curtime,stime[n]);
	 n++;
	 getch();
	 system("cls");
	 disp();
			 }
  if (num==2)
  {
			 cout<<"\n\tAll Time Saved\n";
			 for(i=0;i<=n;i++)
			 {
			 cout<<stime[i];
			 }
  else
  {
	  cout<<"\n\tinvalid input";
	  system("cls");
	  disp();
	  } 
}

Edited by dargueta, 27 September 2012 - 10:31 PM.
Added code tags

  • 0

#2 kernelcoder

kernelcoder

    CC Devotee

  • Expert Member
  • PipPipPipPipPipPip
  • 990 posts
  • Location:Dhaka
  • Programming Language:C, Java, C++, C#, Visual Basic .NET
  • Learning:Objective-C, PHP, Python, Delphi/Object Pascal

Posted 24 September 2012 - 04:57 AM

Hey, Welcome to CodeCall!

What you did just put your code. Can you please explain your problem a bit in English? I mean tell us in plain text what is your goal and what is your plan.
  • 0

#3 newbeme

newbeme

    CC Lurker

  • New Member
  • Pip
  • 4 posts
  • Programming Language:Others
  • Learning:C++

Posted 24 September 2012 - 05:19 AM

sorry just new here :D i appreciate your welcome Sir kernelcoder.. i already post my problem and wanted outcome
  • 0

#4 lespauled

lespauled

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1360 posts
  • Programming Language:C, C++, C#, JavaScript, PL/SQL, Delphi/Object Pascal, Visual Basic .NET, Pascal, Transact-SQL, Bash

Posted 24 September 2012 - 06:54 AM

The code looks like it wont compile because your brackets are out of wack. Is that correct?
  • 0
My Blog: http://forum.codecal...699-blog-77241/
"Women and Music: I'm always amazed by other people's choices." - David Lee Roth

#5 notes

notes

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 88 posts
  • Location:Poland
  • Programming Language:C++
  • Learning:Java, C#

Posted 24 September 2012 - 07:13 AM

Do you want to save your current data so that another user can later run your program and see saved data ?
Or you want just to put your data in dynamic memory and see it while program is still running ?

The solution to first answer is - in my opinion- a bit more complex. You will have to store your time-date variable in some file on your disk.
To achive second question you will need to store data's in some sort of array, or list, vector, queue(all implemented in STL's).

In your place I would first try to open and read/write file on your disk and later try more complex tasks :)
  • 1
Remebre about KISS & DRY

#6 newbeme

newbeme

    CC Lurker

  • New Member
  • Pip
  • 4 posts
  • Programming Language:Others
  • Learning:C++

Posted 24 September 2012 - 09:27 AM

@notes thanks a lot sir ill try to look for guide using open and read/write file....
  • 0

#7 newbeme

newbeme

    CC Lurker

  • New Member
  • Pip
  • 4 posts
  • Programming Language:Others
  • Learning:C++

Posted 24 September 2012 - 11:13 PM

i Tried making a file on the save option but when i open the file the time save is all the same .......

#include <ctime>
#include <iostream>
#include <conio.h>
#include <string.h>
#include <stdio.h>

using namespace std;
static char Curtime[ 256 ] ;
int disp ();
int num,n=0,i;
char stime[256];
FILE * pFile;
int main()
{
   static time_t raw ;
   time( &raw ) ;
   static tm* timeinfo ;
   timeinfo = localtime( &raw ) ;
   strftime( Curtime, 255, "%a %b %d %Y %X", timeinfo) ;
   cout<<Curtime<<endl;
   disp ();
  getch();
return 0;
}
int disp ()
{

	cout<<"\n\t1.) To Save Current Time";
   cout<<"\n\t2.) Display all save.\n\n\tOption Selected: ";
   cin>>num;
   if (num==1)
   {
	cout<<"\n\tData Saved";
	pFile = fopen ("timefile.txt","w");
	  for(i=1;i<=n;i++)
	  {
	  fprintf (pFile, "Time Save %d[%-24.25s]\n",i,Curtime);
	  }
	   fclose (pFile);
	   n++;
	   getch();
	   system("cls");
		disp();
		 }
   else if (num==2)
   {
	cout<<"\n\tAll Time Saved\n";
	 }
   else
   {
   cout<<"\n\tinvalid input";
   system("cls");
   disp();
   }
}

Edited by dargueta, 27 September 2012 - 10:31 PM.
Added code tags

  • 0

#8 BlackRabbit

BlackRabbit

    CodeCall Legend

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 3871 posts
  • Location:Argentina
  • Programming Language:C, C++, C#, PHP, JavaScript, Transact-SQL, Bash, Others
  • Learning:Java, Others

Posted 25 September 2012 - 02:45 AM

Hello Newbeme,
the problem seems to be you are saving N times (in the loop) the same time variable ( curtime ) which you calculated just once in the main()

so, in order to get current time you should calculate everytime inside the loop, but still the loop will work so fast that it could even be the same anyway, you can test this concept by adding a sleep or key input in between fprintfs, and of course recalculating the time in every single loop
  • 0





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download