|
Ah! That's it - it's a two-character sequence, and you're streaming into single character variables. It imagine what's happening is the second character is buffered and then read as the third input. Try it with just single character entries and I bet it'll work.
|
|
|
|
|
Yeah, I changed char to short and it worked. I wish there were a simple byte primitive like C# has.
|
|
|
|
|
Well, there is, and char is (god, I'm rusty on stuff like this) typedef'd to a byte - it IS a byte. And that was the trouble - the variables only allowed for one byte of storage, so it stuck one byte into each, not realizing you'd intended to enter a multi-byte response into a single variable.
Your fix is appropriate, methinks - the variables probably should have been shorts or ints to begin with.

|
|
|
|
|
There is an __int8 primitive in VC++, its just not standard C++. cin handles it the same way as it does with char. There should be a way to enter a value of up to 255 in the console window and have it convert that string into its byte representation like .NET's Byte.Parse("255");
|
|
|
|
|
I wouldn't be surprised if there's a way to do it; someone else might know how. I'm afraid I'm recovering from years of losing brain cells to Java. Totally ruins ones C++ skills, it does.

|
|
|
|
|
Hi Captain,
There is absolutely nothing C# can do that C++ cannot do. Your sample code is flawed. You are trying to put multiple bytes into a 1 byte container.
CaptainSeeSharp wrote: There should be a way to enter a value of up to 255 in the console window and have it convert that string into its byte representation like .NET's Byte.Parse("255");
Like this?
string magic_SeeSharpBuffer;
cout << "Enter a byte:";
cin >> magic_SeeSharpBuffer;
BYTE b = atoi(magic_SeeSharpBuffer.c_str());
The design and theory of C# and .NET languages somewhat has roots in OLE/COM technologies. Your C# datatypes are similar to VARIANTS[^] where the datatype can be interchanged. The .NET languages discourage direct memory access and direct data conversions by the end-user. The .NET internel conversion code was written by an engineer at Microsoft and you rely on the IL instructions generated by him/her.
The C++ language gives the engineer more control over the datatype and memory. The C++ language trusts that you know what you are doing. It will allow you to write code that will blow-up and crash.
The Microsoft C++ compiler *could* detect these situations and throw a compile-time warning or error. But believe it or not sometimes C++ software developers *want* to overflow a buffer or overwrite memory. Thats exactly what makes the language so powerful. Its greatest strength and at the same time its weakness.
I am happy that you are experimenting with the C++ language. Don't get frustrated because eventually you will understand these fundemental concepts and will be a better software engineer because of it.
Best Wishes,
-David Delaune
|
|
|
|
|
Hey, this is my first time posting anything on this forum. I need some help with this reoccurring problem. When I try to typecast a character pointer to a LPCSTR for some reason all of the string doesn't make it. When I try to pass it to a function that uses a LPCSTR it gets clipped at the whitespace. So if I use something like "This is a test." the only thing that seems to make it to the function is "This". I've noticed this with MessageBox and the send winsock function. So I guess it may be a const char* to char* problem but anyway the point is I have no clue whats going on and this has been bugging me. Below I left some code I was messing with. This is probably something simple that I'm missing but any help would be gratefully appreciated.
#include <iostream>
#include <windows.h>
using namespace std;
int main()
{
char MyString[255];
scanf("%s",MyString);
MessageBox(NULL,LPCSTR(MyString),"Test",MB_OK | MB_ICONINFORMATION);
return 0;
}
|
|
|
|
|
From Format Specification Fields: scanf and wscanf Functions[^] :
An input field is defined as all characters up to the first white-space character (space, tab, or newline), or up to the first character that cannot be converted according to the format specification, or until the field width (if specified) is reached. If there are too many arguments for the given specifications, the extra arguments are evaluated but ignored.
Setting a breakpoint on the MessageBox call would allow you to examine the contents of MyString before it's displayed, where you can see it's already truncated.
A couple additional points. My C is a little rusty, but ..
a) Your 'LPCSTR(MyString)' cast is unnecessary.
b) It's not a cast, but rather (I believe) an invocation of a LPCSTR constructor with MyString as a parameter. You're not executing a cast, you're creating a new null-terminated string pointer, pointed at MyString.
c) A cast would have been '(LPCSTR)MyString'. But like I said, it isn't required here.
modified on Saturday, October 31, 2009 12:52 AM
|
|
|
|
|
Oh ok I see what your saying. I find it kinda hard to believe I missed that in all the time I have spent using C++ Thanks for the help and explanation.
|
|
|
|
|
It's definitely incorrect syntax for a cast, and a cast was definitely unnecessary; my only doubt is whether or not it executes a constructor the way you'd written it. Like I said, I'm a bit rusty.

