My adding program outputs SUPER WEIRD - string

I have been trying to read two numbers as string, convert them into int vectors, then add them for my lab at school (I loop this process 4 times). I have run my code to find very strange results.
My code:
#include <iostream>
#include <vector>
#include <string>
using namespace std;
void input(string &largeString1, string &largeString2);
void convert(string largeString1, string largeString2, vector<int> &largeInt1, vector<int> &largeInt2);
int asciiToInt(char ch);
void add(vector<int> largeInt1, vector<int> largeInt2, vector<int> &finalInt);
void output(const vector<int> finalInt);
int main()
{
string largeString1;
string largeString2;
vector<int> largeInt1(12, 0);
vector<int> largeInt2(12, 0);
vector<int> finalInt(13, 0);
for (int i = 0; i < 4; i++)
{
input(largeString1, largeString2);
convert(largeString1, largeString2, largeInt1, largeInt2);
add(largeInt1, largeInt2, finalInt);
output(finalInt);
}
system("pause");
return 0;
}
void input(string &largeString1, string &largeString2)
{
cout << "Input:" << endl << endl;
cin >> largeString1;
cin >> largeString2;
}
void convert(string largeString1, string largeString2, vector<int> &largeInt1, vector<int> &largeInt2)
{
int size1 = size(largeString1);
int size2 = size(largeString2);
for (int i = 0; i < size1; i++)
{
largeInt1[11 - i] = asciiToInt(largeString1[size1 - 1 - i]);
}
for (int j = 0; j < size2; j++)
{
largeInt2[11 - j] = asciiToInt(largeString2[size2 - 1 - j]);
}
}
int asciiToInt(char ch)
{
return (ch - '0');
}
void add(vector<int> largeInt1, vector<int> largeInt2, vector<int> &finalInt)
{
for (int i = 0; i < 12; i++)
{
if (largeInt1[11 - i] + largeInt2[11 - i] >= 10)
{
finalInt[12 - i] = finalInt[12 - i] + largeInt1[11 - i] + largeInt2[11 - i] - 10;
finalInt[12 - i - 1] = 1;
}
else
finalInt[12 - i] = finalInt[12 - i] + largeInt1[11 - i] + largeInt2[11 - i];
}
}
void output(const vector<int> finalInt)
{
cout << endl << "Output:" << endl << endl << "The sum is: ";
for (int i = 0; i < 13; i++)
{
cout << finalInt[i];
}
cout << endl << endl;
}
My outputs first output seems to give the correct answers, but the next 3 outputs do not add up (pun not intended). Also, how do I get rid of the annoying leading zeroes?

Related

How to debug Run-Time Check Failure #2 - Stack around the variable 'newString' was corrupted

This is a function to convert a long string (sentence) into a vector of strings. From the error, it seems obvious that the char ptr newString is causing some access violation, run-time issues. Probably it's length is the root cause?
Also, please note that the output of the function is just coming as expected, but with the run-time error occurring.
vector<string> convertStrgToStrVect(char *inString)
{
unsigned int end = 0, start = 0, i = 0, len = 0, j = 0;
char *inStr = inString, *s;
char newString[] = "";
vector<unsigned int> startLenVect, endLenVect, strLenVect;
vector<unsigned int> :: iterator itr1, itr2;
vector<string> stringVect;
vector<string> :: iterator itr3;
s = inStr;
// Add an extra space an the end of the string
for( i = 0; i < strlen(s); i++)
{
}
s[i] = ' ';
i++;
s[i] = '\0';
cout << s << endl;
cout << strlen(s) << endl;
// Create vectors for start and end indexes to split the words separated by a space
for( i = 0; i < strlen(s); i++)
{
if(s[i] != ' ')
{
end++;
len = end - start;
}
else
{
endLenVect.push_back(end);
startLenVect.push_back(start);
strLenVect.push_back(len);
end = i+1;
start = i+1;
}
}
cout << "startLenVect: ";
for(itr1 = startLenVect.begin(); itr1 != startLenVect.end(); itr1++)
{
cout << *itr1 << " ";
}
cout << "endLenVect: ";
for(itr1 = endLenVect.begin(); itr1 != endLenVect.end(); itr1++)
{
cout << *itr1 << " " << endl;
}
for(itr1 = startLenVect.begin(), itr2 = endLenVect.begin(); itr1 != startLenVect.end(); itr1++, itr2++)
{
strcpy(newString, "");
for(i = *itr1, j = 0; i < *itr2; i++, j++)
{
newString[j] = s[i];
}
newString[j] = '\0';
stringVect.push_back(newString);
}
for(itr3 = stringVect.begin(); itr3 != stringVect.end(); itr3++)
{
cout << "stringVect: " << *itr3 << endl;
}
return stringVect;
}

(C++) How to round decimals like 1.05000?

