By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,365 Members | 3,180 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,365 IT Pros & Developers. It's quick & easy.

adding sums of an html table

P: 57
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.

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main() {
  5.  
  6. cout << "<html>";
  7. cout << "<body>";
  8. cout << "<table border>";
  9.  
  10.  
  11.         int value1,value2,value3;
  12.  
  13.                 cout << "<tr><td>"<< "one"<< "</td>";
  14.                 cout << "<td>"<< "two" << "</td>";
  15.                 cout << "<td>"<< "three"<< "</td>";
  16.                 cout << "<td>"<< "Sum"<< "</td></tr>" << endl;
  17.         int sum;
  18.         int count = 1;
  19.         while (count > 0) //infinite loop that will only be broken if zero is a input.
  20.         {
  21.         cin >> value1;
  22.  
  23.                 if(value1 == 0)
  24.                 break;
  25.  
  26.                 else
  27.  
  28.                 if (value1 > 0)
  29.                 cout << "<tr><td>"<< value1<<" </td>";
  30.                 if (value1 < 0)
  31.                 cout << "<td><font color=red>" << value1 << "</font></td>";
  32.  
  33.         cin >> value2;
  34.                 if(value2 == 0)
  35.                 break;
  36.  
  37.                 else
  38.  
  39.                 if (value2 > 0)
  40.                 cout << "<td>"<< value2 << " </td>";
  41.                 if (value2 < 0)
  42.                 cout << "<td><font color=red>"<<  value2 << " </font></td>";
  43.  
  44.         cin >> value3;
  45.                 if(value3 == 0)
  46.                 break;
  47.  
  48.                 else
  49.  
  50.                 if (value3 > 0)
  51.                 cout << "<td>" << value3 <<"</td>";
  52.                 if (value3 < 0)
  53.                 cout << "<td><font color=red>" << value3 << "</font></td>";
  54.                 sum = value1 + value2 + value3;
  55.                 if (sum > 0)
  56.                 cout << "<td>" << sum << " </td>";
  57.                 else
  58.                 cout << "<td><font color=red>" << sum << "</font></td>";
  59.                 cout << "</tr>" << endl;
  60.                 count ++;
  61.  
  62.  
  63. }
  64.                 int cell = 1;
  65.                 while (cell <=3)
  66.                 {
  67.                 if (value1 == 0)
  68.                 cout << "<tr><td>" << " " << " </td>";
  69.                 cout << "<td>" << " " << " </td>";
  70.                 cout << "<td>" << " " << " </td>";
  71.                 sum = value1;
  72.                 if (sum > 0)
  73.                 cout << "<td>" << sum << " </td></tr>";
  74.                 else
  75.                 cout << "<td><font color=red>" << sum << "</font></td></tr>";
  76.                 cell++;
  77.                 break;
  78.  
  79.  
  80.  
  81.                 if (value2 == 0)
  82.                 cout << "<td>" << " " << " </td>";
  83.                 cout << "<td>" << " " << " </td>";
  84.                 sum = value1;
  85.                 if (sum > 0)
  86.                 cout << "<td>" << sum << " </td></tr>";
  87.                 else
  88.                 cout << "<td><font color=red>" << sum << "</font></td></tr>";
  89.                 cell++;
  90.                 break;
  91.  
  92.  
  93.  
  94.                 if (value3 == 0)
  95.                 cout << "<td>" << " " << " </td>";
  96.                 sum = value1+value2;
  97.                 if (sum > 0)
  98.                 cout << "<td>" << sum << " </td></tr>";
  99.                 else
  100.                 cout << "<td><font color=red>" << sum << "</font></td></tr>";
  101.                 cell++;
  102.  
  103.                 break;
  104.                 }
  105.  
  106. cout << "</html>";
  107. cout << "</body>";
  108. cout << "</table>" << endl;
  109.  
  110.         return 0;
  111.  
  112. }
ps: I know it would have been easier to use functions but the assignment didn't allow it.
Feb 21 '07 #1
Share this Question
Share on Google+
1 Reply


