I have written the program to take input and put it in a table with negative numbers in red and positive numbers in black. The last cell in the row is used for the sum of that row. My question now is how would I go about getting the code for the columns up and down and not just across if I don't know in advance how many inputs there are? This is what I have done so far.
ps: I know it would have been easier to use functions but the assignment didn't allow it.
Code:
#include <iostream> using namespace std; int main() { cout << "<html>"; cout << "<body>"; cout << "<table border>"; int value1,value2,value3; cout << "<tr><td>"<< "one"<< "</td>"; cout << "<td>"<< "two" << "</td>"; cout << "<td>"<< "three"<< "</td>"; cout << "<td>"<< "Sum"<< "</td></tr>" << endl; int sum; int count = 1; while (count > 0) //infinite loop that will only be broken if zero is a input. { cin >> value1; if(value1 == 0) break; else if (value1 > 0) cout << "<tr><td>"<< value1<<" </td>"; if (value1 < 0) cout << "<td><font color=red>" << value1 << "</font></td>"; cin >> value2; if(value2 == 0) break; else if (value2 > 0) cout << "<td>"<< value2 << " </td>"; if (value2 < 0) cout << "<td><font color=red>"<< value2 << " </font></td>"; cin >> value3; if(value3 == 0) break; else if (value3 > 0) cout << "<td>" << value3 <<"</td>"; if (value3 < 0) cout << "<td><font color=red>" << value3 << "</font></td>"; sum = value1 + value2 + value3; if (sum > 0) cout << "<td>" << sum << " </td>"; else cout << "<td><font color=red>" << sum << "</font></td>"; cout << "</tr>" << endl; count ++; } int cell = 1; while (cell <=3) { if (value1 == 0) cout << "<tr><td>" << " " << " </td>"; cout << "<td>" << " " << " </td>"; cout << "<td>" << " " << " </td>"; sum = value1; if (sum > 0) cout << "<td>" << sum << " </td></tr>"; else cout << "<td><font color=red>" << sum << "</font></td></tr>"; cell++; break; if (value2 == 0) cout << "<td>" << " " << " </td>"; cout << "<td>" << " " << " </td>"; sum = value1; if (sum > 0) cout << "<td>" << sum << " </td></tr>"; else cout << "<td><font color=red>" << sum << "</font></td></tr>"; cell++; break; if (value3 == 0) cout << "<td>" << " " << " </td>"; sum = value1+value2; if (sum > 0) cout << "<td>" << sum << " </td></tr>"; else cout << "<td><font color=red>" << sum << "</font></td></tr>"; cell++; break; } cout << "</html>"; cout << "</body>"; cout << "</table>" << endl; return 0; }
Comment