Still learning in programming and this problem is course related. Although I am not asking for the answer I would appreciate some help and if anyone can point out what I might be doing wrong that would be awesome.

I was able to find other articles addressing the matter although I am still unable to compile...

Thank you for your time.

Issue:
- MyTime.cpp:88: error: ‘itoa’ was not declared in this scope

Program:
- MyTime
- I am currently trying to make a program that is not compiling because of the issue stated above.
- The program is to display the times that I specify in my driver program.
- Although in creating my string I am unable to compile.

Utilities:
- Mac OSX
- Compiling in terminal using g++

***MyTime.h***

#include <string> 
#include <iostream> 

using namespace std; 

class MyTime
{

public: 
    // Constructors
    MyTime();
    MyTime(int h, int m, int s); 
    
    // Accessors 
    int getHour();
    int getMinute(); 
    int getSecond(); 
    
    // Modifiers
    void setMyTime(int h, int m, int s); 
    
    // Operations 
    MyTime add(MyTime t); 
    MyTime subtract(MyTime t); 
    MyTime add(int h); 
    
    // String
    string toString();  // HH:MM:SS

private: 
    int hour;
    int minute;
    int second; 
    
};

***MyTime.cpp***

#include "MyTime.h"
#include <cstdlib>
#include <iostream>

using namespace std; 

// Constructors
MyTime::MyTime()
{
    hour = 0;
    minute = 0;
    second = 0; 
}

MyTime::MyTime(int h, int m, int s)
{
    hour = h;
    minute = m;
    second = s; 
}
   
// Accessors 
int MyTime::getHour()
{
    return hour;    
}

int MyTime::getMinute()
{
    return minute; 
}

int MyTime::getSecond()
{
    return second; 
}

/***    
// Modifiers
void MyTime::setMyTime(int h, int m, int s)
{

}
***/

// Operations 
MyTime MyTime::add(MyTime t)
{    
    MyTime result;
    result.hour = hour + t.hour;
    result.minute = minute + t.minute; 
    result.second = second + t.second; 
    return result; 
}

MyTime MyTime::subtract(MyTime t)
{
    MyTime result;
    result.hour = hour - t.hour;
    result.minute = minute - t.minute; 
    result.second = second - t.second; 
    return result; 
}

/***
MyTime MyTime::add(int h)
{

}
***/

// String
string MyTime::toString()  // HH:MM:SS
{
    // Declare C-String
    char hourCstr[10];
    char minuteCstr[10];
    char secondCstr[10];
    
    // itoa(int, C-String)
    itoa(hour, hourCstr, 10);
    itoa(minute, minuteCstr, 10); 
    itoa(second, secondCstr, 10);
    
    // Instantiate C++ string with C-String value
    string hourCPstr(hourCstr);
    string minuteCPstr(minuteCstr);
    string secondCPstr(secondCstr);
    
    return hourCPstr + ":" + minuteCPstr + ":" + secondCPstr;
}

***MyTimeTest.cpp***

#include "MyTime.h"
#include <iostream>

using namespace std; 

int main()
{
    MyTime t1(2, 25, 35);
    MyTime t2(1, 40, 15);
    
    cout << "Time 1: " << t1.toString() << endl;
    cout << "Time 2: " << t2.toString() << endl; 
    
    MyTime sum = t1.add(t2); 
    cout << "The sum of both times: " << sum.toString() << endl; 

    cin.get();
    return 0;
}

Recommended Answers

All 15 Replies

I apologize but from what I've read in that thread utilizing sprintf() function is recommended. Although I'm not familiar with the code in using that function so I'm experiencing other errors...

I've also found that using the itoa() function has been used in linux... so I'm curious as to why I am unable to use it...

itoa isn't standard. I don't know why. But since it isn't standard, some compilers will have it, some won't. You can either find a compiler that has it, write your own, or use a standard function like sprintf. I suggest going with options 2 or 3.

I appreciate the info and I am currently trying the sprintf() although I'm receiving other errors. I am unable to utilize another compiler and I wouldn't want to write one lol if I can't even get this program to compile...


Issue:
- invalid conversion from 'int' to 'char*'

***MyTime.cpp***

#include "MyTime.h"
#include <cstdlib>
#include <iostream>
#include <stdlib.h> 

using namespace std; 

// Constructors
MyTime::MyTime()
{
    hour = 0;
    minute = 0;
    second = 0; 
}