P: 8
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.

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main() {
  5.  
  6. cout << "<html>";
  7. cout << "<body>";
  8. cout << "<table border>";
  9.  
  10.  
  11.         int value1,value2,value3;
  12.  
  13.                 cout << "<tr><td>"<< "one"<< "</td>";
  14.                 cout << "<td>"<< "two" << "</td>";
  15.                 cout << "<td>"<< "three"<< "</td>";
  16.                 cout << "<td>"<< "Sum"<< "</td></tr>" << endl;
  17.         int sum;
  18.         int count = 1;
  19.         while (count > 0) //infinite loop that will only be broken if zero is a input.
  20.         {
  21.         cin >> value1;
  22.  
  23.                 if(value1 == 0)
  24.                 break;
  25.  
  26.                 else
  27.  
  28.                 if (value1 > 0)
  29.                 cout << "<tr><td>"<< value1<<" </td>";
  30.                 if (value1 < 0)
  31.                 cout << "<td><font color=red>" << value1 << "</font></td>";
  32.  
  33.         cin >> value2;
  34.                 if(value2 == 0)
  35.                 break;
  36.  
  37.                 else
  38.  
  39.                 if (value2 > 0)
  40.                 cout << "<td>"<< value2 << " </td>";
  41.                 if (value2 < 0)
  42.                 cout << "<td><font color=red>"<<  value2 << " </font></td>";
  43.  
  44.         cin >> value3;
  45.                 if(value3 == 0)
  46.                 break;
  47.  
  48.                 else
  49.  
  50.                 if (value3 > 0)
  51.                 cout << "<td>" << value3 <<"</td>";
  52.                 if (value3 < 0)
  53.                 cout << "<td><font color=red>" << value3 << "</font></td>";
  54.                 sum = value1 + value2 + value3;
  55.                 if (sum > 0)
  56.                 cout << "<td>" << sum << " </td>";
  57.                 else
  58.                 cout << "<td><font color=red>" << sum << "</font></td>";
  59.                 cout << "</tr>" << endl;
  60.                 count ++;
  61.  
  62.  
  63. }
  64.                 int cell = 1;
  65.                 while (cell <=3)
  66.                 {
  67.                 if (value1 == 0)
  68.                 cout << "<tr><td>" << " " << " </td>";
  69.                 cout << "<td>" << " " << " </td>";
  70.                 cout << "<td>" << " " << " </td>";
  71.                 sum = value1;
  72.                 if (sum > 0)
  73.                 cout << "<td>" << sum << " </td></tr>";
  74.                 else
  75.                 cout << "<td><font color=red>" << sum << "</font></td></tr>";
  76.                 cell++;
  77.                 break;
  78.  
  79.  
  80.  
  81.                 if (value2 == 0)
  82.                 cout << "<td>" << " " << " </td>";
  83.                 cout << "<td>" << " " << " </td>";
  84.                 sum = value1;
  85.                 if (sum > 0)
  86.                 cout << "<td>" << sum << " </td></tr>";
  87.                 else
  88.                 cout << "<td><font color=red>" << sum << "</font></td></tr>";
  89.                 cell++;
  90.                 break;
  91.  
  92.  
  93.  
  94.                 if (value3 == 0)
  95.                 cout << "<td>" << " " << " </td>";
  96.                 sum = value1+value2;
  97.                 if (sum > 0)
  98.                 cout << "<td>" << sum << " </td></tr>";
  99.                 else
  100.                 cout << "<td><font color=red>" << sum << "</font></td></tr>";
  101.                 cell++;
  102.  
  103.                 break;
  104.                 }
  105.  
  106. cout << "</html>";
  107. cout << "</body>";
  108. cout << "</table>" << endl;
  109.  
  110.         return 0;
  111.  
  112. }
ps: I know it would have been easier to use functions but the assignment didn't allow it.
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. void main() 
  5. {
  6.  
  7.     int value[100];    
  8.     int sum=0;
  9.     int count; 
  10.  
  11.     cout << endl << "Kindly enter the number of inputs" <<endl;
  12.     cin >> count;
  13.  
  14.     cout << endl << "Kindly enter the values for the specified number of inputs" << endl;
  15.     for(int i=0;i<count;i++)
  16.     cin >> value[i];
  17.  
  18.     cout << endl << endl;
  19.     cout << "<html>";
  20.     cout << "\n";
  21.     cout << "<body>";
  22.     cout << "\n\t";
  23.     cout << "<table border>";    
  24.  
  25.  
  26.     i=0;    
  27.     while (i < count)
  28.     {
  29.  
  30.         if(value[i] == 0)
  31.         break;
  32.  
  33.         else
  34.  
  35.             if (value[i] > 0)
  36.             cout << endl << "\t\t" << "<tr>" << "\n\t\t\t" <<"<td> value" << i << "</td>" << "\n\t\t\t" << "<td>" << value[i] << "</td>" <<"\n\t\t" <<"</tr>";
  37.             if (value[i] < 0)
  38.             cout << endl << "\t\t" << "<tr>" << "\n\t\t\t" <<"<td> value" << i << "</td>" << "\n\t\t\t" <<"<td><font color=red>" << value[i] << "</font></td>" <<"\n\t\t" <<"</tr>";
  39.  
  40.         sum = sum + value[i];
  41.         i++;
  42.     }
  43.  
  44.  
  45.     if (sum > 0)
  46.     cout << endl << "\t\t" << "<tr>" << "\n\t\t\t" <<"<td> Sum </td>" <<"\n\t\t\t"  <<"<td>" << sum << "</td>" <<"\n\t\t" <<"</tr>";
  47.     else
  48.     cout << endl << "\t\t" << "<tr>" << "\n\t\t\t" <<"<td> Sum </td>" <<"\n\t\t\t"  <<"<td><font color=red>" << sum << "</font></td>" <<"\n\t\t" <<"</tr>";
  49.  
  50.  
  51.     cout << "\n\t";
  52.     cout << "</table>" << endl;
  53.     cout << "</html>" <<endl;
  54.     cout << "</body>" <<endl;
  55.  
  56. }
  57.  

Hope this will solve ur problem..

- Bawa
Feb 21 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.