I have a problem where in my algorhithm I should round the result down to 5 decimal places, with the zeros included even after the last number.
The only test-case not working in my algorhithm is:
Input:
milk 1
4
bread
meat
milk
aaaaa
Output:
1.05000 // and my output displays 1.5
For an example; my result of 1.05 should display as 1.05000 or 1.2 as 1.20000. Now, the rest of the algorhithm is working fine, so the only problem is the rounding part:
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
char name[50];
cin >> name;
double price = 0;
cin >> price;
int N;
cin >> N;
char check_name[50];
double result = 0;
bool found = false;
double result_circle = 0;
int finally_found = 0
for (int k = 0; k < N; k++) {
cin >> check_name;
for (int i = 0; i < strlen(name); i++) {
if (name[i] == check_name[i]) {
found = true;
} else {
found = false;
break;
}
}
if (found) {
finally_found++;
break;
}
}
if (finally_found > 0) {
result = price + (price * 0.05);
} else {
result = price + (price * 0.18);
}
// here is where the problem starts
result_circle = result * 1000000; //temporarily expanding the number to the 6th decimal place
if ((int)result_circle % 10 > 4) { // checking if the 6th decimal place is bigger than 4
result_circle += 10; // increasing the 5th decimal place
}
result_circle = (int)result_circle / 10; // removing the 6th decimal place which we were checking
cout << (int)result_circle / 100000 << '.' << (int)result_circle % 100000; // here is the main problem, where 105000 % 100000 is seen as 5000 not 05000
return 0;
}
I assume the main problem here is that ‘105000 % 100000 = 5000’ because the 0 after the decimal point is unfortunately left out.
If anyone could display the simplest way to fix this problem it would be great.
#include <iomanip>
.
.
.
cout << fixed << setprecision(5) << result;
This is the code that worked for me, answered by _Bob.
The full code:
#include <iostream>
#include <string.h>
#include <iomanip>
using namespace std;
int main()
{
char name[50];
cin >> name;
double price = 0;
cin >> price;
int N;
cin >> N;
char check_name[50];
double result = 0;
bool found = false;
double result_circle = 0;
int finally_found = 0;
for (int k = 0; k < N; k++) {
cin >> check_name;
for (int i = 0; i < strlen(name); i++) {
if (name[i] == check_name[i]) {
found = true;
} else {
found = false;
break;
}
}
if (found) {
finally_found++;
}
}
if (finally_found > 0) {
result = price + (price * 0.05);
} else {
result = price + (price * 0.18);
}
cout << fixed << setprecision(5) << result;
return 0;
}

C++ Console Application1.exe has triggered a breakpoint,Visual Studio 2015.Array and random numbers

I'm trying to run this code in VS 2015 c++, but I have this problem: while getting the correct answer, I also get the error C++ Console Application1.exe has triggered a breakpoint. My task is to randomly choose 10 numbers between 1 and 50, show these numbers and sort them by odd and even numbers. Thanks in advance.
#include<iostream>
#include<ctime>
using namespace std;
void main() {
const int size = 10;
srand(time(NULL));
int arr[size], odd = 0, even = 0;
for (int i = 0; i < size; i++)
{
arr[i] = rand() % 50 + 1;
cout << arr[i] << '\t';
}
cout << endl;
int *arrOdd = new int[odd];
int *arrEven = new int[even];
for (int i = 0; i < size; i++)
{
if (arr[i] % 2 == 0) {
arrEven[even] = arr[i];
even++;
}
else
{
arrOdd[odd] = arr[i];
odd++;
}
}
int a = 0, b = 0;
for (int i = 0; i < size; i++)
if (arr[i] % 2 == 0) {
cout << "arrEven = " << arrEven[a] << endl;
a++;
}
else
{
cout << "arrOdd = " << arrOdd[b] << endl;
b++;
}
system("pause");
}
int arr[size], odd = 0, even = 0;
//odd's and event's dont change
//...
int *arrOdd = new int[odd];
int *arrEven = new int[even];
So you are trying to allocate arrays with 0 length

How to set end condition of string iterator in C++?

