Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

Please help!! Addition in C++"?

0 votes
asked Oct 11, 2018 by Akash
edited Oct 11, 2018
#include <iostream>
using namespace std;
int
main ()
{
    cout << "Enter your name " << flush;
    string name;
    cin >> name;
    cout << "One day " << name << " will be successful." << endl;
    
    cout << "Enter any number " << flush;
    string num;
    cin >> num;
    int nnum = num + 1;
    cout << "If you add " << num << " with 1 you get " << nnum << endl;
    
    return 0;
    
}

In second last line num + 1 i can't add why please any one explain.......

#TIA

3 Answers

+1 vote
answered Oct 11, 2018 by Lukas (540 points)
#include <iostream>
using namespace std;

int main()
{
    int firstNumber, secondNumber, sumOfTwoNumbers;
    
    cout << "Enter two integers: ";
    cin >> firstNumber >> secondNumber;

    // sum of two numbers in stored in variable sumOfTwoNumbers
    sumOfTwoNumbers = firstNumber + secondNumber;

    // Prints sum 
    cout << firstNumber << " + " <<  secondNumber << " = " << sumOfTwoNumbers;     

    return 0;
}
0 votes
answered Oct 16, 2018 by Dienne1234 (160 points)
#include <iostream>
using namespace std;
int
main ()
{
    cout << "Enter your name " << flush;
    string name;
    cin >> name;
    cout << "One day " << name << " will be successful." << endl;
    
    cout << "Enter any number " << flush;
    int num;
    cin >> num;
    int nnum = num + 1;
    cout << "If you add " << num << " with 1 you get " << nnum << endl;
    
    return 0;
    
}

Note : You have declared 'num ' as a string. You cannot add a variable which is a string  to an integer . If you want to add numbers both variables have to be of the same type i.e integer , float etc and not string.
0 votes
answered Oct 16, 2018 by Muhammad Abubakar
Because nnum & num don't have same data type.
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...