|
Oops. Ta for that correction.
Steve
|
|
|
|
|
So I'm trying to sort each value from a files. A user inputs a string of 8 numerals. then breaks the string down to an array and writes each element from the array to a file on each line.
So if the user inputs: 87654321
Its output in the file will be:
8
7
6
5
4
3
2
1
What I want to do is get the first integer of the file, compare it to the second and write to a new file the numbers in ascending order:
1
2
3
4
5
6
7
8
Now, I am aware of the sort() func, and would like to avoid it as much as possible. I'm trying to use the if/for/while to accomplish this task. Below is my code of what I have so far:
<code>
// fstsreamCompare.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <fstream>
#include <iostream>
#include <conio.h>
#include <string>
using namespace std;
int v;
int loop;
int aA[8];
string ns;
int _tmain(int argc, _TCHAR* argv[])
{
ofstream ouI;
ouI.open("rwB.dat");
if( ouI.fail()){
cout << "Error reading file: " << endl;
exit(1);}
ifstream inI;
inI.open("rwB.dat");
if( inI.fail()){
cout << "Error writing file: " << endl;
exit(1);}
do {
cout << "File(SORT) Input an 8-Digit numeral: "; cin >> ns;
if ( ns.length() < 8 )
{
cout << "Human Error! " << "The value is too short!" << endl;
cout << "The value: " << ns << " is " << ns.length() << " digits long." << endl;
cout << "Press any key to re-enter a valid numeral." << endl;
_getch();
system("CLS");}
else if ( ns.length() == 8)
{
aA[0] = ns[0] - '0';
aA[1] = ns[1] - '0';
aA[2] = ns[2] - '0';
aA[3] = ns[3] - '0';
aA[4] = ns[4] - '0';
aA[5] = ns[5] - '0';
aA[6] = ns[6] - '0';
aA[7] = ns[7] - '0';
for ( v=0; v<ns.length(); v++ )
ouI << aA[v] << endl;
}
} while ( ns.length() < 8 ); system("CLS");
cout << endl << "Values have been written to FILE(SORT)" << endl;
cout << "Press any key to sort the values." << endl << endl;
_getch();
return 0;
}
</code>
And thanks to David Crow I know how to convert strings to arrays .
I know how to get the first integer from the file but don't know how to get the second to begin comparing.
Keep in mind this is not a school project this is for my leisure.
|
|
|
|
|
I'd do something like this -- but I'd use the std::sort function!
-----------------------------------------------------------------
// Console.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <iterator>
#include <stdexcept>
using namespace std;
inline int CharToNum(char ch)
{
// Check for a bad digit.
if (ch<'0' || ch>'9')
{
throw range_error("Non-digit encountered!");
}
return ch-'0';
}
void main()
{
// Get a string of numbers from the user.
string s;
cout << "Enter a string of chars: ";
cin >> s;
// Create an array of numbers from the string -- each digit is one entry.
vector<int> nums;
try
{
transform(s.begin(), s.end(), back_inserter(nums), CharToNum);
}
catch (const range_error &e)
{
cout << e.what() << endl;
return;
}
// Sort the numbers.
sort(nums.begin(), nums.end());
// Output the results.
copy(nums.begin(), nums.end(), ostream_iterator<int>(cout, "\n"));
}
Steve
|
|
|
|
|
hi
#include "stdafx.h"
#include <fstream>
#include <iostream>
#include <conio.h>
#include <string>
using namespace std;
int v;
int loop;
int aA[8];
string ns;
int _tmain(int argc, _TCHAR* argv[])
{
ofstream ouI;
ouI.open("rwB.dat");
if( ouI.fail()){
cout << "Error reading file: " << endl;
exit(1);}
do {
cout << "File(SORT) Input an 8-Digit numeral: "; cin >> ns;
if ( ns.length() < 8 )
{
cout << "Human Error! " << "The value is too short!" << endl;
cout << "The value: " << ns << " is " << ns.length() << " digits long." << endl;
cout << "Press any key to re-enter a valid numeral." << endl;
_getch();
system("CLS");}
else if ( ns.length() == 8)
{
aA[0] = ns[0] - '0';
aA[1] = ns[1] - '0';
aA[2] = ns[2] - '0';
aA[3] = ns[3] - '0';
aA[4] = ns[4] - '0';
aA[5] = ns[5] - '0';
aA[6] = ns[6] - '0';
aA[7] = ns[7] - '0';
for ( v=0; v<ns.length(); v++="" )
="" oui="" <<="" aa[v]="" endl;=""
="" }
="" }="" while="" (="" ns.length()="" <="" 8="" );="" system("cls");
="" oui.close();="" cout="" endl="" "values="" have="" been="" written="" to="" file(sort)"="" endl;
="" "press="" any="" key="" sort="" the="" values."="" ifstream="" ini;
="" ini.open("rwb.dat");
="" if(="" ini.fail()){
="" "error="" writing="" file:="" "="" exit(1);}
="" char="" buff;
="" int="" aarray[8];
="" for(="" iloop="0" ;="" iloop<8;iloop++)
="" {
="" ini.getline(&buff,2,'\n');
="" aarray[iloop]="buff-'0';
" ini.close();
="" <b=""> print unsorted data but from the file
for( int iLoop=0 ; iLoop<8;iLoop++)
{
cout<
|
|
|
|
|
Herboren wrote: And thanks to David Crow I know how to convert strings to arrays .
Although it was not C++.
It looks as though all you need to do is sort the numbers in ascending order before the for (v=0; ...) loop writes them to the file. That is, unless you actually want the original numbers in a file beforehand.
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
Thats a good point, I'm going to give that a go. gimme a sec...
|
|
|
|
|
hello,
i am using c++ for 2 years but, still dont know what is exacly difference between macro, pragma(directives) and inline?
could someone explain me?
thanks
|
|
|
|
|
|
The va_copy() (function/macro) doesn't seem to be in my Visual Studio 2005; or at least I'm getting an "identifier not found" error. Perhaps I need to point my include directory differently???
Failing that, does anybody know a reliable definition for it? I've searched MSDN and didn't find anything.
David
|
|
|
|
|
I don't know what it does, but maybe you need to implement your own
using va_arg, va_end, va_start[^]
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
If all else fails, you could roll your own, something like:
#define va_copy(dest, src) ((void)((dest) = (src)))
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
Thanks for the thought. Unfortunately, it's not that easy. These functions are compiler supplied for a good reason; they're inherently low-level and depend strongly on how the compiler pushes and pops the stack, alignment issues, and perhaps other things. I have seen one implementation where va_copy(dest, src) was a simple assignment of src to dest. But, on the other hand, if you've looked at Microsoft's implementation of va_start() etc. you know that a simple assignment may be WAY to naive. It MIGHT work, but then again, it might not. Or worse yet, it might work SOMETIMES.
If someone out there KNOWS how it should be implemented in Visual Studio, I'll certainly try it, but I hate to just guess.
David
|
|
|
|
|
Having a the processID how to get the application instance (HINSTANCE) ?
- or how in general people get the HINSTANCE for an applicaytion ?
-----
here is what I'm trying to do and have some issues:
If I'm in my current app and call AfxGetInstanceHandle(), then the result pass it to a function that parses this conforming to the PE headers structures, and changes some PE imports it is ok.
If I want to do the same for arbitrary process after I do OpenProcess() for a processID and pass that handle to the same function that will change some PE imports it fails because it doesn't see it as
a correct PE header.
Probably a more correct question will be: how to change the imports for current running notepads proceses ? I want to make them call a custom function instead the "GetSaveFileName()" API.
I'm stuck here.
please help.
|
|
|
|
|
HANDLE WINAPI OpenProcess(
DWORD dwDesiredAccess,
BOOL bInheritHandle,
DWORD dwProcessId
);
Maxwell Chen
|
|
|
|
|
As I've mentioned here in earlier posts, I've been writing a set of custom controls. They're finished for the most part, but there is one control left to do that has me stumped. I need a custom ComboBox.
The top of the combobox is an image. That is to say, the part of the combobox that displays the current selection as well as the down arrow that you click on to make a selection is a PNG image. What I need to figure out is how to provide the rest of the combobox functionality, specifically, the drop-down part.
The drop-down part doesn't have to be skinable. So I was thinking about using a standard list box. When the user clicks on the combobox's down arrow, a list box appears beneath it. Once the user selects an item, the list box is hidden. If the list box loses focus at any time, it is also hidden.
I've yet to try this, but it seems to be an ok solution. However, I'm looking for better ideas, so I thought I would ask here. Thanks.
[EDIT]
I think I found a better solution. I can subclass the CComboBox class making sure to leave out the WS_VISIBLE style. That way the combo box is invisible. I'll provide the top part image as described above. When the user clicks the down arrow on my image, I'll call ShowDropDown to show only the drop-down list. This seems to give me what I want.
[/EDIT]
-- modified at 13:17 Tuesday 2nd October, 2007
|
|
|
|
|
Leslie Sanford wrote: I can subclass the CComboBox class making sure to leave out the WS_VISIBLE style.
Do you still get paint and mouse messages when the window is not visible?
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Mark Salsbery wrote: Do you still get paint and mouse messages when the window is not visible?
Nope. I just found this out.
What I'm currently testing is creating a custom control derived from CWnd. It has as it's member a CComboBox. The combobox is initialized to have the same dimensions as the custom control. Also, it's created without the WS_VISIBLE style. The custom control does the hit detection with the left mouse button to determine when to show the drop-down list.
This is working so far, but I've yet to wire up the combobox to the custom control so that it propogates selection change events. This may or may not be hard. I'm still refamiliarizing myself with MFC controls.
[EDIT]
Yay! The above approach works. I've got it all wired together. Frankly, it feels like a bit of a kludge, but it's the best approach I can think of other than rewriting the CComboBox from scratch.
[/EDIT]
[EDIT2]
Well, this has turned out to be too kludgey for my tastes. I've found a simpler way. I have a pop-up menu appear below the bitmap representing the combobox header. This works nice and smoothly. And I don't feel like I'm trying to shoe-horn a control into doing what I want it to do.
[/EDIT2]
-- modified at 16:37 Tuesday 2nd October, 2007
|
|
|
|
|
Hi, im a newbe in C++,i only worked whit the old C. Im trying to learn Object Oriented technics.
And now i got stuck in a problem. My prupose is to create a software whit a lot of mathematical functions, so i will explain my problem whit a diferent example but simplest:
supose that i have an abstract class named Form and this class has an pure virtual method named area, cause the area depends of the type of the form. So i'm going to create many derivated classes, for example the triangle, square, and circle; So far so good;
but now;
I want to have an other class to manipulate graphics; so in this class will have an container for forms
for example:
std::vector<form*> Forms; because we can't instanciate the abstrac classes we have to work whit references,wrigth?
how can i add a method to the graphics class to add an triangle whitou declaring an array of triangles and the pass the reference to the vector.
If this can't be done, what is the prupose of the abstract classes: confused:
|
|
|
|
|
Apstolo wrote: how can i add a method to the graphics class to add an triangle whitou declaring an array of triangles and the pass the reference to the vector.
You can simply do that:
Form* pTriangle = new triangle;<br />
...<br />
...<br />
...<br />
Form.push_back(pTriangle);
As triangle inherits from Form, it is a Form. That's the principle of polymorphism.
BTW, I'm think Form is not a correct word, you should prefer Shape instead (not sure but I never heard this word used for such purpose, except in french ).
|
|
|
|
|
Cédric,
Yes you are right, i mean Shape instead of Form, i'm portuguese and shape is Forma, and it was a inconscious association, lol.
But you dind't answerd to my question. see this example:
....
#include "Triangle"
#include "Square"
Class X{
....
....
std::vector<shape*> Shapes;
....
}
void X::addTriangle(..parameters..)
{
Triangle aux(..parameters..);
Shapes.push_back(&aux);
}
--------------------
The problem is that wen the method ends it destroys the aux and the reference/adress is lost
and then is an obvius error when i will try to access that adress.
The only way tath a see to resolve is to have an array of triangles, sqares, circles, etc..
an then add the aux to that array, and then push that adress to the Shapes vector, so it's redundant and usless to have the abstract class.
|
|
|
|
|
Apstolo wrote: But you dind't answerd to my question.
Yes I did, I even gave you a snippet of code
Apstolo wrote: The problem is that wen the method ends it destroys the aux and the reference/adress is lost
and then is an obvius error when i will try to access that adress.
That's why you should use a pointer and create the object with new (like I showed in my example). But don't forget to delete the objects when you empty your container.
The reason is because the object is local to your function and will be destroyed when the function exits.
Apstolo wrote: The only way tath a see to resolve is to have an array of triangles, sqares, circles, etc..
an then add the aux to that array, and then push that adress to the Shapes vector, so it's redundant and usless to have the abstract class.
That won't change anything, the problem will be the same.
Sorry, I misread your explanation. Anyway that is not the best way to do that. It is much better to manage yourself the lifetime of your object using new/delete in that case.
-- modified at 10:42 Tuesday 2nd October, 2007
|
|
|
|
|
Tanks Cédric,
It works!!!!
when i first wrote your code, it was almost equal to my way to create and pass the Triangle-Shape, so i belived that you didn't understod my question, and i did a quick example to test your example , and i got an error, so i assumed that it was the error i expected. after your last post, i retryed to do the example and it was another line code that trigged the error.
I'm realy sory. But now it is working.
you was very helpfull!!!!
|
|
|
|
|
If I am debugging a (32-bit) app using VC6 on Vista 64-bit, and I break out, then the app I am debugging is left running, despite the debugger ending the debugging (apparently successfully). The process appears in task manager, but attempts to kill it simply do nothing (no error, just nothing). The app is locked, so I can't rebuild it, so it's definately loaded into memory somehere. The only way to get out of the situation is to close my VC and re-start it.
Anyone else having this problem? Any one know a fix?
Paul.
"The way of a fool seems right to him, but a wise man listens to advice" - Proverbs 12:15 (NIV)
|
|
|
|
|
Hi,
I'm modifying a property sheet that has two pages, but also has a view window that displays a graphic based on the data the user is editing. I'd like what is drawn to depend on the current active tab, but I'm not sure how to do this. Is there some sort of tab switching event that my sheet can respond to?
|
|
|
|
|
Have you looked at PSN_ messages?
- NS -
|
|
|
|
|