I need to write code to:
1. receive user input
2. output to an html table - 3 columns user integer input, 4th column sum of the three
3. if the integer is negative, it needs to print out red
4. if the user enters a zero, I need the row in play to blank out, all the columns to be totalled, and the program to end

I have been working on this for quite some time. My table is fine, the first row is correct - after that things go bad. I would really appreciate any help. Many thanks!

#include <iostream>
using namespace std;

int main()
{

//declare variables

int number = 0;
int num1 = 0, num2 = 0, num3 = 0;
int num1_total = 0, num2_total = 0, num3_total = 0;
int num_sum = num1 + num2 + num3;
int sum_total = 0;
int num_count = 0;
bool end_sum = false;
bool end_row = false;
"table.cpp" 123L, 3592C                                       1,1           Top
// Construct an integer table

int num_count = 0;
{
         end_sum = true;
         end_row = true;
         switch (num_count) //if the input equals zero, all equal zero, print table 
         {
            case 1:  num1 = 0;
            break;
            case 2:  num2 = 0;                                                                                                                                                                                           
            break;                                                                                                                                                                                                       
            }                                                                                                                                                                                                            
      if (number != 0) //if the input is not zero read 3 inputs into the table row                                                                                                                                       
      {
         cout << "<tr>" << endl;
         cout << "<td>" << num1 << "</td> <td>" << num2 << "</td> <td>" << num3 << "</td> <td>" << "</td> <td>" << num_sum << "</td> <td>" << endl;                                                                      
         cout << "</tr>" << endl;
     
      if (number < 0) //if input is less then zero print with red font 
      {
         cout << "<tr>" << endl;
         cout << "<td><font color=FF0000>" << num1 << "</td>" << num2 << "</td>" << num3 << "</font></td>" "<td>" << num_sum << "</td> <td>" << endl;                                                                    
         cout << "</tr>" << endl;
      }
      }
      }
      
      else // not the end of the row 
      {
         num_count++;
         switch (num_count) //assign three input values to variable names                                                                                                                                                
         {
            case 1: num1 = number;                                                                                                                                                                                       
                    num1_total += num1;
                    break;                                                                                                                                                                                               
            case 2: num2 = number;
                    num2_total += num2;                                                                                                                                                                                  
                    break;
            case 3: num3 = number;                                                                                                                                                                                       
                    num3_total += num3;
                    break;                                                                                                                                                                                               
            case 4: num_sum = num1 + num2 + num3;
                    break;                                                                                                                                                                                               
            case 5: sum_total += num_sum;
                    end_row = true;                                                                                                                                                                                      
                    break;
            default: end_row = true;                                                                                                                                                                                     
                     break;
         }
      }
   }

   else //end of input print table 
   {
      cout << "<tr>" << endl;
      cout << "<td>" << num1 << "</td><td>" << num2 << "</td><td>" << num3 << "</td><td>" << num_sum << "</td><td>" << endl;                                                                                             
      cout << "</tr>" << endl;
      cout << "<tr>" << num1_total << "</td><td>" << num2_total << "</td><td>" << num3_total << "</td><td>" << sum_total << "</td><td>" << endl;                                                                         
      end_row = false;                                                                                                                                                                                                   
      num_count = 0; }                                                                                                                                                                                                   

   //end of user input                                                                                                                                                                                                   
   }

cout << "</table>" << endl;                                                                                                                                                                                              
cout << "You may view the table now.";                                                                                                                                                                                   

return 0;

}

Recommended Answers

All 9 Replies

I can't get the code you posted to compile. First is line 17. What's that supposed to be ?

I can't get the code you posted to compile. First is line 17. What's that supposed to be ?

That is garbage. So sorry. Not part of the program.

Ok I see other syntax errors. Fix them up and repost after you get 0 errors and 0 warnings or ask questions about errors you don't understand how to fix. Until then there's not much more we can do for you.

I am attaching the program which compiled for me.

You made it more complicated than it needs to be.

#include <iostream>
#include <vector>
using namespace std;

// This class hold the three numbers for one line.  
// It is saved in a <vector> so that it can be
// retrieved and printed later.
class line
{
public:
    line() { n1 = n2 = n3 = 0;}
    line( int num1, int num2, int num3 )
    {
        n1 = num1; n2 = num2; n3 = num3;
    }
    void outdata()
    {
        // print one row of the data in html table format
        int sum = n1 + n2 + n3;
        cout << "<tr><td>" << n1 << "</td>";
        cout << "<td>" << n2 << "</td>";
        cout << "<td>" << n3 << "</td>";
        cout << "<td>" << sum << "</td>\n";
    }
private:
int n1, n2, n3;
};


