### Creating a grid with totals Hello all!

I need some help with a project:
The user enters 9 integers to fill in a 3 x 3 grid; determine the totals horizontally and vertically. For example:

1 | 2 | 3 = 6
4 | 5 | 6 = 15
7 | 8 | 9 = 24
--|---|--
12 15 18

I'm using Microsoft Visual Studio. So far, this is my code.

#include <iostream>
using namespace std;
int main() {
int rows;
int cols;
int num;
int size = 3;
rows = 0;
while (rows < size) {
cols = 0;
while (cols < size) {
cols = (cols + 1);
cout << cols << " | " << endl;
}
rows = (rows + 1);
cout << endl;
}
system("pause");
return 0;
}

I'm getting confused on how to add the totals and how to fix the numbers so that they show up the correct way. When I added that cout << cols << " | " << endl; line, it made my numbers appear in all one column instead of 3. If anyone can help me fix this up soon, I would really appreciate it. I'm stuck on how to finish it. Thank you! remove the endl. Oh thank you! Now it looks a little better! Thanks for helping me fix that part use cin.get() instead of system("pause") Not sure if the system("pause") is really going to make that much of a difference in the grand scheme of things. Okay so I figured out that I need to add in an array and add more in to try and complete this. I have this so far:

#include <iostream>
using namespace std;
int main() {
int cols;
int num;
int number;
int counter = 0;
int rows = 0;
int size = 3;
while (counter < 9) {
cout << "Enter a number between 0 and 100: ";
cin >> number;
while ((number < 0) || (number > 100)) {
cout << "Please enter a number between 0 and 100: ";
cin >> number;
}
num[counter] = number;
counter = (counter + 1);
}
while (rows < size) {
cols = 0;
while (cols < size) {
cols = (cols + 1);
cout << cols << " | ";
}
rows = (rows + 1);
cout << endl;
}
system("pause");
return 0;
}

Obviously it's still not giving me the output I need but I keep getting stuck just when I think I have it figured out. Any suggestions? Thanks!
Last edited on a program or an application only do stuffs you tell it to do
check your code line by line what do you they will do and you will find out right away

 ``123456789101112131415161718192021222324252627282930313233`` ``````#include using namespace std; int main() { int cols; int num; int number; int counter = 0; int rows = 0; int size = 3; while (counter < 9) { cout << "Enter a number between 0 and 100: "; cin >> number; while ((number < 0) || (number > 100)) { cout << "Please enter a number between 0 and 100: "; cin >> number; } num[counter] = number; counter = (counter + 1); } counter = 0; while (rows < size) { cols = 0; while (cols < size) { cout << num[counter] << " | "; cols = (cols + 1); counter++; } rows = (rows + 1); cout << endl; } system("pause"); return 0; }``````

this is not the final program u want, but just corrected the mistake you did
Last edited on ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849`` ``````#include #include #include int getNumber( int min, int max ) { int input ; do { if ( !std::cin ) { std::cin.clear() ; //clear error state and ignore the rest of the line. std::cin.ignore(std::numeric_limits::max(), '\n') ; } std::cout << "Enter a number [" << min << ", " << max << "]: " ; } while ( !(std::cin >> input ) || (input < min || input > max) ) ; return input ; } int main() { int num; for ( unsigned i=0; i<9; ++i ) num[i] = getNumber(0,100) ; for ( unsigned row = 0; row < 3; ++row ) { int rowSum = 0 ; unsigned col ; for ( col = 0; col < 2; ++col ) { rowSum += num[col+row*3] ; std::cout << std::setw(3) << num[col+row*3] << '|' ; } rowSum += num[col+row*3] ; std::cout << std::setw(3) << num[col+row*3] << " = " << std::setw(3) << rowSum << '\n' ; } std::cout << "---|---|---\n" ; for ( unsigned col = 0 ; col < 3; ++col ) std::cout << std::setw(3) << num[col]+num[col+3]+num[col+6] << ' ' ; std::cout << '\n' ; }`````` Wow thank you! I changed my coding around a little bit. I just need to figure out how to get the totals at the bottom of each column and at the end of each row. I'd like to still keep the same code I have (obviously with some changes to it). The asterisks are where the totals are supposed to be. Thanks

#include <iostream>
using namespace std;
int main() {
int cols = 0;
int num;
int number;
int counter = 0;
int rows = 0;
int size = 3;
int rowsum = 0;
while (counter < 9) {
cout << "Enter a number between 0 and 100: ";
cin >> number;
while ((number < 0) || (number > 100)) {
cout << "Please enter a number between 0 and 100: ";
cin >> number;
}
num[counter] = number;
counter = (counter + 1);
}
int counter2 = 0;
while (counter2 < 9) {
cout << num[counter2] << " | ";
if ((counter2 + 1) % 3 == 0) {
cout << " = *" << endl;
}
counter2++;
}
cout << "--|--|--" << endl;
rowsum += num[cols+rows*3];
cin >> rowsum;
cout << rowsum;
cout << "* ";
cout << "* ";
cout << "* " << endl;
system("pause");
return 0;
}
Topic archived. No new replies allowed.