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
Solved

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

Posted on 1998-01-06
4
179 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VS2015 compilation and missing DLLs 9 163
sorting efficency of sorting algorithm 30 121
Unable to start eclipse ? 17 154
Android development question 2 54
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
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 goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

828 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