-2

This is my objective: Develop an adder application in C++ that will either add floating point numbers provided on the command line when the application is run or enter an interactive mode and add floating point numbers provided by the user interactively if no inputs are provided on the command line. Ctrl-Z terminates user input in interactive mode.

When I try to run this and input add, I will get the else if statement, but when I try to type add 1 2 3, I will still get the else if statement instead of the if statement.

What do I do so that it recognizes if I input add 1 2 3 it will make use of the if statement?

One more thing, how do I make multiple inputs in 1 line without it having a limit?

#include <iostream>
#include <sstream>
#include <string>

using namespace std;
int main()
{
    string operation;
    int num1, num2, num3;
    cin >> operation;

    if(operation != "add"){
        cin >> num1 >> num2 >> num3;
        cout << num1+num2+num3;
    } else if (operation =="add") {
    cout << "Type numbers to be added, 1 per line. Ctrl-Z to end input.";
    }


    return 0;
}

I'm trying to achieve this

Frazier
  • 11
  • 3
  • Does this answer your question? [How to read cin with whitespace up until a newline character?](https://stackoverflow.com/questions/1583652/how-to-read-cin-with-whitespace-up-until-a-newline-character) – flyingdutchman Dec 01 '20 at 10:04
  • Could you please clearify what input is valid and what the outcome should be? Is `add 1 2 3` in one valid, is `add` in one line and `1 2 3` in the follow up line valid? Why do you want that `add 1 2 3` uses the first if (the operator is `add`), but the if condition is `operation != "add"`. – t.niese Dec 01 '20 at 10:08
  • "add 1 2 3" is only on 1 line. – Frazier Dec 01 '20 at 10:11
  • `cin >> operation` stops at the first whitespace. If you enter a line with spaces, it will not read the whole line - but you are assuming incorrect it will read the whole line. So entering `add 1 2 3` will read `operation` as `"add"`, and then leave the characters `"1 2 3"` waiting to be read by the next read statement. – Peter Dec 01 '20 at 10:58
  • This doesn't address the question, but `else if (operation == "add")` can be simply `else`. `if (operation != "add")` handles all the other cases. – Pete Becker Dec 01 '20 at 14:48

4 Answers4

3

std::cin reads the input until it meets some separator like or \n, that's why your program behaves as you described.

To get your desired behavior use std::getline.

Like this:

string input;
getline(std::cin, input);

And then split it manually.

Valerii Boldakov
  • 1,751
  • 9
  • 19
1

cin will read from the command line untill it reaches a newline or a space

you need to use cin.getline() as this will get the entire line

0

First - read getline documentation https://en.cppreference.com/w/cpp/string/basic_string/getline

Second - try this code

#include <bits/stdc++.h>

using namespace std;


int main(int, char**) {
    string line;
    char sep = '\n'; // separator
    while (getline(cin, line, sep))
    {
        // handle line
        cout << line;
    }
    return 0;
}
mascai
  • 1,373
  • 1
  • 9
  • 30
0

You can chain cin like this.

#include <iostream>
#include <sstream>
#include <string>
using namespace std;
int main()
{
    string operation;
    int num1, num2, num3;
    cin >> operation >> num1 >> num2 >> num3;   // <-----

    if(operation == "add"){  //  <--------------
        cout << num1+num2+num3;
    } else  {  // <-------
    cout << "Type numbers to be added, 1 per line. Ctrl-Z to end input.";
    }
    return 0;
}
Florent
  • 436
  • 3
  • 8