Hello everyone,
I need help with this code, which does not work properly. It prints fine at the first instant and when I convert it into a double it prints some unknown value. I am trying to get the maximum of all the values in the row. First: I cant figure out how to exclude the first value of the row, which is just a indicator of how many values are in the row (shown here in bold). Second: I cant get the conversion to a float so I can do the comparison. I thank you in advance for helping me out.
Best,
newbie

the test.grf file

"IngWater_HumRcp",3,"FLOAT",0,"mg/kg-d",
1,4,
10,2.657374814e-012,2.658182387e-012,2.658989959e-012,2.659797532e-012,2.660605104e-012,2.661412675e-012,2.662220249e-012,2.663027821e-012,2.663835392e-012,2.664642966e-012,

The code:

#include <stdio.h>
#include <iostream>
#include <string.h>

int main()
{
    char sizeLineInput[40980];
    char *token;
    char c;
    int idx=0;
    char num[15];
    double array[10000], ADD; // Declare an array of 10,000
    int n = 0;
    char * pEnd;


    FILE *SourceFile = fopen ("c:\\Temp\\test.grf","r");
    if (SourceFile != NULL)
    {
       while(fgets(sizeLineInput, 10000, SourceFile)!=NULL)
       {
        token = strstr(sizeLineInput ,"IngWater_HumRcp");
        if (token)
        {
         fgets(sizeLineInput, 10000, SourceFile);
         while((c=fgetc(SourceFile))!='\\')
         {
          if(c==',')
          {
           num[idx]=0;
           n++;
           printf("%s\n",num); // Here it prints right
           array[n]= atof(num);
           //array[n]= strtod (num,&pEnd);
           printf("%d\n", array[n]); // Here it doesnot
           system("PAUSE");
           idx=0;
          }
          else
          {
           num[idx] = c;
           idx++;
          }
         }
        }
       }
    }
for (int i=0;i<n;i++){
if (array[i+1]>array[i]){
ADD=array[i+1];}
else
ADD=array[i];
}
return 0;
printf("%s\n",ADD);
system("PAUSE");
}

Dani AI

Generated

Good diagnosis by — the visible symptom (a sensible string print but a garbage numeric print) usually comes from a format/representation mismatch, but there are a few other bugs in the posted program that commonly produce the same behavior. The key fixes are: (1) use a parsing approach that preserves tokens safely (avoid tiny fixed buffers and fgetc loops unless you check bounds), (2) convert tokens with a routine that reports errors, and (3) fix indexing and flow so you actually compute the maximum and print it before returning.

Two practical points not in the replies:

  • The char num[15] buffer is too small for numbers in exponent notation plus the terminating NUL; that leads to buffer overflow and memory corruption. Use std::string (or increase the buffer and always bounds-check) and only null-terminate where appropriate.
  • The max-finding logic is off by one and the program prints after return (so those lines never run). Initialize your max to the first numeric value (or to -inf) and update it for each parsed value; place return at the end after printing.

A robust, simple C++ approach is to read the numeric line(s) into a std::string, split on commas with std::getline(..., token, ','), skip the first token that holds the count, convert each token with std::stod inside a try/catch (or use strtod and check the end pointer), and update a running maximum. Example approach shown below demonstrates the core flow and safe conversions.

#include <fstream>
#include <sstream>
#include <string>
#include <limits>
#include <iostream>

// read a data line, skip the first token (count), convert remaining tokens with std::stod,
// compute and print the maximum in scientific form

Notes: handle wrapped data lines (the values can be split across multiple lines), trim whitespace from tokens before conversion, and skip empty tokens produced by trailing commas. These precautions prevent subtle runtime and parsing errors that look like conversion problems.

Recommended Answers

All 2 Replies

line 32 is a string in scientific notation. The value is just too small to be represented in normal double precision, so you have to display it in scientific notation, like this: printf("%e\n", array[n]); // Here it doesnot In the value 2.657374814e-012 the -012 tells you to move the decimal point 12 places to the left, which makes the number 0.00000000000265..., a very very tiny number.

Thank you very much. It worked. U did saved me a lot of time, energy and frustration.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.