adding sums of an html table

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gdarian216
    New Member
    • Oct 2006
    • 57

    adding sums of an html table

    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.

    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;
    
    }
    ps: I know it would have been easier to use functions but the assignment didn't allow it.
  • bawa
    New Member
    • Feb 2007
    • 8

    #2
    Originally posted by gdarian216
    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.

    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;
    
    }
    ps: I know it would have been easier to use functions but the assignment didn't allow it.
    Code:
    #include <iostream>
    using namespace std;
    
    void main() 
    {
    
    	int value[100];	
    	int sum=0;
    	int count; 
    
    	cout << endl << "Kindly enter the number of inputs" <<endl;
    	cin >> count;
    
    	cout << endl << "Kindly enter the values for the specified number of inputs" << endl;
    	for(int i=0;i<count;i++)
    	cin >> value[i];
    
    	cout << endl << endl;
    	cout << "<html>";
    	cout << "\n";
    	cout << "<body>";
    	cout << "\n\t";
    	cout << "<table border>";	
    
    
        i=0;    
    	while (i < count)
        {
    				
    		if(value[i] == 0)
            break;
    
            else
    	
    			if (value[i] > 0)
    			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>";
    			if (value[i] < 0)
    			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>";
    	
    		sum = sum + value[i];
    		i++;
    	}
    
    	
    	if (sum > 0)
        cout << endl << "\t\t" << "<tr>" << "\n\t\t\t" <<"<td> Sum </td>" <<"\n\t\t\t"  <<"<td>" << sum << "</td>" <<"\n\t\t" <<"</tr>";
        else
        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>";
    
    	
    	cout << "\n\t";
    	cout << "</table>" << endl;
    	cout << "</html>" <<endl;
    	cout << "</body>" <<endl;
    
    }

    Hope this will solve ur problem..

    - Bawa

    Comment

    Working...