MyTime::MyTime(int h, int m, int s)
{
    hour = h;
    minute = m;
    second = s; 
}
   
// Accessors 
int MyTime::getHour()
{
    return hour;    
}

int MyTime::getMinute()
{
    return minute; 
}

int MyTime::getSecond()
{
    return second; 
}

/***    
// Modifiers
void MyTime::setMyTime(int h, int m, int s)
{

}
***/

// Operations 
MyTime MyTime::add(MyTime t)
{    
    MyTime result;
    result.hour = hour + t.hour;
    result.minute = minute + t.minute; 
    result.second = second + t.second; 
    return result; 
}

MyTime MyTime::subtract(MyTime t)
{
    MyTime result;
    result.hour = hour - t.hour;
    result.minute = minute - t.minute; 
    result.second = second - t.second; 
    return result; 
}

/***
MyTime MyTime::add(int h)
{

}
***/

// String
string MyTime::toString()  // HH:MM:SS
{
    // Declare C-String
    char hourCstr[10];
    char minuteCstr[10];
    char secondCstr[10];
    
    //****************************
    //Trying both itoa and sprintf
    
    // itoa(int, C-String)
    //itoa(hour, hourCstr, 10);
    //itoa(minute, minuteCstr, 10); 
    //itoa(second, secondCstr, 10);
    
    sprintf(hour, hourCstr, 10); 
    sprintf(minute, minuteCstr, 10);
    sprintf(second, secondCstr, 10);
            
    // Instantiate C++ string with C-String value
    string hourCPstr(hourCstr);
    string minuteCPstr(minuteCstr);
    string secondCPstr(secondCstr);
    
    return hourCPstr + ":" + minuteCPstr + ":" + secondCPstr;
}

>> Issue: - invalid conversion from 'int' to 'char*'


Line number?

Sorry,

Lines 95. 96. 97.

sprintf(hour, hourCstr, 10); 
    sprintf(minute, minuteCstr, 10);
    sprintf(second, secondCstr, 10);

Just a quick note I'm afraid I'm mixing both sprintf function and iota function and I'm not sure which way to code the proper string so that I can receive an output of:

- HH:MM:SS

I apologize for the confusion still a little new to the forums and well programming...

- HH : MM : SS

(Trying to display time correctly)

You haven't specified a format like "%d"(red below).

http://www.cplusplus.com/reference/clibrary/cstdio/sprintf/

int sprintf ( char * str, const char * format, ... );

Also, the first parameter is char*(green above).

See the examples on the itoa page.

http://www.cplusplus.com/reference/clibrary/cstdlib/itoa/

A standard-compliant alternative for some cases may be sprintf:
sprintf(str,"%d",value) converts to decimal base.
sprintf(str,"%x",value) converts to hexadecimal base.
sprintf(str,"%o",value) converts to octal base.

>> Just a quick note I'm afraid I'm mixing both sprintf function and iota function

You are. There is no base parameter in sprintf like there is in itoa. Base 10 is "%d", so use that.

Base 10 is "%d", so use that.

Actually, since you are specifying a width of 2, padded by zeroes in the front, you'd want "%02d", not "%d". And I don't see a reason to create three separate strings. One will do, so since you have 8 printable characters, the buffer would need to be at least length 9 for the NULL terminator.

#include <iostream>
#include <cstdlib>
using namespace std;

int main()
{
    int hour = 9, min = 23, sec = 8;
    char buffer[9];
    sprintf(buffer, "%02d:%02d:%02d", hour, min, sec);
    cout << buffer << endl;
    return 0;
}

Results:

09:23:08

Sadly I am unable to see the correlation and how I should even be designing this...

Out of this toString() what is even required? I'm unaware of how to utilize this sprintf function.

***MyTime.cpp***

// String
string MyTime::toString()  // HH:MM:SS
{
    // Declare C-String
    char hourCstr[10];
    char minuteCstr[10];
    char secondCstr[10];
    
    //****************************
    //Trying both itoa and sprintf
    
    // itoa(int, C-String)
    //itoa(hour, hourCstr, 10);
    //itoa(minute, minuteCstr, 10); 
    //itoa(second, secondCstr, 10);
    
    sprintf("%d", hour, hourCstr, 10); 
    sprintf("%d", minute, minuteCstr, 10);
    sprintf("%d", second, secondCstr, 10);
            
    // Instantiate C++ string with C-String value
    string hourCPstr(hourCstr);
    string minuteCPstr(minuteCstr);
    string secondCPstr(secondCstr);
    
    return hourCPstr + ":" + minuteCPstr + ":" + secondCPstr;
}

