简体   繁体   中英

How to read a value from a text file and store that value into a temporary variable?

istringstream in("4.2 + 3.4 - 4.1");
ostringstream out;

string sign;
double num;
double tempNum = 0;
double sum = 0;

while (in >> num >> sign) {
    tempNum = num;  

    if (sign == "+") {
        sum = (num + tempNum); 
    }
}
cout << sum << endl;

My Output: 8.4

Correct output: 3.5

I want to store 4.2 into tempNum and read 3.4 into num, so that I can add 8.4 and 3.4 into the sum. However, the problem that I have is whenever it reads 4.2, it assigns 4.2 into num and then it reads 3.4 and assigns 3.4 into tempNum also. How can I assign 4.2 into tempNum and leave 3.4 just in the num variable. So that I can have the sum as 4.2 + 3.4, not 4.2 + 4.2. If the question is not clear, I'd love to explain in more details.

There are multiple problems with the code you have. How to solve the problem as originally stated is already answered. Another problem I have mentioned in a comment. But so far none solves the problem of reading the numbers and "signs" in serial, which I will attempt to show here.

First thing you need to think about is that the input is a sequence of numbers and "signs" all after each other. While you are correct that a loop is needed you also need to think about the actual sequence... You have a number, followed by a sign followed by another number etc.

The easy way to solve this is to think of it a little differently. It's not a sequence starting with a number. Instead think of it as a lonely number, followed by an optional sequence of "signs" and numbers.

Then you could do something like

in >> sum;
while (in >> sign >> num)
{
    if (sign == "+")
    {
        sum += num;
    }
    else if (sign == "-")
    {
        sum -= num;
    }
}

The above loop will handle the case when the input is only a single number. Or arbitrary amount of numbers with + or - in between them.

This will still not be able to properly handle things like multiplication and division though. For that you need another and more complicated approach.

You never read from a stream a second time, just using the same first number twice. After line tempNum = num you probably meant that the new number should be read:

in >> num;

Please note also that step-by-step debug process with opened watches window is extremely good in dealing with this sort of problems.

UP : The whole code looks like this:

istringstream in("4.2 + 3.4");
ostringstream out;

string sign;
double num;
double tempNum = 0;
double sum = 0;

while (in >> num >> sign) {
    tempNum = num;  
    in >> num;
    if (sign == "+") {
        sum = (num + tempNum); 
    }
}
cout << sum << endl;

For the simple case, you can use the stream extraction operator without the while loop. Like so

in >> x >> sign >> y;

Here is the full example

  std::istringstream in("4.2 + 3.4");

  std::string sign;
  double x, y;

  in >> x >> sign >> y;

  std::cout << x << std::endl;
  std::cout << sign << std::endl;
  std::cout << y << std::endl;

  double sum = x + y;

  std::cout << sum << std::endl;

If the input becomes more complex try a while loop like so

  std::istringstream in("4.2 + 3.4 - 4.1");

  std::string temp;
  double op; //for + becomes 1.0 for - becomes -1.0
  double sum = 0;
  in >> sum; //Get the first value
  while (in >> temp) {
    //check for operator
    if (temp == "+") {
      op = 1.0;
    }
    else if (temp == "-") {
      op = -1.0;
    }
    else {
      double next = std::stod(temp,NULL);

      sum += op * next;
    }
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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