Integer to string issue
Oct 27, 2017 at 2:50am UTC
I am currently trying to change an integer to a string. I am successful in that, but I am unable to make my program's class return the string without error. It states that "no operator "[]" matches these operands. The issue is in my last "for" statement in my LargeInt.cpp, first "cout". Any suggestions?
LargeInt.h
1 2 3 4 5 6 7 8 9 10 11 12 13
#pragma once
class LargeInt
{
public :
LargeInt() {};
void ADDITION();
private :
int Number1[255], Number2[255] , ADD[255];
char S1[255], S2[255];
int L1, L2;
};
LargeInt.cpp
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
#include "LargeInt.h"
#include <string>
#include <iostream>
#include "stdafx.h"
using namespace std;
void LargeInt::ADDITION()
{
cout << "What is your first number to add? " << endl;
cin >> S1;
cout << "What is your second number to add? " << endl;
cin >> S2;
cout << endl;
for (L1 = 0; S1[L1] != '\0' ; L1++)
{
Number1[L1] = S1[L1] - '0' ;
}
for (L2 = 0; S2[L2] != '\0' ; L2++)
{
Number2[L2] = S2[L2] - '0' ;
}
int carry = 0;
int k = 0;
int i = L1 - 1;
int j = L2 - 1;
string final = to_string(k);
for (; i >= 0 && j >= 0; i--, j--, k++)
{
ADD[k] = (Number1[i] + Number2[j] + carry) % 10;
carry = (Number1[i] + Number2[j] + carry) / 10;
}
if (L1 > L2)
{
while (i >= 0) {
ADD[k++] = (Number1[i] + carry) % 10;
carry = (Number1[i--] + carry) / 10;
}
}
else if (L1 < L2)
{
while (j >= 0) {
ADD[k++] = (Number2[j--] + carry) / 10;
}
}
else
{
if (carry > 0)
ADD[k++] = carry;
}
cout << "The first number you entered was: " ;
cout << S1 << endl;
cout << "The second number you endered was: " ;
cout << S2 << endl;
cout << "Sum of your two large numbers is = " ;
for (k--; k >= 0; k--)
{
cout << ADD[final] << endl;
cout << endl;
}
}
main.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <string>
#include "LargeInt.h"
using namespace std;
int main()
{
cout << endl;
cout << "Enter a number to calculate the addtion of two large numbers" << endl;
LargeInt digit;
digit.ADDITION();
cout << endl;
system("pause" );
return 0;
}
Oct 27, 2017 at 8:11am UTC
I am currently trying to change an integer to a string.
There is no reason to do that.
final
is a string hence cannot be used as an index and will always contain "0". Why don't you use just
k
?
Oct 28, 2017 at 8:44pm UTC
‘
final ’ is a reserved word:
http://en.cppreference.com/w/cpp/language/final
You can’t declare a variable named ‘final ’. <-- Wrong. See Repeater’s correction below.
To create a std::string from an int array, you need to transform any ‘int’ back into a ‘char’.
Hints:
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
#include "LargeInt.h"
#include <string>
#include <iostream>
void LargeInt::ADDITION()
{
std::cout << "What is your first number to add? " ;
std::cin >> S1;
std::cout << "What is your second number to add? " ;
std::cin >> S2;
std::cout << '\n' ;
for (L1 = 0; S1[L1] != '\0' ; L1++) { Number1[L1] = S1[L1] - '0' ; }
for (L2 = 0; S2[L2] != '\0' ; L2++) { Number2[L2] = S2[L2] - '0' ; }
int carry = 0;
int k = 0;
int i = L1 - 1;
int j = L2 - 1;
for (; i >= 0 && j >= 0; i--, j--, k++)
{
ADD[k] = (Number1[i] + Number2[j] + carry) % 10;
carry = (Number1[i] + Number2[j] + carry) / 10;
}
if (L1 > L2)
{
while (i >= 0) {
ADD[k++] = (Number1[i] + carry) % 10;
carry = (Number1[i--] + carry) / 10;
}
}
else if (L1 < L2)
{
while (j >= 0) {
ADD[k++] = (Number2[j--] + carry) / 10;
}
}
else
{
if (carry > 0)
ADD[k++] = carry;
}
std::cout << "The first number you entered was: " << S1
<< "\nThe second number you entered was: " << S2
<< "\nSum of your two large numbers is = " ;
// for (k--; k >= 0; k--) { std::cout << ADD[k]; }
// std::cout << '\n';
std::string result;
for (k--; k >= 0; k--) { result += ADD[k] + '0' ; }
std::cout << result << '\n' ;
}
Last edited on Oct 29, 2017 at 7:48am UTC
Oct 28, 2017 at 8:47pm UTC
Not really helpful, as i can't understand your code.. but itoa should convert int to char arrays which can be converted into strings,
Oct 28, 2017 at 8:48pm UTC
While I certainly agree it's bad form, the page you link to explicitly states that "final"
may be used to name objects and functions.
Oct 29, 2017 at 7:48am UTC
Repeater wrote:While I certainly agree it's bad form, the page you link to explicitly states that "final"
may be used to name objects and functions.
You’re right, Repeater, I really missed that point. Thank you for your correction.
I’ve edited my previous post.
Last edited on Oct 29, 2017 at 7:53am UTC
Topic archived. No new replies allowed.