i'm new in C++. I met a problem when doing exercise in C++ Primer 5th edition(Ex 9.43). The for loop can't stop in my function find_and_replace. Here is the code:
#include <iostream>
#include <string>
int find_and_replace(std::string& org_str, const std::string& str4find, const std::string& str4replace);
int main(int argc, char const *argv[])
{
std::string str("I am a very loooooong string to be process!");
int find_times;
find_times = find_and_replace(str, "a", "###");
std::cout << find_times << std::endl;
std::cout << str << std::endl;
return 0;
}
int find_and_replace(std::string& org_str, const std::string& str4find, const std::string& str4replace)
{
int find_times = 0;
if (org_str.size() < str4find.size())
{
std::cout << "Error: The original string is too short!" << std::endl;
return find_times;
}
else if (org_str == str4find)
{
org_str.assign(str4replace);
++find_times;
return find_times;
}
for (auto i = org_str.begin(), j = i + str4find.size(); j != org_str.end(); )
{
std::string temp(i, j);
// std::cout << temp << std::endl;
if (temp == str4find)
{
j = org_str.erase(i, j);
org_str.insert(i, str4replace.begin(), str4replace.end());
// std::cout << org_str << std::endl;
// std::cout << *i << std::endl;
j = i + str4find.size();
// std::cout << *j << std::endl;
++find_times;
}
else
{
++i;
++j;
}
}
if (org_str.substr(org_str.size() - str4find.size()) == str4find)
{
org_str.erase(org_str.size() - str4find.size(), str4find.size());
org_str.insert(org_str.end(), str4replace.begin(), str4replace.end());
++find_times;
}
return find_times;
}
I update iterator j and i after erase and insert operation, so i think, i and j are not invalid.
Anyone can tell me why the end condition: j != org_str.end() is not work?
SOLVED!
Although the cout output is correct, the i iterator is already invalid. Here is the new code. Just change few lines in for-loop.
#include <iostream>
#include <string>
int find_and_replace(std::string& org_str, const std::string& str4find, const std::string& str4replace);
int main(int argc, char const *argv[])
{
std::string str("I am a very loooooong string to be process!");
int find_times;
find_times = find_and_replace(str, "a", "###");
std::cout << find_times << std::endl;
std::cout << str << std::endl;
return 0;
}
int find_and_replace(std::string& org_str, const std::string& str4find, const std::string& str4replace)
{
int find_times = 0;
if (org_str.size() < str4find.size())
{
std::cout << "Error: The original string is too short!" << std::endl;
return find_times;
}
else if (org_str == str4find)
{
org_str.assign(str4replace);
++find_times;
return find_times;
}
for (auto i = org_str.begin(), j = i + str4find.size(); j != org_str.end(); )
{
std::string temp(i, j);
// std::cout << temp << std::endl;
if (temp == str4find)
{
i = org_str.erase(i, j);
//CHANGE BEGIN
//org_str.insert(i, str4replace.begin(), str4replace.end());
for (auto k = str4replace.begin(); k != str4replace.end(); ++k, ++i)
{
i = org_str.insert(i, *k);
}
//CHANGE END
// std::cout << org_str << std::endl;
// std::cout << *i << std::endl;
j = i + str4find.size();
// std::cout << *j << std::endl;
++find_times;
}
else
{
++i;
++j;
}
}
if (org_str.substr(org_str.size() - str4find.size()) == str4find)
{
org_str.erase(org_str.size() - str4find.size(), str4find.size());
org_str.insert(org_str.end(), str4replace.begin(), str4replace.end());
++find_times;
The i iterator become invalid after calling erase and insert, you should use the return values of that functions or recalculate the position starting from s.begin() again.
#include <iostream>
#include <string>
using std::cout;
using std::string;
int find_and_replace( string &s, const string &oldVal, const string &newVal ) {
int find_times = 0;
if ( s.size() < oldVal.size() ) {
std::cout << "Error: The original string is too short!" << std::endl;
return find_times;
}
if ( s == oldVal ) {
s.assign(newVal);
return ++find_times;
}
for ( auto i = s.begin(); i != s.end(); ++i ) {
// compare using iterators
bool is_equal = true;
auto k = i;
for ( auto j = oldVal.begin(); j != oldVal.end(); ++j, ++k ) {
if ( k == s.end() || *j != *k ) {
is_equal = false;
break;
}
}
if ( is_equal ) {
auto ie = s.erase(i, i + oldVal.size());
// This may not work with older version of g++
auto ii = s.insert(ie, std::begin(newVal), std::end(newVal));
i = ii + newVal.size() - 1;
++find_times;
}
}
return find_times;
}
int main() {
string str("I am a very loooooong string to be process!");
int find_times;
find_times = find_and_replace(str, "a", "###");
cout << find_times << '\n';
cout << str << '\n';
return 0;
}
The output (as you can see here) is:
2
I ###m ### very loooooong string to be process!

finding largest and smallest numbers in an array

i have to find the max and min value in array of random numbers... here is my code:
#include<iostream>
using namespace std;
void populateArray();
int findMin();
int findMax();
int computeTotal();
int arr[50];
int small, big;
void main()
{
big = small = arr[0];
populateArray();
findMin();
findMax();
computeTotal();
system("pause");
}
void populateArray()
{
for (int i = 0; i < 48; i++)
{
arr[i] = rand() % 1000;
cout << arr[i] << " ";
}
cout << endl;
}
int findMin()
{
for (int i = 0; i < 48; i++)
{
if (arr[i] < small)
{
small = arr[i];
}
cout << "The smallest number is " << small << endl;
return 0;
}
}
int findMax()
{
for (int i = 0; i < 48; i++)
{
if (arr[i] > big)
{
big = arr[i];
}
}
cout << "The biggest number is " << big << endl;
return 0;
}
int computeTotal()
{
int sum = 0;
sum = big + small;
cout << "Total= " << sum << endl;
return 0;
}
But the problem is that it does not display the minimum and maximum value from the array... it displays minimum value 0 and max 995 which are not included even in the array..
any help??

Resources