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

question on c++ program to make an html table

P: 57
I am writing a program that can take input from a user or file of positive and negative numbers. Then display them in a html table. The negative numbers have to be outputted in red html font on the table and the positive numbers will just be outputted. I wrote a loop that shouldn't terminate till a zero is entered. For some reason it won't print my table. This is what I have so far if anyone can shine some light on this for me I would be gratefull.
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>";
  9.  
  10.     int value1,value2,value3;
  11.  
  12.     cout << "<tr><td>"<< "1"<< "</td>";
  13.     cout << "<td>"<< "2" << "</td>";
  14.     cout << "<td>"<< "3"<< "</td>";
  15.     cout << "<td>"<< "Sum"<< "</td></tr>" << endl;
  16.     int sum;
  17.     int count = 1;
  18.     while (count > 0) //infinite loop that will only be broken if zero is a input.
  19.     {
  20.         cin >> value1 >> value2 >> value3;
  21.         while(value1 == 0)
  22.         {
  23.             cout << "<tr><td>" << " " << " </td>";
  24.             cout << "<td>" << " " << " </td>";
  25.             cout << "<td>" << " " << " </td>";
  26.             sum = value1;
  27.             cout << "<td>" << sum << " </td>";
  28.             cout << "</tr>";
  29.         }
  30.         break;
  31.         if (value1 > 0)
  32.             cout << "<tr><td>"<< value1<<" </td>";
  33.         else;
  34.             cout << "<td><font color=red>" << value1 << "</font></td>";
  35.  
  36.         while(value2 == 0)
  37.         {
  38.             cout << "<td>" << " " << " </td>";
  39.             cout << "<td>" << " " << " </td>";
  40.             sum = value1+value2;
  41.             cout << "<td>" << sum << " </td>";
  42.             cout << "</tr>";
  43.         }
  44.         break;
  45.  
  46.         if (value2 > 0)
  47.             cout << "<tr><td>"<< value2 << " </td>";
  48.         else;
  49.         cout << "<td><font color=red>"<<  value2 << " </font></td>";
  50.  
  51.         while(value3 == 0)
  52.         {
  53.              cout << "<td>" << " " << " </td>";
  54.              sum = value1+value2+value3;
  55.              cout << "<td>" << sum << " </td>";
  56.              cout << "</tr>";
  57.          }
  58.          break;
  59.  
  60.          if (value3 > 0)
  61.              cout << "<tr><td>" << value3 <<"</td>";
  62.          else;
  63.          cout << "<td><font color=red>" << value3 << "</font></td>";
  64.          sum = value1 + value2 + value3;
  65.          cout << "<td>" << sum << " </td>";
  66.          cout << "</tr>" << endl;
  67.     }
  68.     cout << "</html>";
  69.     cout << "</body>";
  70.     cout << "</table>" << endl;
  71.  
  72.     return 0;
  73.  
  74. }
Feb 21 '07 #1
Share this Question
Share on Google+
1 Reply


sicarie
Expert Mod 2.5K+
P: 4,677
I am writing a program that can take input from a user or file of positive and negative numbers. Then display them in a html table. The negative numbers have to be outputted in red html font on the table and the positive numbers will just be outputted. I wrote a loop that shouldn't terminate till a zero is entered. For some reason it won't print my table. This is what I have so far if anyone can shine some light on this for me I would be gratefull.
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>";
  9.  
  10.     int value1,value2,value3;
  11.  
  12.     cout << "<tr><td>"<< "1"<< "</td>";
  13.     cout << "<td>"<< "2" << "</td>";
  14.     cout << "<td>"<< "3"<< "</td>";
  15.     cout << "<td>"<< "Sum"<< "</td></tr>" << endl;
  16.     int sum;
  17.     int count = 1;
  18.     while (count > 0) //infinite loop that will only be broken if zero is a input.
  19.     {
  20.         cin >> value1 >> value2 >> value3;
  21.         while(value1 == 0)
  22.         {
  23.             cout << "<tr><td>" << " " << " </td>";
  24.             cout << "<td>" << " " << " </td>";
  25.             cout << "<td>" << " " << " </td>";
  26.             sum = value1;
  27.             cout << "<td>" << sum << " </td>";
  28.             cout << "</tr>";
  29.         }
  30.         break;
  31.         if (value1 > 0)
  32.             cout << "<tr><td>"<< value1<<" </td>";
  33.         else; 
  34.         cout << "<td><font color=red>" << value1 << "</font></td>";
  35.  
  36.         while(value2 == 0)
  37.         {
  38.             cout << "<td>" << " " << " </td>";
  39.             cout << "<td>" << " " << " </td>";
  40.             sum = value1+value2;
  41.             cout << "<td>" << sum << " </td>";
  42.             cout << "</tr>";
  43.         }
  44.         break;
  45.  
  46.         if (value2 > 0)
  47.             cout << "<tr><td>"<< value2 << " </td>";
  48.         else;
  49.         cout << "<td><font color=red>"<<  value2 << " </font></td>";
  50.  
  51.         while(value3 == 0)
  52.         {
  53.              cout << "<td>" << " " << " </td>";
  54.              sum = value1+value2+value3;
  55.              cout << "<td>" << sum << " </td>";
  56.              cout << "</tr>";
  57.          }
  58.          break;
  59.  
  60.          if (value3 > 0)
  61.              cout << "<tr><td>" << value3 <<"</td>";
  62.          else;
  63.          cout << "<td><font color=red>" << value3 << "</font></td>";
  64.          sum = value1 + value2 + value3;
  65.          cout << "<td>" << sum << " </td>";
  66.          cout << "</tr>" << endl;
  67.     }
  68.     cout << "</html>";
  69.     cout << "</body>";
  70.     cout << "</table>" << endl;
  71.  
  72.     return 0;
  73.  
  74. }
You use breaks after the inner while loops - that pulls you out, no matter what (I think you might want an if statement - if the first while loop was entered, then break...). Also, I'm not sure if you mean to have those ';'s after the else statements - they signify that the else is empty and the cout statements after that will be run, even if the else condition is not met (just in case that was not by design - I have seen that used, and used it myself before).
Feb 21 '07 #2

Post your reply

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