Member Avatar for brazentongue

how can I input a console-entered string into multiple structure variables? I have the following structure:

struct MyTime {int hours, minutes, seconds;};

and need to prompt a user to enter a time in format hours:minutes:seconds and then store that time into the structure directly i.e. I can't input a string and then parse the string into hours, minutes, seconds. So I need a method that reads 1-2 numbers from the stream into MyTime.hours until a ':' is found, and then repeats for MyTime.minutes and so on. Also, I'd prefer a way that does not involve declaring extra variables. Any ideas?

Recommended Answers

All 4 Replies

Something like this should suffice, although you might want something better :

#include <iostream>
#include <sstream>
#include <string>
using namespace std;

struct SimpleTime{
 int hours;
 int minutes;
 int seconds;
 SimpleTime(int h = 0, int m = 0, int s = 0)
  : hours(h), minutes(m), seconds(s) {}
};

//does no error check
SimpleTime praseStrTime(const std::string& timeStr, char delim ){
 string::size_type hourPos = timeStr.find(delim);
 string::size_type minPos = timeStr.find(delim,hourPos+1);

 string hours = timeStr.substr(0,hourPos);
 string minutes = timeStr.substr(hourPos+1,(minPos - hourPos - 1));
 string seconds = timeStr.substr(minPos+1); 
 stringstream stream( hours + " " + minutes + " " + seconds );
 int timeArray[3] = {0};
 int index = 0; 
 while(stream >> timeArray[index++]) continue;
 return SimpleTime(timeArray[0],timeArray[1],timeArray[2]);
}

void printTime(const SimpleTime& t){
 cout << "hours = " << t.hours << endl;
 cout << "minutes = " << t.minutes << endl;
 cout << "seconds = " << t.seconds << endl;
}
int main(){
 string input = "09:12:59"; //hours:minutes:seconds
 printTime( praseStrTime(input,':') );
 return 0;
}
Member Avatar for brazentongue

Something like this should suffice, although you might want something better :

#include <iostream>
#include <sstream>
#include <string>
using namespace std;

struct SimpleTime{
 int hours;
 int minutes;
 int seconds;
 SimpleTime(int h = 0, int m = 0, int s = 0)
  : hours(h), minutes(m), seconds(s) {}
};

//does no error check
SimpleTime praseStrTime(const std::string& timeStr, char delim ){
 string::size_type hourPos = timeStr.find(delim);
 string::size_type minPos = timeStr.find(delim,hourPos+1);

 string hours = timeStr.substr(0,hourPos);
 string minutes = timeStr.substr(hourPos+1,(minPos - hourPos - 1));
 string seconds = timeStr.substr(minPos+1); 
 stringstream stream( hours + " " + minutes + " " + seconds );
 int timeArray[3] = {0};
 int index = 0; 
 while(stream >> timeArray[index++]) continue;
 return SimpleTime(timeArray[0],timeArray[1],timeArray[2]);
}

void printTime(const SimpleTime& t){
 cout << "hours = " << t.hours << endl;
 cout << "minutes = " << t.minutes << endl;
 cout << "seconds = " << t.seconds << endl;
}
int main(){
 string input = "09:12:59"; //hours:minutes:seconds
 printTime( praseStrTime(input,':') );
 return 0;
}

wow, thanks! but that is so far beyond me at this point. will have to try for something a little simpler.

If you know the input is going to be something like, hour:min:sec then you can just
read it from the stream, and discard the ":" character into a variable or something.

int main(){
	int h(0),m(0),s(0)
	char tmp = 0;	
	cin >> h >> tmp >> m >> tmp >> s;
	cout << h << endl << m << endl << s << endl;
}
Member Avatar for brazentongue

yep, this is what I ended up doing. didn't know cin was smart enough to recognize the ':' as a new character. thanks again for the input!

If you know the input is going to be something like, hour:min:sec then you can just
read it from the stream, and discard the ":" character into a variable or something.

int main(){
	int h(0),m(0),s(0)
	char tmp = 0;	
	cin >> h >> tmp >> m >> tmp >> s;
	cout << h << endl << m << endl << s << endl;
}
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.