Inputting the data (C)

Back to General discussions forum

jpl_thefirst     2016-03-29 18:45:38

OK I've got to 5 completes but only by hard-coding the data. Can someone point me to a page that explains how to input the data using in C++ using cin? For example if the input file consists of a number of rows (eg 27) and three columns of numbers to input, how do I tell my program which item is which?

Thanks

Quandray     2016-03-30 13:49:30
User avatar

Hi,

There's an example using scanf at http://www.codeabbey.com/index/wiki/running

To use cin, that needs to be changed to

#include <iostream>
using namespace std;

int main(){
  int a,b;
  cin >> a >> b;
  cout << a+b << endl;
  return 0;
}

That will work fine if you are running the code from the codeabbey interface.

To do the same on your PC, you will also need to tell your compiler where to read the input from. How you do that will depend on what compiler you are using. In MS Visual C++ 2010, it's Project...properties...Configuration properties...Debugging...Command Arguments... and enter the following to read data from a file called input.txt.

<input.txt

If you later want to write output to a file, instead of to the screen, you could also enter

>output.txt
jpl_thefirst     2016-03-30 15:28:43

Thanks for your help

Yes, it was the codeabbey interface I was interested in.

What about for

127

56 67 78

89 90 100 ...

I guess the first ">>" corresponds to 127, the second to 56, the third to 78, the fourth to 89 .. etc?

Quandray     2016-03-30 15:53:00
User avatar

I think you meant "...the third to 67, the fourth to 78" :-)

For the problems you've already solved, you can look at how other people solved them in C++ and hopefully some will have used cin.

If you get stuck, please ask again.

Matthew Cole     2016-03-31 01:41:12

To get the rows of data, you'll need to use some sort of for loop. For problem-wide variables, you'll have some sort of cin outside the loop. For each row to be processed, you have some sort of cin inside the loop.

For problem #5 - which sounds similar to your problem - here's one way to do it...

#include <iostream>

using namespace std;

int min-of-three(int a, int b, int c)
{
    // Your function definition here!
}

int main (int argc, char** argv)
{
    int num_rows, x, y, z;

    //Get the number of rows
    cin >> num_rows;

    for (int row = 0; row < num_rows; row++)
    {
        cin >> x >> y >> z;
        cout << min-of-three(x, y, z);
    }

    cout << endl;
    return 0;
}
Please login and solve 5 problems to be able to post at forum