0

So I'm doing acm programming practice problems and I'm always stuck on getting input from user. I'm using C++. So I need to read input from user and it could be multiple lines or a single line we don't know. So if the input is as follow:

2

1 2 3 4 5

2 2 2

The first line represent the number of games that the user play and the lines follow are the scores. The end of each game will be terminated by a newline. I need to read those lines and store it somewhere. How do I go about doing that? I've looked on websites and it seems like most people use scanf or cin or getline but I have no clue what those does.

Another example:

12 21

13 43

1 4

A C

0 0

Each line will contain the two number I need to add, separated by whitespace. When I see two zeros, the input is done. How do I read these and check if it's 0 0? I tried something like:

string num1;
while (true) {
    getline(cin,num1);
    if (num1.empty()) {
        break;
    }
}

But it didn't work. Please help I think I know how to solve the problem but I can't the point of getting input from user. Thanks

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
user2649966
  • 57
  • 1
  • 1
  • 4
  • 1
    What you tried "didn't work"? How did it "not work"? Did you get any error messages? What the value not what you expected? – Chad Feb 05 '14 at 16:09
  • 'most people use scanf or cin or getline but I have no clue what those does' - try googling for them? http://www.cplusplus.com/reference/cstdio/scanf/ http://www.cplusplus.com/reference/iostream/cin/ http://www.cplusplus.com/reference/string/string/getline/ – Graeme Feb 05 '14 at 16:14
  • You should consider getting a decent [book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – molbdnilo Feb 05 '14 at 16:18
  • Well the input ends with newline. For the code above I have to have two newlines to end the input. – user2649966 Feb 05 '14 at 18:33
  • I you want the _idea_ here you are: if every input of the same structure - create such `structure` or `class`, define `operator>>` (for input) and/or `operator<<` (for output). Also take a look at `stringstream` and as you've already figured out `getline`. Here is [reference](http://en.cppreference.com/) – Yuriy Ivaskevych Jan 24 '17 at 14:58

3 Answers3

0

Well, I learned this from doing online challenges and this is the way I do this when necessary.

First case, I think you have to declare N cases, so I have:

int N;

Then, we need multiple numbers in each case, let's say 3. So I'll do this:

int score1[N];
int score2[N];
int score3[N];

Finally, when you have to take input, you do this:

cin >> N;

for(int i = 0; i < N; i++)
    cin >> score1[i] >> score2[i] >> score3[i];

I'm not sure if this is the best way to do this, but this is how I do it when I am doing online challenges. Good luck!

user2699298
  • 1,446
  • 3
  • 17
  • 33
0

This is kind of crude and bulky, but it should give you something to work with:

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

using namespace std;

int main() {
    std::cout << "Enter two numbers (0 0 to exit): ";

    string num1;
    while (true) {
        getline(cin, num1);

        string buf;
        stringstream ss(num1);
        vector<string> tokens; // Create vector to hold the two numbers (separately)

        // Split the two numbers (by the spaces)
        while (ss >> buf)
            tokens.push_back(buf);

        cout << "Adding " + tokens[0] << " and " << tokens[1] << endl;

        int sum = atoi(tokens[0].c_str()) + atoi(tokens[1].c_str());

        cout << 0 + sum;

        if (num1.empty()) {

            break;
    }
}

}

L. D. James
  • 1,679
  • 1
  • 22
  • 35
0

Another example:

12 21
13 43
1 4
A C
0 0

For this you can do:

int a, b;
while(cin >> a >> b , !(a == 0 && b == 0)){
    cout << a+b;
    // any logic for input
}

and for this input:

2
1 2 3 4 5
2 2 2

with c++ can be some tricky

vector<vector<int> > data;
int n;  cin >> n;
for(int i = 0; i < n; ++i)
    data.push_back(read_vector_line());

the tricky function here is read_vector_line. It's something like this:

vector<int> read_vector_line()
{
    vector<int> ans;
    string s;
    getline(cin, s);
    // the below line is necessary sometimes for online judges
    // getchar();
    int num = 0;
    for(int i = 0; i < s.size(); ++i)
    {
        char c = s[i];
        if(c == ' ')
        {
            ans.push_back(num);
            num = 0;
        }
        else
        {
            num = num*10 + (c-'0');
        }
    }
    return ans;
}

I'm not sure if this code compiles correctly. I left you the idea because I did not have any C++ compiler

ivangalbans
  • 470
  • 5
  • 14