VernonDozier I would like to say thank you for your time. I was able to compile and therefore utilize the function that I created. Provided the example you had written.

Although if you could please explain what you have made. For example the buffer is what you declared the entire string is what I'm figuring.

Anyways I appreciate your input.

With this particular string I was able to declare the time im MyTimeTest.cpp program and was able to specify the time.

This is what I was able to come up with:

***MyTime.cpp***

#include "MyTime.h"
#include <cstdlib>
#include <iostream>
#include <stdlib.h> 

using namespace std; 

// Constructors
MyTime::MyTime()
{
    hour = 0;
    minute = 0;
    second = 0; 
}

MyTime::MyTime(int h, int m, int s)
{
    hour = h;
    minute = m;
    second = s; 
}
   
// Accessors 
int MyTime::getHour()
{
    return hour;    
}

int MyTime::getMinute()
{
    return minute; 
}

int MyTime::getSecond()
{
    return second; 
}

/***    
// Modifiers
void MyTime::setMyTime(int h, int m, int s)
{

}
***/

// Operations 
MyTime MyTime::add(MyTime t)
{    
    MyTime result;
    result.hour = hour + t.hour;
    result.minute = minute + t.minute; 
    result.second = second + t.second; 
    return result; 
}

MyTime MyTime::subtract(MyTime t)
{
    MyTime result;
    result.hour = hour - t.hour;
    result.minute = minute - t.minute; 
    result.second = second - t.second; 
    return result; 
}

/***
MyTime MyTime::add(int h)
{

}
***/

// String
string MyTime::toString()  // HH:MM:SS
{

/********************************
    // Declare C-String
    char hourCstr[10];
    char minuteCstr[10];
    char secondCstr[10];
        
    // itoa(int, C-String)
    itoa(hour, hourCstr, 10);
    itoa(minute, minuteCstr, 10); 
    itoa(second, secondCstr, 10);
    
    sprintf("%d", hour, hourCstr, 10); 
    sprintf("%d", minute, minuteCstr, 10);
    sprintf("%d", second, secondCstr, 10);
            
    // Instantiate C++ string with C-String value
    string hourCPstr(hourCstr);
    string minuteCPstr(minuteCstr);
    string secondCPstr(secondCstr);
    
    return hourCPstr + ":" + minuteCPstr + ":" + secondCPstr;
********************************/

char buffer[9]; 
sprintf(buffer, "%02d:%02d:%02d", hour, minute, second); 
return buffer;  

}

You're welcome. You'll want to covert buffer to a string since your function says it's returning a string.

char buffer[9]; 
sprintf(buffer, "%02d:%02d:%02d", hour, minute, second); 
string str(buffer); // added
return str;

buffer is a character array large enough to hold the entire string (9 characters, one for the NULL terminator). The sprintf function requires a char* to "print" to (i.e. fill in) and that's what buffer is here.

int sprintf ( char * str, const char * format, ... );

Filling in buffer:

int sprintf ( buffer, const char * format, ... );

Filling in format

int sprintf ( buffer, "%02d:%02d:%02d", ... );

"%02d:%02d:%02d" - each %02d corresponds to an integer variable. That's where the "..." comes in. One variable for every % sign, so the "..." is replaced by your three variables.

"%02d" - % signifies the start of a formatter. The "2" means you want this to be exactly two characters long. The "d" means "integer type". the "0" means pad with zeroes until you get 2 spaces.

"%02d:%02d:%02d" - The colons are printed as colons.

If you look at the sprintf page, there is a good chart of the formatting types.

http://www.cplusplus.com/reference/clibrary/cstdio/sprintf/


So you provide a character buffer that gets filled in. If the function was printf instead of sprintf, you would not provide that buffer and everything would go to the screen. Finally convert the buffer to a string and return it.

Once again I appreciate the help. Your post was very informative and I wouldn't of been able to create that string if you hadn't said something.

I've still got more to work on for this particular program although the string was my main concern.

Thank you.

Hopefully I won't post an more questions pertaining to this particular program. haha

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.