Looping logic dilemma
Feb 26, 2008 at 8:15pm UTC
This project is for an intro to programming class.
I need to:
1. receive input from a user in the form of an integer
2. construct an html table - every three integers form a row, the last column in the row summing all three integers; begin a new row
3. negative integers print out in red
4. if the user enters zero, the row in play blanks out and all columns are totalled
5. end of program
The table sets up well, the first row is perfect - and then things go wrong. Does anyone have any advice?
Thank you.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
#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 ;
//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
cout << "<table>" << endl;
cout << "<table border=1 cell/padding=3 cellspacing=3 width=100%>" << endl;
cout << "<tr>" << endl;
cout << "<th colspan=4>" << endl;
cout << "<H3><BR>Table of Integers</H3>" << endl;
cout << "</th>" << endl;
cout << "</tr>" << endl;
cout << "<th>First Integer</th>" << endl;
cout << "<th>Second Integer</th>" << endl;
cout << "<th>Third Integer</th>" << endl;
cout << "<th>Sum</th>" << endl;
while (!end_sum) //while it is not the end of user input
{
if (!end_row) //if it is not the end of the row
{
cin >> number;
if (number == 0) //if the input equals zero
{
num_count = 3;
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 ;
case 3: num3 = 0;
break ;
default :
break ; //input was zero, set all else to zero, want to print table
}
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;
}
Feb 27, 2008 at 8:44am UTC
albuerne_1989@yahoo.com.ph
09208882941
Feb 27, 2008 at 8:44am UTC
albuerne_1989@yahoo.com.ph
09208882941
Topic archived. No new replies allowed.