Click here to Skip to main content
15,905,508 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: array of Strings Pin
convert_sg29-Oct-03 15:48
convert_sg29-Oct-03 15:48 
GeneralRe: array of Strings Pin
vcplusplus29-Oct-03 15:39
vcplusplus29-Oct-03 15:39 
GeneralRe: array of Strings Pin
Christian Graus29-Oct-03 15:48
protectorChristian Graus29-Oct-03 15:48 
GeneralChange font size for CComboBox and CEdit Pin
srev29-Oct-03 14:14
srev29-Oct-03 14:14 
GeneralRe: Change font size for CComboBox and CEdit Pin
Yonatan26-Nov-03 2:04
Yonatan26-Nov-03 2:04 
Generalsimple console input question Pin
rajdawg29-Oct-03 14:09
rajdawg29-Oct-03 14:09 
GeneralRe: simple console input question Pin
Christian Graus29-Oct-03 15:23
protectorChristian Graus29-Oct-03 15:23 
GeneralRe: simple console input question Pin
Christian Graus29-Oct-03 15:47
protectorChristian Graus29-Oct-03 15:47 
Apparently some of my last advice was a little off. I've not done any C++ for a while now. The following is a complete solution that reads a line, then it extracts the numbers from the string, it assumes that only numbers are entered ( which is bad ), but it will only read the number of items you entered as the first digit, but if there is less, it just breaks and does not blow up. You could make it present an error if you'd like.

This is obviously your homework, and I ask you not to use this code unless you first do the research to be able to reproduce it yourself, and understand/explain what it does and how it does it.

<br />
<br />
#include <iostream><br />
#include <vector><br />
#include <string><br />
#include <algorithm><br />
<br />
using std::cin;<br />
using std::cout;<br />
using std::vector;<br />
using std::string;<br />
<br />
int _tmain(int argc, _TCHAR* argv[])<br />
{<br />
	string s;<br />
	int nCount;<br />
	int n;<br />
<br />
        // Read the entire line into a string, cin >> stops on a space<br />
	std::getline(cin, s);<br />
<br />
	int nPos = s.find(' ', 0);<br />
 <br />
        // extract the count<br />
	nCount = ::atoi(s.substr(0, nPos).c_str());<br />
<br />
	vector<int> vecInt;<br />
<br />
        // nStart will hold the position past the last space found ( i.e. the start of the next number )<br />
	int nStart = nPos + 1;<br />
<br />
	for (int i = 0; i < nCount; ++i)<br />
	{<br />
		nPos = s.find(' ', nStart);<br />
<br />
                // nPos will be -1 when no space is found.  I've not tested this with trailing spaces<br />
		if (nPos > 0)<br />
		{<br />
			vecInt.push_back(::atoi(s.substr(nStart, nPos).c_str()));<br />
			nStart = nPos + 1;<br />
		}<br />
		else<br />
		{<br />
                        // Grab the last number in the sequence and break.<br />
                        // The only reason to persist with the for loop is the requirement for a count to<br />
                        // be provided at all, and it does mean that if more numbers than the count are <br />
                        // input, they will be ignored.<br />
			vecInt.push_back(::atoi(s.substr(nStart, s.size()).c_str()));<br />
			break;<br />
		}<br />
	}<br />
<br />
        // The rest of this code is just about repeating the sequence to ensure it was input properly<br />
        // This could be done in one line using an adapter to pipe everything to cout via for_each,<br />
        // but I'm on work time and couldn't be bothered looking it up.  Man, I miss C++ though.....<br />
	std::vector<int>::iterator first = vecInt.begin();<br />
	std::vector<int>::iterator end = vecInt.end();<br />
<br />
	bool bFirst = true;<br />
<br />
	for(;first != end; ++first)<br />
	{<br />
		if (!bFirst) cout << ", ";<br />
		cout << *first;<br />
		bFirst = false;<br />
	}<br />
<br />
	cin >> s;<br />
<br />
	return 0;<br />
}<br />
<br />


Christian

I have drunk the cool-aid and found it wan and bitter. - Chris Maunder
GeneralRe: simple console input question Pin
rajdawg30-Oct-03 1:08
rajdawg30-Oct-03 1:08 
GeneralLayered Service Provider (LSP) Pin
catmust29-Oct-03 13:55
catmust29-Oct-03 13:55 
GeneralRe: Layered Service Provider (LSP) Pin
Alexander M.,30-Oct-03 2:49
Alexander M.,30-Oct-03 2:49 
GeneralRe: Layered Service Provider (LSP) Pin
catmust30-Oct-03 8:55
catmust30-Oct-03 8:55 
GeneralLoading dll issue during debuging Pin
catmust29-Oct-03 13:48
catmust29-Oct-03 13:48 
Generalmemory allocation issues Pin
Steven M Hunt29-Oct-03 13:21
Steven M Hunt29-Oct-03 13:21 
GeneralRe: memory allocation issues Pin
Christian Graus29-Oct-03 15:52
protectorChristian Graus29-Oct-03 15:52 
Generalsockets and threads Pin
michael_cowan29-Oct-03 13:14
michael_cowan29-Oct-03 13:14 
GeneralWINS Pin
Jair29-Oct-03 13:06
Jair29-Oct-03 13:06 
GeneralRe: WINS Pin
Jair1-Nov-03 3:29
Jair1-Nov-03 3:29 
QuestionXP GetSysColor()??? Pin
alex.barylski29-Oct-03 12:17
alex.barylski29-Oct-03 12:17 
AnswerRe: XP GetSysColor()??? Pin
michael_cowan29-Oct-03 13:00
michael_cowan29-Oct-03 13:00 
GeneralRe: XP GetSysColor()??? Pin
alex.barylski29-Oct-03 15:17
alex.barylski29-Oct-03 15:17 
GeneralCalling a dialog from a dll question Pin
Steve Messer29-Oct-03 11:53
Steve Messer29-Oct-03 11:53 
GeneralRe: Calling a dialog from a dll question Pin
igor196029-Oct-03 12:49
igor196029-Oct-03 12:49 
GeneralRe: Calling a dialog from a dll question Pin
Steve Messer29-Oct-03 14:35
Steve Messer29-Oct-03 14:35 
GeneralProblem with control arrays Pin
Stephan Poirier29-Oct-03 11:49
Stephan Poirier29-Oct-03 11:49 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.