Solved

Need help fast! char MG[3]; MG="400?due" ?

Posted on 1998-01-06
4
175 Views
Last Modified: 2010-04-10
This is the function I am having problem with:

void Les_Inn_Verdier()
{
      char InnBuffer[100];
      unsigned int count=100;
      int fhInn;
        fhInn = _open( "f:\\diplom\\flysim.dat", _O_RDWR |           _O_CREAT, _S_IREAD | _S_IWRITE);
      _read( fhInn, InnBuffer, count );
      _close( fhInn );
      char MG[3];
      char MA[5],MP[4];
      MG[0]=InnBuffer[0];
      MG[1]=InnBuffer[1];
      MG[2]=InnBuffer[2];
      Max_Groundspeed = atof(MG);
      MA[0]=InnBuffer[3];
      MA[1]=InnBuffer[4];
      MA[2]=InnBuffer[5];
      MA[3]=InnBuffer[6];
      MA[4]=InnBuffer[7];
      Max_Altitude = atof(MA);
      MP[0]=InnBuffer[8];
      MP[1]=InnBuffer[9];
      MP[2]=InnBuffer[10];
      MP[3]=InnBuffer[11];
      Max_Pitch = atof(MP);
}

With my buffer, called InnBuffer like this:
InnBuffer = "400;30000;45.0;"
I get this result:
MG = "400?due"
MA = "30000ye"
MP = "45.00210002,d"
I had expectet MG = "400", MA="30000" and MP = "45.0", but that was clearly wishful thinking. I have no idea what can be wrong and really hope someone can help me with this as fast as possible.

0
Comment
Question by:Krafer
  • 3
4 Comments
 
LVL 22

Accepted Solution

by:
nietod earned 200 total points
ID: 1177654
answer comming
0
 
LVL 22

Expert Comment

by:nietod
ID: 1177655
The problem is that your strings are not NUL termianted.  C (and C++) uses a NUL character (a byte with the value 0) to terminate a string, that is, to indicate the end of the string.  You need to declare your strings with enough space to have the terminator and you need to set the terminator.  You need to do something like.

char MG[4]; // room for 3 characters and a terminator.
MG[0]=InnBuffer[0];
MG[1]=InnBuffer[1];
MG[2]=InnBuffer[2];
MG[3]=0; // terminate.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1177656
Same sort of logic applies to the other strings (character arrays).

Hope that is fast enough.
0
 

Author Comment

by:Krafer
ID: 1177657
That was impressingly fast! And of course the answer was correct. I was just about to start working my way around the problem, and that would probably have taken hours.

The chances that I get this diploma finished in time has increased.

Thanks!

Norwegian student

0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now