0

I am trying to make a code for one of the problems on an online coding platform. I have already made the code, but cannot get the input in the desired format. I need to take an input which has three integers (say n1, n2 and m). The question says that the input can be numbers separated by white spaces. I tried looking for help and even found a way, but it isn't working.

Here is my code:

#include <string>
#include <ctype.h>
#include <iostream>
#include <conio.h>
#include <stdlib.h>
using namespace std;

int main()
{
    int t;
    cout << "Enter the number of test cases" << endl;
    cin >> t;
    do
    {
        string rawInput;
        int isNum, n1, n2, m, t, i, j;
        int arr[3];
        arr[0] = 0;
        arr[1] = 0;
        arr[2] = 0;
        cout << "Enter a number, or numbers separated by a space, between 1 and 1000." << endl;
        while (getline(cin, rawInput))
        {
            cout << "Rawinput" << rawInput.length();
            for (i = 0; i < rawInput.length(); ++i)
            {
                cout << rawInput[i];
            }
            int j = 0, k = 0;
            for (int j; j < rawInput.length(); ++j)
            {
                isNum = isdigit(rawInput[j]);
                if (isNum)
                {
                    arr[k] = arr[k] * 10 + atoi(rawInput[j]);
                }
                else
                    k = k++;
            }
            cout << "I am Array" << endl;
            for (int l = 0; l < 3; l++)
                cout << arr[l] << endl;
        }
        if (arr[0] >= arr[2] && arr[1] >= arr[2])
        {
            for (int i = 1; i <= arr[2]; i++)
            {
                if (arr[0] >= i && arr[1] >= i)
                {
                    arr[0] = arr[0] - i;
                    arr[1] = arr[1] - i;
                }
            }
        }
        cout << arr[1] + arr[0];
        t--;
    } while (t > 0);
}

Specifically the function atoi doesn't seem to work. I tried using stoi, but even that isn't working.

user4581301
  • 33,082
  • 7
  • 33
  • 54
Sahai
  • 33
  • 1
  • 8
  • 1
    That... is a lot of code. Please try to create a [mcve], it makes it a lot easier to diagnose errors, and maybe you'll find them yourself while writing it. – hlt Dec 07 '15 at 19:59
  • Also, check out [this](http://stackoverflow.com/a/18786719/1106415) answer, because you're mixing `getline` with `>>` – hlt Dec 07 '15 at 20:01
  • Off topic: `atoi(rawInput[j]);` consider instead `rawInput[j] - '0';` `atoi` wants a pointer to an array of characters. You are giving it only one character. And so much for staying off topic. Sorry. – user4581301 Dec 07 '15 at 20:04
  • As @user4581301 said, try the other way as well. Typically `atoi` is used for converting command line arguments passed to a program, not user input. – m_callens Dec 07 '15 at 20:06
  • `k = k++;` would be redundant if it weren't undefined behaviour. Crom only knows whether `k=k` or `k++` is going to execute first. `k++` is sufficient if you want `k` to be increased by 1 (AKA `k= k+1;`) – user4581301 Dec 07 '15 at 20:12
  • Multiple variable `j`s are defined. `int isNum, n1, n2, m, t, i, j;` looks to be unused. `int j = 0, k = 0;` also unused. `for (int j; j < rawInput.length(); ++j)` is used, but not initialized so it's starting value could be anything resulting in unpredictable loop behaviour. – user4581301 Dec 07 '15 at 20:17

1 Answers1

2

If you are simply trying to collect a series of integers separated by spaces as user input..ie 1 2 3 4 5, you don't have to user the getline method.

You can redo the while loop for a condition like this:

int input;

while (cin >> input)
{
    <<HANDLE INPUT>>
}

This is drastically reduce the line parsing you are trying to do and will capture the next inputted integer on that line as long as there is one to take. The loop iteration will go like this with the same series above...

Loop #      Input Taken
1           1
2           2
3           3
...         ...

This way there is no parsing necessary as it will handle ONE integer input per iteration.

m_callens
  • 6,100
  • 8
  • 32
  • 54