简体   繁体   English

解析数组C ++的内容

[英]Parse contents of array C++

I have this Code: 我有此代码:

cout << "Your Value is: ";

for ( int x = nStart + 1; x < sBuffer.length(); x++ )
          {

              if ( sBuffer[ x ] == ',' )
              {    
                   nStart = x;
                   break;
                   }
              cout << sBuffer[ x ];
          }
// setprecision doesnt work right when used with [] but i try anyway..

       cout << setprecision(2) << sBuffer[ x ];

And my sBuffer[ x ] contains a double value, say, 4.1415679 我的sBuffer [x]包含一个双精度值,例如4.1415679

I want to use setprecision( 2 ) to display only 4.14 我想使用setprecision(2)仅显示4.14

Im stuck, what can i do? 我被困住了,我该怎么办?


it doesn't contain a double because i'm reading a text file, which i forgot to mention sorry. 它不包含双精度字符,因为我正在读取文本文件,但我忘了对不起。

my text file has this format: 我的文本文件具有以下格式:

string, some string, 49.59494, 29.4094944 字符串一些字符串49.59494、29.4094944

storing each Field before a comma into the sBuffer[ x ]. 将逗号之前的每个字段存储到sBuffer [x]中。

so i in fact do have a double. 所以我实际上有一个双。 The reason that i think its not working is that the complier is interpreting it as a string, or char, and not a double value. 我认为它不起作用的原因是编译器将其解释为字符串或char,而不是double值。

right? 对?

Heres my full code feel free to compile in dev or something but be sure to use the text file with this format: 这是我的完整代码,可以随意在dev或类似物中进行编译,但请确保使用以下格式的文本文件:

String,Some String,54.2345,34.6654 ... String,Some String,54.2345,34.6654 ...

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <iomanip>
#include <cstdlib>



using namespace std;


int main()
{
    string usrFileStr,
    fileStr = "test.txt",  // declaring an obj string literal
    sLine,                        // declaring a string obj
    sBuffer;

    int lineCount = 1;

    fstream inFile;                  // declaring a fstream obj
    // cout is the name of the output stream
    cout << "Enter a file: ";
    cin >> usrFileStr;


    inFile.open( usrFileStr.c_str(), ios::in ); 
    // at this point the file is open and we may parse the contents of it

    while ( getline ( inFile, sBuffer ) && !inFile.eof() )
    {
          int nStart = -1 ;
          cout << "First String " << lineCount << " (";
          for ( int x = nStart + 1; x < sBuffer.length(); x++ )
          {
              if ( sBuffer[ x ] == ',' )
              {
                   nStart = x;
                   break;
                   }
              cout << sBuffer[ x ];
          }
          cout << ") ";
          for ( int x = nStart + 1; x < sBuffer.length(); x++ )
          {

              if ( sBuffer[ x ] == ',' )
              {    
                   nStart = x;
                   break;
                   }
              cout << sBuffer[ x ];
          }
          cout << " (First dValue";
          for ( int x = nStart + 1; x < sBuffer.length(); x++ )
          {

              if ( sBuffer[ x ] == ',' )
              {
                   nStart = x;
                   break;
                   }
              cout << setprecision(2) << sBuffer[ x ];
          }
          cout << ", Second dValue: ";
          for ( int x = nStart + 1; x < sBuffer.length(); x++ )
          {
              if ( sBuffer[ x ] == ',' )
              {
                   nStart = x;
                   break;
                   }
              cout << sBuffer[ x ];
          }
          cout << ") \n";
          lineCount++;
    }


    cout << "There are a Total of: " <<  lineCount << " line(s) in the file."
    << endl;


    inFile.clear();           // clear the file of any errors
    inFile.close();  // at this point we are done with the file and we close it

    fgetc( stdin );
    return 0;
}

// use a funnction

Yeah i know i could use a function prototype for the excessive looping(4) But i'd like to get this value issue taken care of first. 是的,我知道我可以使用函数原型进行过多的循环(4),但是我想首先解决这个值问题。

It looks like sBuffer is a string or an array of chars, so when you print sBuffer[x], it's treating it as a character instead of a floating point number. 看起来sBuffer是一个字符串或一个字符数组,因此当您打印sBuffer [x]时,会将其视为字符而不是浮点数。

You need to convert the string representation of the number into a double. 您需要将数字的字符串表示形式转换为双精度形式。 You can use the atof or strtod functions from C, or boost::lexical_cast<double> . 您可以使用C中的atofstrtod函数,也可以使用boost::lexical_cast<double>

It doesn't look like your sBuffer[x] would contain a double, you compare it against ',' after all. 看起来您的sBuffer[x]不会包含双sBuffer[x]您将其与','进行了比较。 So sBuffer is a buffer of characters? 那么sBuffer是字符缓冲区吗? Then sBuffer[x] is just one character of your "4.1415679" and setprecision won't do what you want if you just output this character. 然后sBuffer[x]只是“ 4.1415679”的一个字符,如果仅输出此字符,setprecision将无法执行您想要的操作。

You can use stringstreams to read a double from a string: 您可以使用stringstreams读取字符串的双精度型:

#include <sstream>

istringstream strm("4.1415679");
double d;

if (strm >> d) {
  cout << "Your number: " << setprecision(2) << d << endl;
}
else {
  cout << "Not a number." << endl;
}

If you have the boost libraries installed (always a good idea), you can also use boost::lexical_cast<double>("...") , like Matthew Crumley said. 如果您已经安装了boost库(总是一个好主意),则也可以使用boost::lexical_cast<double>("...") ,就像Matthew Crumley所说的那样。

You probably want std::fixed : 您可能想要std::fixed

cout << fixed << setprecision(2) << sBuffer[ x ];
//      ^^^^^

如果sBuffer是一个char数组,则它不包含double。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM