0

I am creating a time class (string base) for my school project! I get a pointer character for the time!I have a function to normalize the time if it is weird.
in normalize function I have a character array to store the correct time but when I want to Assign the character array to the pointer character it is going false!

char st[10] = "", sh[3] = "", sm[3] = "", ss[3] = "";
itoa(hour, sh, 10);
itoa(minute, sm, 10);
itoa(second, ss, 10);

if(hour<10){strcat(st, "0");}
strcat(st, sh);strcat(st, ":");
if(minute<10){strcat(st, "0");}
strcat(st, sm);strcat(st, ":");
if(second<10){strcat(st, "0");}
strcat(st, ss);strcat(st, "");

stime = st;

stime is pointer character which save the time in class.
when I want to use value of stime I get very weird result. stime get the value of last class stime. for example I have this code:

time a("1:50:0"), b("4:5:10");
a.print();
b.print();

but I get 04:05:10 for two classes and I don't know why!
If you need the rest of code I upload it here: Google Drive link to file

5
  • 2
    Use std::string please when programming in c++. This makes my eyes bleed. Commented Jun 6, 2016 at 17:32
  • Would need to see class declaration to be sure but I would guess that the way you are assigning stime is causing all of them to point at the same memory location and so only the most recently assigned stime prints. Commented Jun 6, 2016 at 17:38
  • @Russ I added file link in Post! Commented Jun 7, 2016 at 10:03
  • @πάνταῥεῖ If that's painful to you, note that he's done his whole implementation in a header file Commented Jun 7, 2016 at 10:28
  • Your code does not compile with g++. Commented Jun 7, 2016 at 11:04

2 Answers 2

1

When I compile your code I get the following warning:

warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]
   time(char *t = "0:0:0"):stime(t){normalize(-1, -1, -1);}

This is what is causing your issue.

"0:0:0" in C++ is a const char[5] which can be converted implicitly to a const char * but not to a simple char *, which is the storage type you have chosen for time.

As others have mentioned, in C++ you should use std::string rather than char*.


As a general rule you should never ignore warnings unless you are sure you know why they are appearing. Often, as in this case, they are telling you that your code is not going to behave in the way you'd expect.

Sign up to request clarification or add additional context in comments.

6 Comments

I think you could keep your previous answer (or add it here), because although the question is tagged as C++, the OP has essentially attempted to solve it in C, so your original solution is nonetheless relevant here.
The reason I removed it is because I realized that it doesn't answer the actual question of "why do these two calls print the same thing" and even after applying my change OP would have the same issue. Your answer advocates the use of std::strings and would fix the underlying issue.
@sokkyoku, I think you're wrong. C++ string literals are of type const char[]. The problem is that the time constructor wants char *, not const char *. (But yes, using std::strings is absolutely the correct approach!)
@Roddy Ah yes, good catch! I focused on the "string" part of the warning and got confused. The "constant" part was the matter, I'll edit to reflect that.
@sokkayoku but I can not use functions like strcat() or strcpy() with strings.
|
1

You can try this as a C++ solution:

#include <sstream>
#include <iomanip>
#include <iostream>

using namespace std;

string GetComponent(int value)
{
    ostringstream oss;
    oss << setfill('0') << setw(2) << value;
    return oss.str();
}

void PrintTime(int hh,int mm,int ss)
{
    cout << GetComponent(hh) << ':' << GetComponent(mm) << ':' << GetComponent(ss) << endl;
}

Usage example:

PrintTime(1,2,3);
PrintTime(1,2,33);
PrintTime(1,22,33);
PrintTime(11,22,33);

3 Comments

But I want to save the time in a variable when the class constructor called and I could extract hour, minute and second in that or other works like that.
@amirsa00: I don't see any classes or constructors in your code, only a C-style attempt to print the time. I suggest that you first make up your mind as to what language exactly you're focusing on, then post the relevant piece of code that you have at hand, and ask questions directly related to that piece of code.
@barakmanos there actually is a class in the google drive hosted file that's linked at the bottom of the question. I agree however that this is clumsy C-style code, which suggests that OP is a C++ beginner

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.