Solved

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

Posted on 1998-01-06
4
181 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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 …
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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 be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

688 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