Konsal
Konsal

Reputation: 25

Adding integers split into vectors

So, i'm trying to build a program that takes 2 integers. Later it splits the plus/minus sign and the digits and saves them into vectors. Last i would like to add those two integers. I managed to split the ints into the vectors and vector.size() gives me correct answers although i can't print them. Any clue on how to make the addition of the integers? Thanks,

This is my code so far:

#include <iostream>
#include <vector>
#include <cmath>

using namespace std;

int
main(){
cout<<"Give 2 integers.\n";
int a,b;
cin>>a;
cin>>b;

vector<int> adigits;
//10 for positive, 20 for negative integer
adigits.push_back(a<0 ? 20:10);
  a=abs(a);
  while(a>0){
  adigits.push_back(a%10);
  a=a/10;
 }

  vector<int> bdigits;
  //10 for positive, 20 for negative integer
  bdigits.push_back(b<0 ? 20:10);
  b=abs(b);
  while(b>0){
    bdigits.push_back(b%10);
    b=b/10;
  }

 vector <int>::size_type c;
 vector <int>::size_type d;
 c=adigits.size();
 d=bdigits.size();

  cout<<c;
  cout<<d;

 return 0;
}

Upvotes: 1

Views: 225

Answers (1)

Borealid
Borealid

Reputation: 98529

adigits.push_back(a<0 ? 20:10);
 while(a>0){
  adigits.push_back(a%10);
  a=a/10;
 }

This will just push a 20 onto adigits if a is already less than zero before the loop executes even once.

Rethink your logic; the bdigits loop has the same flaw.

Upvotes: 0

Related Questions