Convert string to int

1

In this program I have to enter 2 whole numbers a and b and the program should display on the screen the numbers harshad (numbers that are divisible by the sum of their numbers) between a and b. A and b must be positive, and b greater than a.

In the compilation it tells me that there are failures in the operator ++ of the for, and one of the whiles (I do not know which).

The program that I have used is:

#include<iostream>
#include<string>
using namespace std;

int main(){
    int a,b,n,suma;
    string numero;
    bool harshad;

    harshad = (numero%suma=0);
    cout<<"introduce los numeros a y b:";
    cin>>a>>b;

    while((a<=0)||(b<a){
        cout<<"introduce los numeros a y b:";
        cin>>a>>b;
    }

    for(numero=a;numero<=b;numero++){
        n=0;
        while(numero.at(n)!=0){
            numero.at(n)=numero.at(n)+numero.at(n+1);
        }
    }
    suma=numero.at(n);

    while(harshad=true){
        cout<<numero<<endl;
        numero++;
    }
    return 0;
}
    
asked by Pablo 20.05.2017 в 17:51
source

1 answer

1

The first error is that in the for you can not use a string as a counter:

string numero;
    for(numero=a;numero<=b;numero++){
            n=0;
            while(numero.at(n)!=0){
                numero.at(n)=numero.at(n)+numero.at(n+1);
            }

What you should do is create an auxiliary variable to perform the for:

for(int i=a;i<=b;i++){
    //lo que quieras realizar
}

Second, in the while you are doing a assignment , not a comparison . I need you to add a =

while(harshad==true){
        // lo que quieras realizar
    }

Third, I recommend that you re-analyze the logic of your code, because it has several errors.

Greetings

    
answered by 20.05.2017 в 19:04