cin没有从输入文件中获取

时间:2017-09-13 01:48:21

标签: c++ input file-io cin

我有以下代码和输入文件。当试图使用cin时,似乎没有任何东西进入“数字”,程序只是坐在那里直到它被杀死。字符串到poly函数只是将字符串分成两部分。

#include <iostream>
#include <vector>
#include <string>
#include <stdlib.h>

using namespace std;

struct polynomial
{
    float numA;
    float numB;
};

polynomial stringToPoly(string input);

int main(int argc, char * argv[])
{
    vector<polynomial> Numbers;
    string number;
    polynomial polynumber;

    while (cin >> number)
    {                                                                                   
        polynumber = stringToPoly(number);
        Numbers.push_back(polynumber);
    }

}
polynomial stringToPoly (string input)
{
    polynomial polynumber;
    int negswitch = 1;
    string partA, partB;
    int i=0;

    while (input[i]!='x')
    {
        partA += input[i];
        i++
    }   
    i++; // to bypass the 'x' in the binomial

    while (input[i] <= input.size())
    {
         partB+= input[i];
         i++;
    }

    float a = strtof((partA).c_str(),0);
    float b = strtof((partB).c_str(),0);

    polynumber.numA = a;
    polynumber.numB = b;

    return polynumber;
}

以下是命令行给出的输入文件:

  25.4x+5.2
  -32.6x+13.7
  2.6x-9.7
  -16.1x+0     

0 个答案:

没有答案