I'm having some issues producing this output for my code.
Apples
10 # 0.98/UNIT: $9.80
Bananas
1 # 1.29/UNIT: $1.29
Flank Steak
1 # 8.82/UNIT: $8.82
Chocolate Ice Cream
1 # 3.23/UNIT: $3.23
Gym Bag
1 # 23.12/UNIT: $23.12
ORDER TOTAL:************************************$46.26
My issue is with the alignment of the decimal places for the totals with the dollar sign attached. I'm supposed to be able to do it with raw setw() code and right, left alignment, but I'm not so sure how to go about it without getting spaces between the $ and the actual numerical value.
Here's what I've gotten so far..
void printReceipt(const int cart[], const string productName[], const double prices[], int productCount){
double orderTotal = 0;
for (int i = 0; i < productCount; i++){ //Loop for output of receipt
if (cart[i] != 0){ //Will not output for item not ordered.
cout << productName[i] << endl;
cout << fixed << setprecision(2)
<< setw(3) << left << cart[i]
<< setw(3) << " # " //Formatting for receipt print
<< setw(6) << prices[i]
<< setw(35) << left << "/UNIT:" << "$"
<< setw(6)<< right << (cart[i] * prices[i]) << endl;
orderTotal = orderTotal + (cart[i] * prices[i]);
}}
cout << fixed << setfill('*') << setw(47)<< left << "ORDER TOTAL:";
cout << setfill(' ') << "$" << setw(6) << right << setprecision(2) << orderTotal;
}
current output is as follows
Apples
5 # 0.98/UNIT: $ 4.90
Bananas
5 # 1.29/UNIT: $ 6.45
Flank Steak
5 # 8.82/UNIT: $ 44.10
Chocolate Ice Cream
5 # 3.23/UNIT: $ 16.15
Gym Bag
5 # 23.12/UNIT: $115.60
ORDER TOTAL:***********************************$187.20
You will have to do this as a two-step process.
Format the dollar amount (cart[i]*prices[i]) into a std::ostringstream, using only the setprecision manipulator. No minimum setw, so all you get is the formatted amount. Obtain the string representation from the std::ostringstream, using str().
By using the length of the string that's returned from str(), you can calculate the amount of padding needed to position the '$'.
Instead of the fixed setw(35), you will compute the padding for this field. Just as a rough estimate, this will probably be:
<< setw(42-amount.length()) << left << "/UNIT:" << "$" << amount << std::endl;
where amount is the formatted std::string you obtained in step 1. In this manner, the width here will automatically adjust to give the appropriate amount of room for the amount.
This, by itself will not adjust for the unit count at the beginning of the line, which is also variable length. But after handling the amount correctly, here, you should be able to figure out how to fix that in the same manner.
Related
I just started classes for my college programming course, so I am a complete beginner. I want to align my decimals between my input and output phases. I can get my output phase all aligned, but the input phase is not lining up. Is there a way to ensure my decimals line up? I have added a picture to show the code and the line that I would like to align. (lines 16/17 to line up with lines 30-33). Thank you for any help you can provide. I am completely new to programming and am not even 100% sure of what I am doing, a lot of trial and error at this point.
program/code screenshot
Here is the code I have written so far:
#include <iomanip>
using namespace std;
int main()
{
//define variables
float number_books;
float cost_book;
float total_order;
float total_shipping;
float total_order_shipping;
// input phase
cout << "Enter number of books to order: ";
cin >> number_books;
cout << "Enter cost per book: $ ";
cin >> cost_book;
// process phase
total_order = number_books * cost_book;
if (total_order > 50)
total_shipping = 0;
else
total_shipping = 25.00f;
total_order_shipping = total_shipping + total_order;
// output phase
cout << setprecision(2) << fixed;
cout << "Book order total: $" << setw(8) << total_order << endl;
cout << "Shipping total: $" << setw(8) << total_shipping << endl;
cout << "Order & Shipping total:$" << setw(8) << total_order_shipping << endl;
system("pause");
return 0;
}
It's not possible to directly format a cin with setw, in your case setw(8).
See the reference: Issues with cin and Aligning a cin to the right
But in your case, you can hack the format into setw(8) by the help of the user:
Enter number of books to order: 4
Enter cost per book: $ 2.15
Book order total: $ 8.60
Shipping total: $ 25.00
Order & Shipping total:$ 33.60
User can calculate how many spaces until 8 is proper, in your case, put two spaces. If that's not what you want, then unfortunately it's out of luck to directly use cin to achieve what you want...
Let me explain little further. I am writing a code to calculate the amount of water in swimming pool after filling it for some time at the filling rate.
Input taken is length, width, depth in foot, time to fill the pool as timeToFillPool in seconds, water filling rate in pool as fillingRate in in US Gallons/minute, amount of water already in the pool as poolCurrWaterAmount in US Gallons.
From this information I calculated total pool water capacity, totalPoolCapacity, by multiplying length, width and depth and converting it to US Gallons.
I asked the user that how much water is already in the pool and then calculated and shown user appropriate messages like water can be filled or not if the pool is already full or water filled in given time will exceed pool capacity?
I am assuming all values input are positive or equal to zero if allowed to be zero.
#include <iostream>
#include <iomanip>
#include <cmath> //for fabs() functions
using namespace std ;
int main()
{
double timeToFillPool ;
double length, width, depth, fillingRate ;
double poolCurrWaterAmount, totalPoolCapacity ;
const double CUBIC_FOOT_TO_USGALLON = 7.48052 ; //i.e. 1 cubic foot = 7.48052 US Gallon
//setting tolerance value for comparing floating point numbers to 1/10000
//any thing less will be considered zero
const double EPSILON = 0.0001 ;
//preparing the output stream to print floating numbers in decimal
//form with precision to print two digits after decimal point
cout << fixed << setprecision(2);
cout << "Please enter swimming pool's dimensions,capacity,fill rate & drain rate information.\n";
cout << "Enter Length in foot : " ;
cin >> length ;
cout << "Enter width in foot : " ;
cin >> width ;
cout << "Enter depth in foot : " ;
cin >> depth ;
cout << "Enter filling rate of water in US Gallon/min : " ;
cin >> fillingRate ;
//calculating totalPoolCapacity in US Gallon
totalPoolCapacity = length * width * depth * CUBIC_FOOT_TO_USGALLON ;
cout << "\n\nTotal pool capacity = " << totalPoolCapacity << " US Gallon." ;
cout << "\n\nPlease enter current amount of water in pool in US Gallon to " ;
cout << "\nfill the pool according to filling rate for the specific amount of time in minutes : " ;
cin >> poolCurrWaterAmount ;
//to check minimum and maximum range of current amount of water.
while( !(poolCurrWaterAmount >= 0.0 && poolCurrWaterAmount <= totalPoolCapacity ) )
{
cout << "\nYou have entered in-valid value for current amount of water!"
<< "\nEnter current amount of water value from 0 to maximum capacity of pool "
<< setw(10) << totalPoolCapacity << " in US Gallon : " ;
cin >> poolCurrWaterAmount ;
}
cout << "\nPlease enter time in minute to fill water in pool : " ;
cin >> timeToFillPool ;
//Calculations and message displayed are on the basis of whether the filling water
//will cause overflow of water after filling the pool or not.
//managing floating point eqaulity poolCurrWaterAmount == totalPoolCapacity
//setting the tolerance value EPSILON to 1/10000 = 0.0001 of a US Gallon
if ( fabs(poolCurrWaterAmount - totalPoolCapacity) < EPSILON)
{
cout << "\n\nPool is Full. Water cannot be added." ;
cout << "\nTotal water in pool is " << setw(10) << totalPoolCapacity << " US Gallon." ;
}
else if (fillingRate * timeToFillPool > (totalPoolCapacity - poolCurrWaterAmount) )
{
//case to check that time entered for filling water will cause overflow of water or not
cout << "\n\nWarning! Pool will be overflowed with water! No water added!" ;
cout << "\nCurrent amount of water in pool = "
<< setw(10) << poolCurrWaterAmount << " US Gallon." ;
cout << "\nMaximum time required to completely fill the pool at\nfilling rate of "
<< setw(10) << fillingRate << " US Gallon/min is "
<< setw(10) << ( (totalPoolCapacity - poolCurrWaterAmount) / fillingRate ) << " minute." ;
}
else //case where time entered for filling water will not cause overflow of water in pool
{
cout << "\n\nCurrent amount of water in pool = "
<< setw(10) << poolCurrWaterAmount << " US Gallon." ;
cout << "\nAfter filling "
<< setw(10) << (fillingRate * timeToFillPool) << " US Gallon at filling rate of "
<< setw(10) << fillingRate << " US Gallons/min for "
<< setw(10) << timeToFillPool << " minute\nthe new amount of water in pool is "
<< setw(10) << ( poolCurrWaterAmount + fillingRate * timeToFillPool ) << " US Gallon." ;
}
}
//end of main function
this is the ouput of the program: -
***********************************
Please enter swimming pool's dimensions,capacity,fill rate & drain rate information.
Enter Length in foot : 3
Enter width in foot : 2
Enter depth in foot : 2
Enter filling rate of water in US Gallon/min : 4
Total pool capacity = 89.77 US Gallon.
Please enter current amount of water in pool in US Gallon to
fill the pool according to filling rate for the specific amount of time in minutes : 89.77
You have entered in-valid value for current amount of water!
Enter current amount of water value from 0 to maximum capacity of pool 89.77 in US Gallon :
************************************************************************************************
The problem is that the internal value stored in totalPoolCapacity is 89.76624
and due to setprecision(2) it rounds of the value to 89.77 so when I enter 89.77 it doesn't
accept it as a right value although it should be right value according to display message.
I don't want to show whole value to user.
Also please explain how to handle this calculation with setprecision(2)
(totalPoolCapacity - poolCurrWaterAmount) / fillingRate
and what will be good EPSILON value to compare floating point numbers.
therefore time calculated and shown to user will not effect the overall calculation with rounding effects. That is what user sees, the program behaves according to that by manipulating internal representation of floating point numbers and their rounding off effects.
You should never do a floating point comparison like poolCurrWaterAmount <= totalPoolCapacity.
Instead you should do (poolCurrWaterAmount - totalPoolCapacity) < epsilon.
In your case, epsilon should be 0.005.
In general, for an equality operator, epsilon could be as small as
DBL_EPSILON.
For a deep-dive into this topic, including more rigorous algorithms, see comparing-floating-point-numbers-2012-edition.
One thing you could do is call std::fesetround(FE_DOWNWARD); so that your displayed numbers get rounded downwards rather than upwards. That would make it so that when the user re-enters the rounded value he saw in your output, the entered value is slightly less than the actual capacity of the pool, rather than slightly more, and would therefore avoid triggering your error message.
Or, if you don't like that approach, you could alternatively just set poolCurrWaterAmount = std::min(poolCurrWaterAmount, totalPoolCapacity); instead of emitting an error message, so that if the user enters a value greater than the pool's capacity, it is treated as if he entered a value equal to the pool's capacity.
For the first question:
The problem is that the internal value stored in totalPoolCapacity is
89.76624 and due to setprecision(2) it rounds of the value to 89.77 so when I enter 89.77 it doesn't accept it as a right value although it
should be right value according to display message. I don't want to
show whole value to user.
you can try set the poolCurrentWaterAmount to totalPoolCapacity if the user enters a value that is equal to the rounded value of totalPoolCapacity, for example:
#include <iostream>
#include <iomanip>
#include <sstream>
double round_double_value(double val, int prec) {
std::stringstream strstream;
strstream << std::fixed << std::setprecision(prec) << val;
double result;
strstream >> result;
return result;
}
int main()
{
const double CUBIC_FOOT_TO_USGALLON = 7.48052 ;
const double EPSILON = 0.0001 ;
double length = 3.0;
double width = 2.0;
double depth = 2.0;
double fillingRate = 4.0;
double totalPoolCapacity = length * width * depth * CUBIC_FOOT_TO_USGALLON ;
int out_precision = 2;
std::cout << std::fixed << std::setprecision(out_precision);
std::cout << "Total pool capacity = " << totalPoolCapacity << " US Gallon.\n" ;
double poolCurrWaterAmount = 89.77;
std::cout << "You entered current pool water amount = " << poolCurrWaterAmount << '\n';
if ((poolCurrWaterAmount > totalPoolCapacity)
&& (poolCurrWaterAmount == round_double_value(totalPoolCapacity, out_precision)) ) {
// Assume the user meant to input the maximum..
poolCurrWaterAmount = totalPoolCapacity;
}
if( !(poolCurrWaterAmount >= 0.0
&& poolCurrWaterAmount <= totalPoolCapacity ) )
{
std::cout << "You have entered in-valid value for current amount of water!\n";
return(1);
}
return 0;
}
I'm struggling getting my columns to align on an assignment I have for school. The directions are to ensure that each column heading aligns correctly with its respective column. I've tried everything I've found online so far, including the cout.width() and the setw(), neither of which has worked. I'm hoping it's not due to me implementing these methods incorrectly, though I can't think of any other reason why they wouldn't work. The assignment specifically has 2 student names to use as an example. Jones, Bob and Washington, George. Due to the major difference in the number of characters between one student and the next, the columns just won't align. I know that setting a column width should fix that, but in my case it isn't. Then code I'm supplying is using the setw() method, but I can supply how I tried using the cout.width() if needed. Any help is GREATLY appreciated.
#include <iostream>
#include <iomanip>
#include <string>
#include <cstdlib>
using std::cout;
using std::cin;
using namespace std;
const int ARRAYSIZE = 2;
struct studentData{
string lastName;
string firstName;
float studentGpa;
}studentArray[ARRAYSIZE];
void displayData(int a);
int main()
{
int counter = 0;
for (int a = 0; a < ARRAYSIZE; a++)
{
cout << "Enter last name: " << endl;
cin >> studentArray[a].lastName;
cout << "Enter first name: " << endl;
cin >> studentArray[a].firstName;
cout << "Enter GPA: "<< endl;
cin >> studentArray[a].studentGpa;
counter++;
}
cout << "\n\n";
displayData(counter);
return 0;
}
void displayData(int a)
{
int newSize = a;
cout << left << setw(20) <<"\nName(Last, First)";
cout << right << setw(20) << "GPA\n";
for (int z = 0; z < newSize; z++)
{
cout << studentArray[z].lastName << ", " << studentArray[z].firstName;
cout << right << setw(20) << fixed << setprecision(2) <<studentArray[z].studentGpa << endl;
}
}
And my console input/output:
Enter last name:
Jones
Enter first name:
Bob
Enter GPA:
3.0
Enter last name:
Washington
Enter first name:
George
Enter GPA:
4.0
Name(Last, First) GPA
Jones, Bob 3.00
Washington, George 4.00
You are along the right lines, but std::setw only applies to the next output operation. If you turn printing the name into one operation, instead of 3 (last, comma, first) then you can pad the width of that more easily:
void displayData(std::size_t a)
{
std::cout << std::left
<< std::setw(20)
<< "Name(Last, First)"
<< "GPA\n";
for (std::size_t z = 0; z < a; ++z)
{
std::cout << std::left
<< std::setw(20)
<< (studentArray[z].lastName + ", " + studentArray[z].firstName)
<< std::fixed << std::setprecision(2)
<< studentArray[z].studentGpa
<< '\n';
}
}
Output:
Name(Last, First) GPA
Jones, Bob 3.00
Washington, George 4.00
Here, both for printing the column headings, and for the student names, I use std::left to say that the content should be at the left of the padded total, and std::setw to pad this output operation to 20 characters total (by default it will pad with spaces).
In both cases, this is the 1st column in the output, so I don't need to do anything with the 2nd column. If you had a 3rd column, you would need to pad the 2nd as well.
I also replaced your ints with size_ts for array indexing. It's a little point, but you shouldn't really use a signed type for indexing into a container where accessing a negative index would be Undefined Behaviour.
Also, please reconsider your use of what are often considered bad practices: using namespace std; and endl (those are links to explanations).
I am writing a program that will calculate password strength according to two formulas. It requires the user to enter 2 passwords, one being eight characters or less and the other being 20 characters or more. The first parts executes with out problem. But, when I go to execute the second part, the prompt to enter the password and character set both show up at the same time and when I enter anything, whether it be numbers or characters, it aborts. I have checked over my code several times and don't understand why this is happening. Any help would be greatly appreciated!
int main()
{
//All variables and constants are declared
string eight_password, first_char, next_seven, twenty_password, first_char_twenty, next_seven_twenty, next_twelve, remaining;
int ep_length, character_set, first_char_length, next_seven_length, character_set_20, twenty_length;
double eight_ent_strength, eight_nist_strength, twenty_ent_strength;
const int NIST_FIRST = 4, NIST_SEVEN = 2, NIST_REM = 1, NIST_CHARACTER=94, NIST_BONUS=6;
const double NIST_TWELVE = 1.5;
//Console prompts for user to input password and character set
cout << "Hello! Please enter a password of 8 characters or less (including spaces!):" << endl;
getline(cin, eight_password);
cout << "What character set is being used?";
cin >> character_set>>ws;
//Password length and information entropy strength are calculated and saved to the appropriate variables
ep_length = eight_password.length();
eight_ent_strength = (ep_length*((log(character_set))/(log(2))));
//First character and next seven characters are extracted and saved to the appropriate variables
first_char = eight_password.substr(0, 1);
next_seven = eight_password.substr(1, 7);
//First character and next seven characters lengths are calculated and saved to the appropriate variables
first_char_length = first_char.length();
next_seven_length = next_seven.length();
//NIST strength is calculated and saved to the appropriate variable
eight_nist_strength = (first_char_length*NIST_FIRST) + (next_seven_length*NIST_SEVEN)+((character_set/NIST_CHARACTER)*NIST_BONUS);
//The information that was calculated is now printed back out on the console to be viewed by the user
cout << "Your password " << eight_password << " is " << ep_length << " characters long. According to the information " << endl;
cout<<"entropy formula, it has a strength of " << eight_ent_strength << "." << endl;
cout << "The first character is \"" << first_char << "\" and the next seven characters are \"" << next_seven << "\". " << endl;
cout << "According to the NIST formula, it has a strength of " << eight_nist_strength << "." << endl << endl;
cout << "Now, please enter a password of 8 characters or less (including spaces!):" << endl;
getline(cin, twenty_password);
cout << "What character set is being used?";
cin >> character_set_20;
twenty_length = twenty_password.length();
twenty_ent_strength = (twenty_length*((log(character_set_20)) / (log(2))));
first_char_twenty = twenty_password.substr(0, 1);
next_seven_twenty = twenty_password.substr(1, 7);
next_twelve = twenty_password.substr(7, 19);
remaining = twenty_password.substr(19);
cout << remaining;
return 0;
}
Change
cin >> character_set;
to
cin >> character_set;
cin.ignore( 1<<14, '\n' );
The call to getline(cin, twenty_password) consumes the previous newline leftover from cin >> character_set; which is why it doesn't wait. This same problem and solution is here: getline(cin, aString) receiving input without another enter
try this ignore function in between the 2 getline() functions to get-rid of the newline in the buffer
cin.ignore (std::numeric_limits<std::streamsize>::max(), '\n');
It works after this inclusion
The program I wrote below seems to be doing its job, but it produces the wrong answer. As described in the comments, I need to multiply 5 consecutive digits of the 1000-digit string and then find the largest product throughout the entire string.
/*
OBJECTIVE: Multiply five consecutive numbers and then compare products to find largest product
PROVE: 40824
*/
#include <iostream>
using namespace std;
int main()
{
// string length is 1000 characters
string str = "7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450";
int store = 0; // what I need to compare total to later
int total = 1; // total must be '1' for multiplication or result is always '0'
for(int i = 0;i<=999;) // i = 0 because that's the starting point for place of first number in string (str[0]); because the length is 1 short, 999 is the limit
{
total = 1; // what will be multiplied by inputs from str[]
int incr = i + 4; // places for the next 5 now that 0-4 are out of the way would be 5,6,7,8,9
// A general note: pattern this generates is that the end digit of an even break in groupings will always either be 4 or 9
// e.g. breaking the loop at position 44, 199, 234 will multiply 5 places properly without error
if(i == 0) // when starting out i = 0
{
for(;i<=4;i++) // i = str[0] which is then incrmemented to str[4] which is fifth number
{
cout << "i: " << i << '\t' << "str[" << i << "]: " << str[i] << '\t';
total*=(str[i] - '0'); // takes str[1],str[2],...str[4] and multiplies them together using total as valueholder.
// " - '0' " is because string numbers return ASCII codes
cout << "Total Product: " << total << '\t';
cout << "Store: " << store << '\t' << endl;
};
store = total > store ? total:store; // if total is greater than store, then store = total; simple enough
cout << endl; // line-break after first set of five
};
if(i>=5) // i is no longer 0
{
for(;i<=incr;i++) // i must be added by 5 now because 0,1,2,3,4 are first five and 5,6,7,8,9 are next
{
cout << "i: " << i << '\t' << "str[" << i << "]: " << str[i] << '\t';
total*=(str[i] - '0'); // takes str[1],str[2],...str[4] and multiplies them together using total as valueholder.
// " - '0' " is because string numbers return ASCII codes
cout << "Total Product: " << total << '\t';
cout << "Store: " << store << '\t' << endl;
};
store = total > store ? total:store; // if total is greater than store, then store = total; simple enough
cout << endl; // line-break after each set of five
};
};
cout << endl << endl << store; // final output; what should be answer
return 0;
}
The program shows that it is indeed multiplying five consecutive digits together and successfully bumping the store value when necessary, alas I still get a wrong answer of 31752 which should be 40824 (According to the Project Euler Solutions google-group: http://code.google.com/p/projecteuler-solutions/). What have I done wrong? What must I do to fix this program? Any tips for the future to avoid the probably-stupid mistake I'm not seeing?
From a cursory glance, I believe your problem lies in the fact that you modify i in multiple locations aside from the for loop declaration. This would cause your scan through the number to skip over sequences that could have potentially been correct.\
If you reason out your code, it starts at position 0 in the string. Then you have a conditional where you increment i 4 times, so then the next time through the loop, you start attempting to find a sequence starting at position 4 in the string, rather than starting at position 1.
Fixed Code:
/*
OBJECTIVE: Multiply five consecutive numbers and then compare products to find largest product
PROVE: 40824
*/
#include <iostream>
using namespace std;
int main()
{
// string length is 1000 characters
string str = "7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450";
int store = 0; // what I need to compare total to later
int total = 1; // what will be multiplied by inputs from str[]
int n = 0; // keeps track of single iterations
for(int i = 0;n<=999;n++) // i = 0 because that's the starting point for place of first number in string (str[0]); because the length is 1 shorter, 999 is the limit
{
total = 1; // must be set back to one each successful product loop
int incr = n + 4; // places for the next 5 after 0
// A general note: pattern this generates is that the end digit of an even break in groupings will always either be 4 or 9
// e.g. breaking the loop at position 44, 199, 234 will multiply 5 places properly without error
if(n == 0) // string starts at 0, must be accounted for especially
{
for(;i<=4;i++) // i = str[0] which is then incrmemented to str[4] which is fifth number
{
cout << "n: " << n << '\t' << "str[" << i << "]: " << str[i] << '\t';
total*=(str[i] - '0'); // takes str[1],str[2],...str[4] and multiplies them together using total as valueholder.
// " - '0' " is because string numbers return ASCII codes
cout << "Total Product: " << total << '\t';
cout << "Store: " << store << '\t' << endl;
};
store = total > store ? total:store; // if total is greater than store, then store = total; simple enough
};
for(;i<=incr;i++) // i must be added by 4 now because 0,1,2,3,4 are first five and 1,2,3,4,5 are next
{
cout << "n: " << n << '\t' << "str[" << i << "]: " << str[i] << '\t';
total*=(str[i] - '0'); // takes str[1],str[2],...str[4] and multiplies them together using total as valueholder.
// " - '0' " is because string numbers return ASCII codes
cout << "Total Product: " << total << '\t';
cout << "Store: " << store << '\t' << endl;
};
store = total > store ? total:store; // if total is greater than store, then store = total; simple enough
i = (n + 1); // i needs to start at a consecutive n each successful loop
};
cout << endl << endl << store;
return 0;
}
well, this problem makes me think of the Pattern Match Algorithm "KMP".
probably you should check out KMP, may gain some hint.