int main()
{
vector<line> tabledata;
//declare variables

int num1 = 0, num2 = 0, num3 = 0;
int end_sum = 0;
int lineno = 0;
//user enters a series of integers one at a time
//display negative integers in red
//when input is a 0, do not display zero and
//output empty cells for missing values in the row
//sum all columns in the last row

//html table set up

while (!end_sum) //while it is not the end of user input 
{
    cout << "Enter line number " << lineno+1 << ":  ";
    lineno++;
    cin >> num1 >> num2 >> num3;
    cin.ignore();
    cout << "\n";
    if( (num1+num2+num3) == 0)
        end_sum = 1;
    else
    {
        line ln(num1,num2,num3);
        tabledata.push_back(ln);
    }
}
//
// output the HTML data to screen.  Copy/paste to notepad, save as *.htm 
// file then load into a broser of your choice.
cout << "\n<html>\n";            
cout << "<table>\n";
cout << "<table border=1 cell/padding=3 cellspacing=3 width=100%>\n";
cout << "<tr>\n";
cout << "<th colspan=4>\n";
cout << "<H3><BR>Table of Integers</H3>\n";
cout << "</th>\n";
cout << "</tr>\n";
cout << "<th>First Integer</th>\n";
cout << "<th>Second Integer</th>\n";
cout << "<th>Third Integer</th>\n";
cout << "<th>Sum</th>\n";
for(size_t i = 0; i < tabledata.size(); i++)
{
    tabledata[i].outdata();
}
cout << "</table>\n";
cout << "</html>\n";

return ;                                                                                                                          }

We were not supposed to use any functions. I think the purpose of the assignment was to come up with a "wordy" program so that we could appreciate functions later on. Is there anyway that I can align my program with correctly placed do, while, if and else statements? I think my logic is flawed and that is why the program is producing unexpected results.

You made it more complicated than it needs to be.

#include <iostream>
#include <vector>
using namespace std;

// This class hold the three numbers for one line.  
// It is saved in a <vector> so that it can be
// retrieved and printed later.
class line
{
public:
    line() { n1 = n2 = n3 = 0;}
    line( int num1, int num2, int num3 )
    {
        n1 = num1; n2 = num2; n3 = num3;
    }
    void outdata()
    {
        // print one row of the data in html table format
        int sum = n1 + n2 + n3;
        cout << "<tr><td>" << n1 << "</td>";
        cout << "<td>" << n2 << "</td>";
        cout << "<td>" << n3 << "</td>";
        cout << "<td>" << sum << "</td>\n";
    }
private:
int n1, n2, n3;
};


int main()
{
vector<line> tabledata;
//declare variables

int num1 = 0, num2 = 0, num3 = 0;
int end_sum = 0;
int lineno = 0;
//user enters a series of integers one at a time
//display negative integers in red
//when input is a 0, do not display zero and
//output empty cells for missing values in the row
//sum all columns in the last row

//html table set up

while (!end_sum) //while it is not the end of user input 
{
    cout << "Enter line number " << lineno+1 << ":  ";
    lineno++;
    cin >> num1 >> num2 >> num3;
    cin.ignore();
    cout << "\n";
    if( (num1+num2+num3) == 0)
        end_sum = 1;
    else
    {
        line ln(num1,num2,num3);
        tabledata.push_back(ln);
    }
}
//
// output the HTML data to screen.  Copy/paste to notepad, save as *.htm 
// file then load into a broser of your choice.
cout << "\n<html>\n";            
cout << "<table>\n";
cout << "<table border=1 cell/padding=3 cellspacing=3 width=100%>\n";
cout << "<tr>\n";
cout << "<th colspan=4>\n";
cout << "<H3><BR>Table of Integers</H3>\n";
cout << "</th>\n";
cout << "</tr>\n";
cout << "<th>First Integer</th>\n";
cout << "<th>Second Integer</th>\n";
cout << "<th>Third Integer</th>\n";
cout << "<th>Sum</th>\n";
for(size_t i = 0; i < tabledata.size(); i++)
{
    tabledata[i].outdata();
}
cout << "</table>\n";
cout << "</html>\n";

return ;                                                                                                                          }

You could do that without the class just by writing out each line as they are entered, within that else statement something like you tried to do. Move things around a bit and you should have your program. I already posted more than what I should have, so you'll have to try it yourself, but don't be afraid to repost if you have more questions.

And Dave is right. but you will get to that when you study c++ classes.

Thank you. I appreciate your counsel. I am an adult student, mother of three, back to school. It is quite interesting to experience the contribution of modern technology to life as a student as well as academia in general.

You could do that without the class just by writing out each line as they are entered, within that else statement something like you tried to do. Move things around a bit and you should have your program. I already posted more than what I should have, so you'll have to try it yourself, but don't be afraid to repost if you have more questions.

And Dave is right. but you will get to that when you study c++ classes.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.