?
Solved

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

Posted on 1998-01-06
4
Medium Priority
?
183 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 800 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

770 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