I am trying to make a code to allow the user to enter a very huge number “bigger than the size of the normal “int” , so am trying to take the user input as a string and take each value from the string and pass it to an int e.g. s="3786473646768" so int will take each value and save it in a vector.
This is a very simple syntax I enter the two strings but if they aren’t equal it gives me a window saying “Expression: String Subscript is out of range”
Any help please ?
#include "stdafx.h"
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
int val1=0,val2=0,val3=0,val4=0;
string i,p;
vector <int>add;
vector <int>sub;
cout<<"Enter a number: "<<endl;
cin>>i;
int sizei=i.length();
cout<<"Enter a number: "<<endl;
cin>>p;
int sizep=p.length();
for (int n=0; n<sizei; n++)
{
val1=int(i[n]);
val1=val1-48;
val2=int(p[n]);
val2=val2-48;
val3=val1+val2;
val4=val1-val2;
add.push_back(val3);
sub.push_back(val4);
}
cout<<"\n";
for(unsigned int a=0;a<add.size();a++)
cout<<add.at(a);
cout<<"\n";
for(unsigned int a=0;a<sub.size();a++)
cout<<sub.at(a);
system("pause");
return 0;
}
Within this loop, the value of n goes from
0 ... sizei - 1However, if sizep is less than
sizei - 1, it will try to accessp[n]or equivalently,p[sizei-1], which is outside the bounds of the string you declared.