|
|
|
|
|
try this -
scanf("%[^\n]", MyString);
|
|
|
|
|
Hello, in my application there are 864 variables associated to each control(edit boxes and radio buttons), when the application is started, all this variables must to be updated from a file. So it's a lot of work to type all this code, my question is: is there any possibility to use a matrix (in this case will be 3 or more) in which each element will be a variable of each control? how can i do this?
|
|
|
|
|
Create a structure and put all the variables or references/pointers to the variables in the structure.
Now you can read the entire file into this structure in a single read.
You will still have to type in this code unless you have a great speech recognition engine that can understand and translate what you speak or better still, some AI module that can type what you think.
|
|
|
|
|
i m facing problem in linking of dll file of ACR-View in VC++, the template to be used is MFC. kindly list step by step procedure.ACR-View can be downloaded from http://www.parkermotion.com/scripts/support_downloads.asp, plz help
|
|
|
|
|
rahul1167 wrote: kindly list step by step procedure
Sorry, but that is not how it works.
Please describe what you have done - including any relevant code, and what is not working - including any error messages. Also note that if you are using a third party product (I assume ACR-View is third party) then you should try the producer web-site for help in the first instance.
Please also read the guidelines at the top of this page.
|
|
|
|
|
rahul1167 wrote: kindly list step by step procedure.
Of what? Are you wanting to implicitly link to the DLL (via the LIB file), or explicitly link (via LoadLibrary() ) to it?
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
|
|
|
|
|
I need to use the functions of ACR-View for motion control, It is a software provided by parker automation for motion control. I am new to programming so i don't know what kind of linking is required, plz help if possible suggest ways for both type of linking I will try
|
|
|
|
|
rahul1167 wrote: I need to use the functions of ACR-View for motion control, It is a software provided by parker automation for motion control.
Isn't there any kind of documentation provided with the dll ? Reading this documentation would obviously be the first step...
|
|
|
|
|
rahul1167 wrote: I am new to programming so i don't know what kind of linking is required...
It all depends on which of the DLL, LIB, and H files you have.
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
|
|
|
|
|
Hi all,
I am using CComBSTR as a parameter by reference in a WebService method ,
this variable is supposed to fetch data from Webservice,But if huge buffer data is there then data is currepting. I mean I am getting incomplete buffer.
lease help me.
Thanks & Regards,
|
|
|
|
|
Is there any solution for the above problem..
Please help me.
|
|
|
|
|
Hello:
My application (C++ / MFC) uses the the microsoft Jet engine and the DAO classes to read and write microsoft access files.
Under Windows-7 the Jet engine is not available (except in XPMode).
Is there a quick and easy way to allow my application to continue reading and writing access files without the use of the microsoft jet engine? Do I need to remove the DAO classes and completely rewrite this part of the application?
Thanks
BobVal
|
|
|
|
|
Hi,
Im looking for a bit of guidance on this. I have a project for college for network design. It is to implement a C program for the breadth first search algorithm.
I can program in c but this is really confusing me, iv seen examples but they are hard to follow. Please see the brief below. Just need a push in the right direction to get started not the whole thing thanks.
BRIEF:
Implement Moores + Breadth First Search algorithm
-Input a list of edges in the format
{A, B}
-Where A, B are integers.
-The application should return
* Shortest path between a pair of user selected nodes.
* A spanning tree for the network.
Estimate the performance of the algorithm as a function of no of edges in the input list.
(I.e. time no of top level operations, memory usage etc)
|
|
|
|
|
|
I agree that the web is a good source of information on this issue. For example, you could look at the following URL: http://en.wikipedia.org/wiki/Dijkstra's_algorithm
There are several text books that discuses this issue as well. One book that comes to mind is "Introduction to Algorithms" by Cormen, Leiserson, Rivest and Stein. This book is not particular easy to follow but it does a very good job of analyzing the running time of the algorithms it discuses.
Bob
|
|
|
|