integers

Write a program that reads nine integers and prints them three in a line separated by commas as shown below:

1
2
3
4
5
6
Input: 10 31 2 73 24 65 6 87 18

Output:
10, 31, 2
73, 24, 65
6, 87, 18
What have you attempted!?
#include <iostream>

using namespace std;

int main ()
{
cout << "10, 31, 2" << endl;
cout << "73, 24, 65" << endl;
cout << "6, 87, 18" << endl;
return 0;
}

It works but am i writing it the way the problem asks me to?
The problem with your program is that you're not reading nine integers; you're hard-coding them instead. Modify it so that it first prompts the user for nine integers and then displays them as requested.
First, you should start with:
#include <iostream>
Should be the first thing you type when writing a program.
Then:
using namespace std;
Include a semicolon after every statement ";".
Now, when you're actually going to start writing what the program does, you need an int main().
1
2
3
4
#include <iostream>
using namespace std;

int main(){

Next, declare your variables.
int a, b, c, d, e, f, g, h, i;
When declaring them, int is used for whole numbers, while double is used for whole numbers and decimals.
More stuff:
cout << "Please input 9 numbers, separated by spaces." << '\n';
"cout << " Tells the comp to say something to the user.
with cout << , use "" to tell the machine to say whatever's between the quotes. For example:
cout << "thereisnospoon";
"cin >> " takes the user's input and saves them to variables.
'\n' and endl start new lines.
>> is used to input
<< is used to output
1
2
3
4
cin >> a >> b >> c >> d >> e >> f >> g >> h >> i;
cout << a << ", " << b << ", " << c << ", " << '\n';
cout << d << ", " << e << ", " << f << ", " << '\n';
cout << g << ", " << h << ", " << i;

This is how you would output all of those numbers in that format.

EDIT:
Forgot to add,

1
2
3
cin.ignore(2);
return 0;
}

Make sure to close the brace.
cin.ignore(); so that when you run it, it stays open long enough for you to see what it did.
Last edited on
@Datnewnew why so complicated? Use an array and a for()loop.
Topic archived. No new replies allowed.